Use int32 for pool sizes
This commit is contained in:
@@ -84,7 +84,7 @@ type Pool struct {
|
|||||||
|
|
||||||
constructor Constructor
|
constructor Constructor
|
||||||
destructor Destructor
|
destructor Destructor
|
||||||
maxSize int
|
maxSize int32
|
||||||
|
|
||||||
acquireCount int64
|
acquireCount int64
|
||||||
acquireDuration time.Duration
|
acquireDuration time.Duration
|
||||||
@@ -95,7 +95,7 @@ type Pool struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewPool creates a new pool.
|
// 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{
|
return &Pool{
|
||||||
cond: sync.NewCond(new(sync.Mutex)),
|
cond: sync.NewCond(new(sync.Mutex)),
|
||||||
destructWG: &sync.WaitGroup{},
|
destructWG: &sync.WaitGroup{},
|
||||||
@@ -126,10 +126,10 @@ func (p *Pool) Close() {
|
|||||||
|
|
||||||
// Stat is a snapshot of Pool statistics.
|
// Stat is a snapshot of Pool statistics.
|
||||||
type Stat struct {
|
type Stat struct {
|
||||||
constructingResources int
|
constructingResources int32
|
||||||
acquiredResources int
|
acquiredResources int32
|
||||||
idleResources int
|
idleResources int32
|
||||||
maxResources int
|
maxResources int32
|
||||||
acquireCount int64
|
acquireCount int64
|
||||||
acquireDuration time.Duration
|
acquireDuration time.Duration
|
||||||
emptyAcquireCount int64
|
emptyAcquireCount int64
|
||||||
@@ -137,28 +137,28 @@ type Stat struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TotalResource returns the total number of resources.
|
// TotalResource returns the total number of resources.
|
||||||
func (s *Stat) TotalResources() int {
|
func (s *Stat) TotalResources() int32 {
|
||||||
return s.constructingResources + s.acquiredResources + s.idleResources
|
return s.constructingResources + s.acquiredResources + s.idleResources
|
||||||
}
|
}
|
||||||
|
|
||||||
// ConstructingResources returns the number of resources with construction in progress in
|
// ConstructingResources returns the number of resources with construction in progress in
|
||||||
// the pool.
|
// the pool.
|
||||||
func (s *Stat) ConstructingResources() int {
|
func (s *Stat) ConstructingResources() int32 {
|
||||||
return s.constructingResources
|
return s.constructingResources
|
||||||
}
|
}
|
||||||
|
|
||||||
// AcquiredResources returns the number of acquired resources in the pool.
|
// AcquiredResources returns the number of acquired resources in the pool.
|
||||||
func (s *Stat) AcquiredResources() int {
|
func (s *Stat) AcquiredResources() int32 {
|
||||||
return s.acquiredResources
|
return s.acquiredResources
|
||||||
}
|
}
|
||||||
|
|
||||||
// IdleResources returns the number of idle resources in the pool.
|
// IdleResources returns the number of idle resources in the pool.
|
||||||
func (s *Stat) IdleResources() int {
|
func (s *Stat) IdleResources() int32 {
|
||||||
return s.idleResources
|
return s.idleResources
|
||||||
}
|
}
|
||||||
|
|
||||||
// MaxResources returns the maximum size of the pool.
|
// MaxResources returns the maximum size of the pool.
|
||||||
func (s *Stat) MaxResources() int {
|
func (s *Stat) MaxResources() int32 {
|
||||||
return s.maxResources
|
return s.maxResources
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -254,7 +254,7 @@ func (p *Pool) Acquire(ctx context.Context) (*Resource, error) {
|
|||||||
emptyAcquire = true
|
emptyAcquire = true
|
||||||
|
|
||||||
// If there is room to create a resource do so
|
// 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}
|
res := &Resource{pool: p, creationTime: startTime, status: resourceStatusConstructing}
|
||||||
p.allResources = append(p.allResources, res)
|
p.allResources = append(p.allResources, res)
|
||||||
p.destructWG.Add(1)
|
p.destructWG.Add(1)
|
||||||
|
|||||||
+20
-20
@@ -105,8 +105,8 @@ func TestPoolAcquireDoesNotCreatesResourceWhenItWouldExceedMaxSize(t *testing.T)
|
|||||||
|
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
||||||
assert.Equal(t, 1, createCounter.Value())
|
assert.EqualValues(t, 1, createCounter.Value())
|
||||||
assert.Equal(t, 1, pool.Stat().TotalResources())
|
assert.EqualValues(t, 1, pool.Stat().TotalResources())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPoolAcquireWithCancellableContext(t *testing.T) {
|
func TestPoolAcquireWithCancellableContext(t *testing.T) {
|
||||||
@@ -132,8 +132,8 @@ func TestPoolAcquireWithCancellableContext(t *testing.T) {
|
|||||||
|
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
||||||
assert.Equal(t, 1, createCounter.Value())
|
assert.EqualValues(t, 1, createCounter.Value())
|
||||||
assert.Equal(t, 1, pool.Stat().TotalResources())
|
assert.EqualValues(t, 1, pool.Stat().TotalResources())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPoolAcquireReturnsErrorFromFailedResourceCreate(t *testing.T) {
|
func TestPoolAcquireReturnsErrorFromFailedResourceCreate(t *testing.T) {
|
||||||
@@ -322,20 +322,20 @@ func TestPoolStatResources(t *testing.T) {
|
|||||||
<-waitingChan
|
<-waitingChan
|
||||||
stat := pool.Stat()
|
stat := pool.Stat()
|
||||||
|
|
||||||
assert.Equal(t, 2, stat.TotalResources())
|
assert.EqualValues(t, 2, stat.TotalResources())
|
||||||
assert.Equal(t, 1, stat.ConstructingResources())
|
assert.EqualValues(t, 1, stat.ConstructingResources())
|
||||||
assert.Equal(t, 1, stat.AcquiredResources())
|
assert.EqualValues(t, 1, stat.AcquiredResources())
|
||||||
assert.Equal(t, 0, stat.IdleResources())
|
assert.EqualValues(t, 0, stat.IdleResources())
|
||||||
assert.Equal(t, 10, stat.MaxResources())
|
assert.EqualValues(t, 10, stat.MaxResources())
|
||||||
|
|
||||||
resAcquired.Release()
|
resAcquired.Release()
|
||||||
|
|
||||||
stat = pool.Stat()
|
stat = pool.Stat()
|
||||||
assert.Equal(t, 2, stat.TotalResources())
|
assert.EqualValues(t, 2, stat.TotalResources())
|
||||||
assert.Equal(t, 1, stat.ConstructingResources())
|
assert.EqualValues(t, 1, stat.ConstructingResources())
|
||||||
assert.Equal(t, 0, stat.AcquiredResources())
|
assert.EqualValues(t, 0, stat.AcquiredResources())
|
||||||
assert.Equal(t, 1, stat.IdleResources())
|
assert.EqualValues(t, 1, stat.IdleResources())
|
||||||
assert.Equal(t, 10, stat.MaxResources())
|
assert.EqualValues(t, 10, stat.MaxResources())
|
||||||
|
|
||||||
close(endWaitChan)
|
close(endWaitChan)
|
||||||
}
|
}
|
||||||
@@ -455,8 +455,8 @@ func TestResourceDestroyRemovesResourceFromPool(t *testing.T) {
|
|||||||
|
|
||||||
res.Hijack()
|
res.Hijack()
|
||||||
|
|
||||||
assert.Equal(t, 0, pool.Stat().TotalResources())
|
assert.EqualValues(t, 0, pool.Stat().TotalResources())
|
||||||
assert.Equal(t, 0, destructorCalls.Value())
|
assert.EqualValues(t, 0, destructorCalls.Value())
|
||||||
|
|
||||||
// Can still call Value and CreationTime
|
// Can still call Value and CreationTime
|
||||||
res.Value()
|
res.Value()
|
||||||
@@ -471,9 +471,9 @@ func TestResourceHijackRemovesResourceFromPoolButDoesNotDestroy(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, 1, res.Value())
|
assert.Equal(t, 1, res.Value())
|
||||||
|
|
||||||
assert.Equal(t, 1, pool.Stat().TotalResources())
|
assert.EqualValues(t, 1, pool.Stat().TotalResources())
|
||||||
res.Destroy()
|
res.Destroy()
|
||||||
assert.Equal(t, 0, pool.Stat().TotalResources())
|
assert.EqualValues(t, 0, pool.Stat().TotalResources())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestResourcePanicsOnUsageWhenNotAcquired(t *testing.T) {
|
func TestResourcePanicsOnUsageWhenNotAcquired(t *testing.T) {
|
||||||
@@ -627,7 +627,7 @@ func ExamplePool() {
|
|||||||
destructor := func(value interface{}) {
|
destructor := func(value interface{}) {
|
||||||
value.(net.Conn).Close()
|
value.(net.Conn).Close()
|
||||||
}
|
}
|
||||||
maxPoolSize := 10
|
maxPoolSize := int32(10)
|
||||||
|
|
||||||
pool := puddle.NewPool(constructor, destructor, maxPoolSize)
|
pool := puddle.NewPool(constructor, destructor, maxPoolSize)
|
||||||
|
|
||||||
@@ -661,7 +661,7 @@ func ExamplePool() {
|
|||||||
|
|
||||||
func BenchmarkPoolAcquireAndRelease(b *testing.B) {
|
func BenchmarkPoolAcquireAndRelease(b *testing.B) {
|
||||||
benchmarks := []struct {
|
benchmarks := []struct {
|
||||||
poolSize int
|
poolSize int32
|
||||||
clientCount int
|
clientCount int
|
||||||
cancellable bool
|
cancellable bool
|
||||||
}{
|
}{
|
||||||
|
|||||||
Reference in New Issue
Block a user