注释优化

This commit is contained in:
kercylan98 2023-04-28 09:47:04 +08:00
parent 513f8eda60
commit a7626965c0
1 changed files with 10 additions and 1 deletions

View File

@ -1,6 +1,10 @@
package synchronization
import "sync"
import (
"go.uber.org/zap"
"minotaur/utils/log"
"sync"
)
func NewPool[T any](bufferSize int, generator func() T, releaser func(data T)) *Pool[T] {
pool := &Pool[T]{
@ -14,6 +18,10 @@ func NewPool[T any](bufferSize int, generator func() T, releaser func(data T)) *
return pool
}
// Pool 线程安全的对象缓冲池
// - 一些高频临时生成使用的对象可以通过 Pool 进行管理,例如属性计算等
// - 缓冲区内存在可用对象时直接返回,否则新建一个进行返回
// - 通过 Release 将使用完成的对象放回缓冲区,超出缓冲区大小的对象将被放弃
type Pool[T any] struct {
mutex sync.Mutex
buffers []T
@ -31,6 +39,7 @@ 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"))
return slf.generator()
}