注释优化
This commit is contained in:
parent
513f8eda60
commit
a7626965c0
|
@ -1,6 +1,10 @@
|
||||||
package synchronization
|
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] {
|
func NewPool[T any](bufferSize int, generator func() T, releaser func(data T)) *Pool[T] {
|
||||||
pool := &Pool[T]{
|
pool := &Pool[T]{
|
||||||
|
@ -14,6 +18,10 @@ func NewPool[T any](bufferSize int, generator func() T, releaser func(data T)) *
|
||||||
return pool
|
return pool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Pool 线程安全的对象缓冲池
|
||||||
|
// - 一些高频临时生成使用的对象可以通过 Pool 进行管理,例如属性计算等
|
||||||
|
// - 缓冲区内存在可用对象时直接返回,否则新建一个进行返回
|
||||||
|
// - 通过 Release 将使用完成的对象放回缓冲区,超出缓冲区大小的对象将被放弃
|
||||||
type Pool[T any] struct {
|
type Pool[T any] struct {
|
||||||
mutex sync.Mutex
|
mutex sync.Mutex
|
||||||
buffers []T
|
buffers []T
|
||||||
|
@ -31,6 +39,7 @@ func (slf *Pool[T]) Get() T {
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
slf.mutex.Unlock()
|
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()
|
return slf.generator()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue