1
0
mirror of https://github.com/fumiama/orbyte.git synced 2026-06-05 02:00:30 +08:00

feat: add dup-detection

This commit is contained in:
源文雨
2025-04-14 23:12:19 +09:00
parent b1efb0e49f
commit 63dd01e81e
3 changed files with 14 additions and 0 deletions

2
go.mod
View File

@@ -1,3 +1,5 @@
module github.com/fumiama/orbyte
go 1.18
require github.com/RomiChan/syncx v0.0.0-20240418144900-b7402ffdebc7

2
go.sum Normal file
View File

@@ -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=

10
pool.go
View File

@@ -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()