From 3ad1330cd937635ce56f1ca70365836f19c97fc8 Mon Sep 17 00:00:00 2001 From: kercylan98 Date: Tue, 19 Sep 2023 12:41:13 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20concurrent.Pool=20=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E9=9D=99=E9=BB=98=E6=A8=A1=E5=BC=8F=E5=8F=AF=E9=80=89=E9=A1=B9?= =?UTF-8?q?=20WithPoolSilent=EF=BC=8C=E5=9C=A8=E8=AF=A5=E6=A8=A1=E5=BC=8F?= =?UTF-8?q?=E4=B8=8B=E5=BD=93=E7=BC=93=E5=86=B2=E5=8C=BA=E5=A4=A7=E5=B0=8F?= =?UTF-8?q?=E4=B8=8D=E8=B6=B3=E6=97=B6=EF=BC=8C=E5=B0=86=E4=B8=8D=E5=86=8D?= =?UTF-8?q?=E8=BE=93=E5=87=BA=E8=AD=A6=E5=91=8A=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/concurrent/pool.go | 11 +++++++---- utils/concurrent/pool_options.go | 12 ++++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) create mode 100644 utils/concurrent/pool_options.go diff --git a/utils/concurrent/pool.go b/utils/concurrent/pool.go index 35cb4250..d50f8d33 100644 --- a/utils/concurrent/pool.go +++ b/utils/concurrent/pool.go @@ -30,6 +30,7 @@ type Pool[T any] struct { generator func() T releaser func(data T) warn int64 + silent bool } // EAC 动态调整缓冲区大小,适用于突发场景使用 @@ -53,10 +54,12 @@ func (slf *Pool[T]) Get() T { slf.mutex.Unlock() return data } - now := time.Now().Unix() - if now-slf.warn >= 1 { - log.Warn("Pool", log.String("Get", "the number of buffer members is insufficient, consider whether it is due to unreleased or inappropriate buffer size"), zap.Stack("stack")) - slf.warn = now + if !slf.silent { + now := time.Now().Unix() + if now-slf.warn >= 1 { + log.Warn("Pool", log.String("Get", "the number of buffer members is insufficient, consider whether it is due to unreleased or inappropriate buffer size"), zap.Stack("stack")) + slf.warn = now + } } slf.mutex.Unlock() diff --git a/utils/concurrent/pool_options.go b/utils/concurrent/pool_options.go new file mode 100644 index 00000000..1fc713f9 --- /dev/null +++ b/utils/concurrent/pool_options.go @@ -0,0 +1,12 @@ +package concurrent + +// PoolOption 线程安全的对象缓冲池选项 +type PoolOption[T any] func(pool *Pool[T]) + +// WithPoolSilent 静默模式 +// - 静默模式下,当缓冲区大小不足时,将不再输出警告日志 +func WithPoolSilent[T any]() PoolOption[T] { + return func(pool *Pool[T]) { + pool.silent = true + } +}