feat: concurrent.Pool 新增静默模式可选项 WithPoolSilent,在该模式下当缓冲区大小不足时,将不再输出警告日志

This commit is contained in:
kercylan98 2023-09-19 12:41:13 +08:00
parent 2d9ffad2ab
commit 3ad1330cd9
2 changed files with 19 additions and 4 deletions

View File

@ -30,6 +30,7 @@ type Pool[T any] struct {
generator func() T generator func() T
releaser func(data T) releaser func(data T)
warn int64 warn int64
silent bool
} }
// EAC 动态调整缓冲区大小,适用于突发场景使用 // EAC 动态调整缓冲区大小,适用于突发场景使用
@ -53,11 +54,13 @@ func (slf *Pool[T]) Get() T {
slf.mutex.Unlock() slf.mutex.Unlock()
return data return data
} }
if !slf.silent {
now := time.Now().Unix() now := time.Now().Unix()
if now-slf.warn >= 1 { 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")) 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.warn = now
} }
}
slf.mutex.Unlock() slf.mutex.Unlock()
return slf.generator() return slf.generator()

View File

@ -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
}
}