Skip to content

Commit

Permalink
feat: concurrent.Pool 新增静默模式可选项 WithPoolSilent,在该模式下当缓冲区大小不足时,将不再输出警告日志
Browse files Browse the repository at this point in the history
  • Loading branch information
kercylan98 committed Sep 19, 2023
1 parent 2d9ffad commit 3ad1330
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
11 changes: 7 additions & 4 deletions utils/concurrent/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Pool[T any] struct {
generator func() T
releaser func(data T)
warn int64
silent bool
}

// EAC 动态调整缓冲区大小,适用于突发场景使用
Expand All @@ -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()

Expand Down
12 changes: 12 additions & 0 deletions utils/concurrent/pool_options.go
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit 3ad1330

Please sign in to comment.