Skip to content

Commit

Permalink
perf: 优化 concurrent.Pool 池对象不够用的日志打印为 1 秒一次,而不是频繁打印
Browse files Browse the repository at this point in the history
  • Loading branch information
kercylan98 committed Sep 6, 2023
1 parent 4214ea4 commit 989b9da
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions utils/concurrent/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/kercylan98/minotaur/utils/log"
"go.uber.org/zap"
"sync"
"time"
)

func NewPool[T any](bufferSize int, generator func() T, releaser func(data T)) *Pool[T] {
Expand Down Expand Up @@ -52,12 +53,13 @@ func (slf *Pool[T]) Get() T {
slf.mutex.Unlock()
return data
}
slf.warn++
slf.mutex.Unlock()
if slf.warn >= 256 {
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 = 0
slf.warn = now
}
slf.mutex.Unlock()

return slf.generator()
}

Expand Down

0 comments on commit 989b9da

Please sign in to comment.