sync: Add a Reset method to Gate which re-opens it.

This commit is contained in:
Sasha Koshka 2024-11-05 17:12:50 -05:00
parent f40eca261b
commit 709f41a974

View File

@ -8,6 +8,7 @@ type Gate[T any] struct {
channel chan T
lock sync.RWMutex
open bool
bufferSize int
}
// NewGate creates a new gate with no buffer.
@ -23,18 +24,23 @@ func NewBufferedGate[T any] (buffer int) Gate[T] {
return Gate[T] {
channel: make(chan T, buffer),
open: true,
bufferSize: buffer,
}
}
// Send sends and item to the channel, returning whether the item was sent.
func (this *Gate[T]) Send (item T) bool {
if !this.Open() { return false }
this.lock.RLock()
defer this.lock.RUnlock()
if !this.open { return false }
this.channel <- item
return true
}
// Receive returns a receive-only channel that can be used to receive items.
func (this *Gate[T]) Receive () <- chan T {
this.lock.RLock()
defer this.lock.RUnlock()
return this.channel
}
@ -48,6 +54,16 @@ func (this *Gate[T]) Close () error {
return nil
}
// Reset re-opens the gate if it is closed, and creates a new channel.
func (this *Gate[T]) Reset () error {
this.lock.Lock()
defer this.lock.Unlock()
this.open = true
if this.channel != nil { close(this.channel) }
this.channel = make(chan T, this.bufferSize)
return nil
}
// Open returns whether the gate is open.
func (this *Gate[T]) Open () bool {
this.lock.RLock()