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:
2
go.mod
2
go.mod
@@ -1,3 +1,5 @@
|
|||||||
module github.com/fumiama/orbyte
|
module github.com/fumiama/orbyte
|
||||||
|
|
||||||
go 1.18
|
go 1.18
|
||||||
|
|
||||||
|
require github.com/RomiChan/syncx v0.0.0-20240418144900-b7402ffdebc7
|
||||||
|
|||||||
2
go.sum
Normal file
2
go.sum
Normal 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
10
pool.go
@@ -5,6 +5,8 @@ import (
|
|||||||
"runtime"
|
"runtime"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
|
||||||
|
"github.com/RomiChan/syncx"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Pool lightweight general pool.
|
// Pool lightweight general pool.
|
||||||
@@ -18,6 +20,7 @@ type Pool[T any] struct {
|
|||||||
// 64 bit align
|
// 64 bit align
|
||||||
|
|
||||||
pool sync.Pool
|
pool sync.Pool
|
||||||
|
dupmap syncx.Map[*Item[T], struct{}]
|
||||||
pooler Pooler[T]
|
pooler Pooler[T]
|
||||||
|
|
||||||
noputbak bool
|
noputbak bool
|
||||||
@@ -90,6 +93,7 @@ func (pool *Pool[T]) newempty() *Item[T] {
|
|||||||
isrecycled := item.stat.hasdestroyed()
|
isrecycled := item.stat.hasdestroyed()
|
||||||
if isrecycled {
|
if isrecycled {
|
||||||
pool.decin()
|
pool.decin()
|
||||||
|
pool.dupmap.Delete(item)
|
||||||
}
|
}
|
||||||
item.stat = status(0)
|
item.stat = status(0)
|
||||||
isfull := atomic.LoadInt32(&pool.countin) > pool.inlim ||
|
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 {
|
atomic.LoadInt32(&pool.countin) > pool.inlim {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_, exist := pool.dupmap.LoadOrStore(item, struct{}{})
|
||||||
|
if exist {
|
||||||
|
panic("duplicated put")
|
||||||
|
}
|
||||||
|
|
||||||
pool.pool.Put(item)
|
pool.pool.Put(item)
|
||||||
|
|
||||||
pool.decout()
|
pool.decout()
|
||||||
|
|||||||
Reference in New Issue
Block a user