sync: Don't panic on double close of Gate, return error instead
This commit is contained in:
parent
709f41a974
commit
9fd40a37b8
11
sync/gate.go
11
sync/gate.go
@ -2,6 +2,16 @@ package usync
|
||||
|
||||
import "sync"
|
||||
|
||||
// Error defines errors that this package can produce
|
||||
type Error string; const (
|
||||
ErrAlreadyClosed Error = "AlreadyClosed"
|
||||
)
|
||||
|
||||
// Error fullfills the error interface.
|
||||
func (err Error) Error () string {
|
||||
return string(err)
|
||||
}
|
||||
|
||||
// Gate wraps a channel and allows the receiver to abruptly stop receiving
|
||||
// messages without causing the sender to lock up.
|
||||
type Gate[T any] struct {
|
||||
@ -47,6 +57,7 @@ func (this *Gate[T]) Receive () <- chan T {
|
||||
// Close closes the gate, drains all remaining messages, and closes the channel.
|
||||
func (this *Gate[T]) Close () error {
|
||||
this.lock.Lock()
|
||||
if !this.open { return ErrAlreadyClosed }
|
||||
this.open = false
|
||||
this.lock.Unlock()
|
||||
for len(this.channel) > 0 { <- this.channel }
|
||||
|
Reference in New Issue
Block a user