From 63dd01e81ea2f116452e15bc48d3c11900409774 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Mon, 14 Apr 2025 23:12:19 +0900 Subject: [PATCH] feat: add dup-detection --- go.mod | 2 ++ go.sum | 2 ++ pool.go | 10 ++++++++++ 3 files changed, 14 insertions(+) create mode 100644 go.sum diff --git a/go.mod b/go.mod index 4b6efe0..10c4684 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module github.com/fumiama/orbyte go 1.18 + +require github.com/RomiChan/syncx v0.0.0-20240418144900-b7402ffdebc7 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..380fe88 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/RomiChan/syncx v0.0.0-20240418144900-b7402ffdebc7 h1:S/ferNiehVjNaBMNNBxUjLtVmP/YWD6Yh79RfPv4ehU= +github.com/RomiChan/syncx v0.0.0-20240418144900-b7402ffdebc7/go.mod h1:vD7Ra3Q9onRtojoY5sMCLQ7JBgjUsrXDnDKyFxqpf9w= diff --git a/pool.go b/pool.go index a1aa1d0..fe3603b 100644 --- a/pool.go +++ b/pool.go @@ -5,6 +5,8 @@ import ( "runtime" "sync" "sync/atomic" + + "github.com/RomiChan/syncx" ) // Pool lightweight general pool. @@ -18,6 +20,7 @@ type Pool[T any] struct { // 64 bit align pool sync.Pool + dupmap syncx.Map[*Item[T], struct{}] pooler Pooler[T] noputbak bool @@ -90,6 +93,7 @@ func (pool *Pool[T]) newempty() *Item[T] { isrecycled := item.stat.hasdestroyed() if isrecycled { pool.decin() + pool.dupmap.Delete(item) } item.stat = status(0) isfull := atomic.LoadInt32(&pool.countin) > pool.inlim || @@ -113,6 +117,12 @@ func (pool *Pool[T]) put(item *Item[T]) { atomic.LoadInt32(&pool.countin) > pool.inlim { return } + + _, exist := pool.dupmap.LoadOrStore(item, struct{}{}) + if exist { + panic("duplicated put") + } + pool.pool.Put(item) pool.decout()