From: qydysky Date: Thu, 12 May 2022 02:33:50 +0000 (+0800) Subject: fix X-Git-Tag: v0.9.5 X-Git-Url: http://127.0.0.1:8081/?a=commitdiff_plain;h=8989e1fe0f2c4a7e506591f406153398b9a7c186;p=part%2F.git fix --- diff --git a/idpool/Idpool.go b/idpool/Idpool.go index 3ed36ac..80583b9 100644 --- a/idpool/Idpool.go +++ b/idpool/Idpool.go @@ -7,8 +7,9 @@ import ( ) type Idpool struct { - pool sync.Pool - sum int64 + pool sync.Pool + sum int64 + initF func() interface{} } type Id struct { @@ -18,6 +19,7 @@ type Id struct { func New(f func() interface{}) *Idpool { return &Idpool{ + initF: f, pool: sync.Pool{ New: func() interface{} { var o = new(Id) @@ -31,6 +33,9 @@ func New(f func() interface{}) *Idpool { func (t *Idpool) Get() (o *Id) { o = t.pool.Get().(*Id) + if o.Item == nil { + o.Item = t.initF() + } atomic.AddInt64(&t.sum, 1) return } diff --git a/idpool/Idpool_test.go b/idpool/Idpool_test.go index 894b2f1..11dabbb 100644 --- a/idpool/Idpool_test.go +++ b/idpool/Idpool_test.go @@ -4,9 +4,11 @@ import ( "testing" ) +type test struct{} + func Test(t *testing.T) { pool := New(func() interface{} { - return new(int) + return &test{} }) a := pool.Get() b := pool.Get()