]> 127.0.0.1 Git - part/.git/commitdiff
fix v0.9.5
authorqydysky <qydysky@foxmail.com>
Thu, 12 May 2022 02:33:50 +0000 (10:33 +0800)
committerqydysky <qydysky@foxmail.com>
Thu, 12 May 2022 02:33:50 +0000 (10:33 +0800)
idpool/Idpool.go
idpool/Idpool_test.go

index 3ed36ac6b4e6ceb313e5e2bed2237ccbf6c2e1b0..80583b9a0a6d06793ec33dc6ffea1b07d66f5734 100644 (file)
@@ -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
 }
index 894b2f183b6c1b7867b5328048755b594f800407..11dabbbc6add9c68acc9c8abe8a2195a89fc2827 100644 (file)
@@ -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()