perf: 优化 concurrent.Pool 池对象不够用的日志打印为 1 秒一次,而不是频繁打印

This commit is contained in:
kercylan98 2023-09-06 16:53:33 +08:00
parent 4214ea4c2b
commit 989b9da33d
1 changed files with 6 additions and 4 deletions

View File

@ -4,6 +4,7 @@ import (
"github.com/kercylan98/minotaur/utils/log" "github.com/kercylan98/minotaur/utils/log"
"go.uber.org/zap" "go.uber.org/zap"
"sync" "sync"
"time"
) )
func NewPool[T any](bufferSize int, generator func() T, releaser func(data T)) *Pool[T] { func NewPool[T any](bufferSize int, generator func() T, releaser func(data T)) *Pool[T] {
@ -52,12 +53,13 @@ func (slf *Pool[T]) Get() T {
slf.mutex.Unlock() slf.mutex.Unlock()
return data return data
} }
slf.warn++ now := time.Now().Unix()
slf.mutex.Unlock() if now-slf.warn >= 1 {
if slf.warn >= 256 {
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 = 0 slf.warn = now
} }
slf.mutex.Unlock()
return slf.generator() return slf.generator()
} }