对象缓冲池警告策略优化

This commit is contained in:
kercylan98 2023-05-19 17:02:09 +08:00
parent 83b61b9254
commit 750992cfe4
1 changed files with 7 additions and 1 deletions

View File

@ -28,6 +28,7 @@ type Pool[T any] struct {
bufferSize int
generator func() T
releaser func(data T)
warn int
}
func (slf *Pool[T]) Get() T {
@ -39,7 +40,11 @@ func (slf *Pool[T]) Get() T {
return data
}
slf.mutex.Unlock()
log.Warn("Pool", zap.String("Get", "the number of buffer members is insufficient, consider whether it is due to unreleased or inappropriate buffer size"))
slf.warn++
if slf.warn >= 100 {
log.Warn("Pool", zap.String("Get", "the number of buffer members is insufficient, consider whether it is due to unreleased or inappropriate buffer size"))
slf.warn = 0
}
return slf.generator()
}
@ -54,6 +59,7 @@ func (slf *Pool[T]) Close() {
slf.bufferSize = 0
slf.generator = nil
slf.releaser = nil
slf.warn = 0
slf.mutex.Unlock()
}