From a7626965c0e67c20fcfd18bcf467354db28c6dec Mon Sep 17 00:00:00 2001 From: kercylan98 Date: Fri, 28 Apr 2023 09:47:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B3=A8=E9=87=8A=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/synchronization/pool.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/utils/synchronization/pool.go b/utils/synchronization/pool.go index 4751dd6..7bbbf03 100644 --- a/utils/synchronization/pool.go +++ b/utils/synchronization/pool.go @@ -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() }