diff --git a/pool.go b/pool.go index c16ee50..8688cfb 100644 --- a/pool.go +++ b/pool.go @@ -60,11 +60,13 @@ type Pool struct { allResources []*Resource idleResources []*Resource - maxSize int - closed bool - constructor Constructor destructor Destructor + maxSize int + + acquireCount uint64 + + closed bool } func NewPool(constructor Constructor, destructor Destructor, maxSize int) *Pool { @@ -97,53 +99,53 @@ func (p *Pool) Close() { } type Stat struct { - constructing int - acquired int - idle int - maxSize int + constructingResources int + acquiredResources int + idleResources int + maxResources int } -// Size returns the total number of resources in the pool. -func (s *Stat) Size() int { - return s.constructing + s.acquired + s.idle +// TotalResource returns the total number of resources in the pool. +func (s *Stat) TotalResources() int { + return s.constructingResources + s.acquiredResources + s.idleResources } -// Constructing returns the number of resources with construction in progress in +// ConstructingResources returns the number of resources with construction in progress in // the pool. -func (s *Stat) Constructing() int { - return s.constructing +func (s *Stat) ConstructingResources() int { + return s.constructingResources } -// Acquired returns the number of acquired resources in the pool. -func (s *Stat) Acquired() int { - return s.acquired +// AcquiredResources returns the number of acquired resources in the pool. +func (s *Stat) AcquiredResources() int { + return s.acquiredResources } -// Idle returns the number of idle resources in the pool. -func (s *Stat) Idle() int { - return s.idle +// IdleResources returns the number of idle resources in the pool. +func (s *Stat) IdleResources() int { + return s.idleResources } -// MaxSize returns the maximum size of the pool -func (s *Stat) MaxSize() int { - return s.maxSize +// MaxResources returns the maximum size of the pool +func (s *Stat) MaxResources() int { + return s.maxResources } // Stat returns the current pool statistics. func (p *Pool) Stat() *Stat { p.cond.L.Lock() s := &Stat{ - maxSize: p.maxSize, + maxResources: p.maxSize, } for _, res := range p.allResources { switch res.status { case resourceStatusConstructing: - s.constructing += 1 + s.constructingResources += 1 case resourceStatusIdle: - s.idle += 1 + s.idleResources += 1 case resourceStatusAcquired: - s.acquired += 1 + s.acquiredResources += 1 } } diff --git a/pool_test.go b/pool_test.go index e48213e..77578d2 100644 --- a/pool_test.go +++ b/pool_test.go @@ -113,7 +113,7 @@ func TestPoolAcquireDoesNotCreatesResourceWhenItWouldExceedMaxSize(t *testing.T) wg.Wait() assert.Equal(t, 1, createCounter.Value()) - assert.Equal(t, 1, pool.Stat().Size()) + assert.Equal(t, 1, pool.Stat().TotalResources()) } func TestPoolAcquireWithCancellableContext(t *testing.T) { @@ -140,7 +140,7 @@ func TestPoolAcquireWithCancellableContext(t *testing.T) { wg.Wait() assert.Equal(t, 1, createCounter.Value()) - assert.Equal(t, 1, pool.Stat().Size()) + assert.Equal(t, 1, pool.Stat().TotalResources()) } func TestPoolAcquireReturnsErrorFromFailedResourceCreate(t *testing.T) { @@ -292,20 +292,20 @@ func TestPoolStat(t *testing.T) { <-waitingChan stat := pool.Stat() - assert.Equal(t, 2, stat.Size()) - assert.Equal(t, 1, stat.Constructing()) - assert.Equal(t, 1, stat.Acquired()) - assert.Equal(t, 0, stat.Idle()) - assert.Equal(t, 10, stat.MaxSize()) + 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()) resAcquired.Release() stat = pool.Stat() - assert.Equal(t, 2, stat.Size()) - assert.Equal(t, 1, stat.Constructing()) - assert.Equal(t, 0, stat.Acquired()) - assert.Equal(t, 1, stat.Idle()) - assert.Equal(t, 10, stat.MaxSize()) + 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()) close(endWaitChan) } @@ -325,7 +325,7 @@ func TestResourceDestroyRemovesResourceFromPool(t *testing.T) { res.Hijack() - assert.Equal(t, 0, pool.Stat().Size()) + assert.Equal(t, 0, pool.Stat().TotalResources()) assert.Equal(t, 0, closeCalls.Value()) } @@ -337,9 +337,9 @@ func TestResourceHijackRemovesResourceFromPoolButDoesNotDestroy(t *testing.T) { require.NoError(t, err) assert.Equal(t, 1, res.Value()) - assert.Equal(t, 1, pool.Stat().Size()) + assert.Equal(t, 1, pool.Stat().TotalResources()) res.Destroy() - assert.Equal(t, 0, pool.Stat().Size()) + assert.Equal(t, 0, pool.Stat().TotalResources()) } func TestPoolAcquireReturnsErrorWhenPoolIsClosed(t *testing.T) {