From 0395a39c2d011f016b6922fca030b8c9507f1ff6 Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Sat, 13 Apr 2019 18:39:08 -0500 Subject: [PATCH] Use int32 for pool sizes --- pool.go | 24 ++++++++++++------------ pool_test.go | 40 ++++++++++++++++++++-------------------- 2 files changed, 32 insertions(+), 32 deletions(-) diff --git a/pool.go b/pool.go index db0cf02..9687abd 100644 --- a/pool.go +++ b/pool.go @@ -84,7 +84,7 @@ type Pool struct { constructor Constructor destructor Destructor - maxSize int + maxSize int32 acquireCount int64 acquireDuration time.Duration @@ -95,7 +95,7 @@ type Pool struct { } // NewPool creates a new pool. -func NewPool(constructor Constructor, destructor Destructor, maxSize int) *Pool { +func NewPool(constructor Constructor, destructor Destructor, maxSize int32) *Pool { return &Pool{ cond: sync.NewCond(new(sync.Mutex)), destructWG: &sync.WaitGroup{}, @@ -126,10 +126,10 @@ func (p *Pool) Close() { // Stat is a snapshot of Pool statistics. type Stat struct { - constructingResources int - acquiredResources int - idleResources int - maxResources int + constructingResources int32 + acquiredResources int32 + idleResources int32 + maxResources int32 acquireCount int64 acquireDuration time.Duration emptyAcquireCount int64 @@ -137,28 +137,28 @@ type Stat struct { } // TotalResource returns the total number of resources. -func (s *Stat) TotalResources() int { +func (s *Stat) TotalResources() int32 { return s.constructingResources + s.acquiredResources + s.idleResources } // ConstructingResources returns the number of resources with construction in progress in // the pool. -func (s *Stat) ConstructingResources() int { +func (s *Stat) ConstructingResources() int32 { return s.constructingResources } // AcquiredResources returns the number of acquired resources in the pool. -func (s *Stat) AcquiredResources() int { +func (s *Stat) AcquiredResources() int32 { return s.acquiredResources } // IdleResources returns the number of idle resources in the pool. -func (s *Stat) IdleResources() int { +func (s *Stat) IdleResources() int32 { return s.idleResources } // MaxResources returns the maximum size of the pool. -func (s *Stat) MaxResources() int { +func (s *Stat) MaxResources() int32 { return s.maxResources } @@ -254,7 +254,7 @@ func (p *Pool) Acquire(ctx context.Context) (*Resource, error) { emptyAcquire = true // If there is room to create a resource do so - if len(p.allResources) < p.maxSize { + if len(p.allResources) < int(p.maxSize) { res := &Resource{pool: p, creationTime: startTime, status: resourceStatusConstructing} p.allResources = append(p.allResources, res) p.destructWG.Add(1) diff --git a/pool_test.go b/pool_test.go index 5e107a4..2b6876c 100644 --- a/pool_test.go +++ b/pool_test.go @@ -105,8 +105,8 @@ func TestPoolAcquireDoesNotCreatesResourceWhenItWouldExceedMaxSize(t *testing.T) wg.Wait() - assert.Equal(t, 1, createCounter.Value()) - assert.Equal(t, 1, pool.Stat().TotalResources()) + assert.EqualValues(t, 1, createCounter.Value()) + assert.EqualValues(t, 1, pool.Stat().TotalResources()) } func TestPoolAcquireWithCancellableContext(t *testing.T) { @@ -132,8 +132,8 @@ func TestPoolAcquireWithCancellableContext(t *testing.T) { wg.Wait() - assert.Equal(t, 1, createCounter.Value()) - assert.Equal(t, 1, pool.Stat().TotalResources()) + assert.EqualValues(t, 1, createCounter.Value()) + assert.EqualValues(t, 1, pool.Stat().TotalResources()) } func TestPoolAcquireReturnsErrorFromFailedResourceCreate(t *testing.T) { @@ -322,20 +322,20 @@ func TestPoolStatResources(t *testing.T) { <-waitingChan stat := pool.Stat() - assert.Equal(t, 2, stat.TotalResources()) - assert.Equal(t, 1, stat.ConstructingResources()) - assert.Equal(t, 1, stat.AcquiredResources()) - assert.Equal(t, 0, stat.IdleResources()) - assert.Equal(t, 10, stat.MaxResources()) + assert.EqualValues(t, 2, stat.TotalResources()) + assert.EqualValues(t, 1, stat.ConstructingResources()) + assert.EqualValues(t, 1, stat.AcquiredResources()) + assert.EqualValues(t, 0, stat.IdleResources()) + assert.EqualValues(t, 10, stat.MaxResources()) resAcquired.Release() stat = pool.Stat() - assert.Equal(t, 2, stat.TotalResources()) - assert.Equal(t, 1, stat.ConstructingResources()) - assert.Equal(t, 0, stat.AcquiredResources()) - assert.Equal(t, 1, stat.IdleResources()) - assert.Equal(t, 10, stat.MaxResources()) + assert.EqualValues(t, 2, stat.TotalResources()) + assert.EqualValues(t, 1, stat.ConstructingResources()) + assert.EqualValues(t, 0, stat.AcquiredResources()) + assert.EqualValues(t, 1, stat.IdleResources()) + assert.EqualValues(t, 10, stat.MaxResources()) close(endWaitChan) } @@ -455,8 +455,8 @@ func TestResourceDestroyRemovesResourceFromPool(t *testing.T) { res.Hijack() - assert.Equal(t, 0, pool.Stat().TotalResources()) - assert.Equal(t, 0, destructorCalls.Value()) + assert.EqualValues(t, 0, pool.Stat().TotalResources()) + assert.EqualValues(t, 0, destructorCalls.Value()) // Can still call Value and CreationTime res.Value() @@ -471,9 +471,9 @@ func TestResourceHijackRemovesResourceFromPoolButDoesNotDestroy(t *testing.T) { require.NoError(t, err) assert.Equal(t, 1, res.Value()) - assert.Equal(t, 1, pool.Stat().TotalResources()) + assert.EqualValues(t, 1, pool.Stat().TotalResources()) res.Destroy() - assert.Equal(t, 0, pool.Stat().TotalResources()) + assert.EqualValues(t, 0, pool.Stat().TotalResources()) } func TestResourcePanicsOnUsageWhenNotAcquired(t *testing.T) { @@ -627,7 +627,7 @@ func ExamplePool() { destructor := func(value interface{}) { value.(net.Conn).Close() } - maxPoolSize := 10 + maxPoolSize := int32(10) pool := puddle.NewPool(constructor, destructor, maxPoolSize) @@ -661,7 +661,7 @@ func ExamplePool() { func BenchmarkPoolAcquireAndRelease(b *testing.B) { benchmarks := []struct { - poolSize int + poolSize int32 clientCount int cancellable bool }{