From e9bc9fb48175dc6544a570fd82d71af66ca8f801 Mon Sep 17 00:00:00 2001 From: kercylan98 Date: Tue, 19 Sep 2023 12:37:18 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20buffer.Unbounded=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E6=96=B0=E7=9A=84=E6=9E=84=E9=80=A0=E5=87=BD=E6=95=B0=EF=BC=8C?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E7=9C=81=E7=95=A5=20generateNil=20=E5=87=BD?= =?UTF-8?q?=E6=95=B0=EF=BC=8C=E6=96=B0=E5=A2=9E=20IsClosed=20=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E6=A3=80=E6=9F=A5=E6=97=A0=E7=95=8C=E7=BC=93=E5=86=B2?= =?UTF-8?q?=E5=8C=BA=E6=98=AF=E5=90=A6=E5=B7=B2=E7=BB=8F=E5=85=B3=E9=97=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/buffer/unbounded.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/utils/buffer/unbounded.go b/utils/buffer/unbounded.go index ba3a6b4..68b4bc8 100644 --- a/utils/buffer/unbounded.go +++ b/utils/buffer/unbounded.go @@ -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 @@ -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 +}