Skip to content

Commit

Permalink
feat: buffer.Unbounded 增加新的构造函数,支持省略 generateNil 函数,新增 IsClosed 函数检查无…
Browse files Browse the repository at this point in the history
…界缓冲区是否已经关闭
  • Loading branch information
kercylan98 committed Sep 19, 2023
1 parent 9b68def commit e9bc9fb
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions utils/buffer/unbounded.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ func NewUnbounded[V any](generateNil func() V) *Unbounded[V] {
return &Unbounded[V]{c: make(chan V, 1), nil: generateNil()}
}

// NewUnboundedN 与 NewUnbounded 相同,只是省略了 generateNil 参数
func NewUnboundedN[V any]() *Unbounded[V] {
return NewUnbounded[V](func() (v V) {
return v
})
}

// Unbounded 是无界缓冲区的实现
type Unbounded[V any] struct {
c chan V
Expand Down Expand Up @@ -72,3 +79,10 @@ func (slf *Unbounded[V]) Close() {
slf.closed = true
close(slf.c)
}

// IsClosed 是否已关闭
func (slf *Unbounded[V]) IsClosed() bool {
slf.mu.Lock()
defer slf.mu.Unlock()
return slf.closed
}

0 comments on commit e9bc9fb

Please sign in to comment.