Remove runtime changing of pool size
Reducing pool to minimize required functionality
This commit is contained in:
@@ -13,9 +13,6 @@ const (
|
|||||||
resourceStatusHijacked = iota
|
resourceStatusHijacked = iota
|
||||||
)
|
)
|
||||||
|
|
||||||
const maxUint = ^uint(0)
|
|
||||||
const maxInt = int(maxUint >> 1)
|
|
||||||
|
|
||||||
// ErrClosedPool occurs on an attempt to get a connection from a closed pool.
|
// ErrClosedPool occurs on an attempt to get a connection from a closed pool.
|
||||||
var ErrClosedPool = errors.New("cannot get from closed pool")
|
var ErrClosedPool = errors.New("cannot get from closed pool")
|
||||||
|
|
||||||
@@ -70,11 +67,11 @@ type Pool struct {
|
|||||||
destructor Destructor
|
destructor Destructor
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPool(constructor Constructor, destructor Destructor) *Pool {
|
func NewPool(constructor Constructor, destructor Destructor, maxSize int) *Pool {
|
||||||
return &Pool{
|
return &Pool{
|
||||||
cond: sync.NewCond(new(sync.Mutex)),
|
cond: sync.NewCond(new(sync.Mutex)),
|
||||||
destructWG: &sync.WaitGroup{},
|
destructWG: &sync.WaitGroup{},
|
||||||
maxSize: maxInt,
|
maxSize: maxSize,
|
||||||
constructor: constructor,
|
constructor: constructor,
|
||||||
destructor: destructor,
|
destructor: destructor,
|
||||||
}
|
}
|
||||||
@@ -115,16 +112,6 @@ func (p *Pool) MaxSize() int {
|
|||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetMaxSize sets the maximum size of the pool. It panics if n < 1.
|
|
||||||
func (p *Pool) SetMaxSize(n int) {
|
|
||||||
if n < 1 {
|
|
||||||
panic("pool MaxSize cannot be < 1")
|
|
||||||
}
|
|
||||||
p.cond.L.Lock()
|
|
||||||
p.maxSize = n
|
|
||||||
p.cond.L.Unlock()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Acquire gets a resource from the pool. If no resources are available and the pool
|
// Acquire gets a resource from the pool. If no resources are available and the pool
|
||||||
// is not at maximum capacity it will create a new resource. If the pool is at
|
// is not at maximum capacity it will create a new resource. If the pool is at
|
||||||
// maximum capacity it will block until a resource is available. ctx can be used
|
// maximum capacity it will block until a resource is available. ctx can be used
|
||||||
|
|||||||
+13
-16
@@ -82,7 +82,7 @@ func waitForRead(ch chan int) bool {
|
|||||||
|
|
||||||
func TestPoolAcquireCreatesResourceWhenNoneAvailable(t *testing.T) {
|
func TestPoolAcquireCreatesResourceWhenNoneAvailable(t *testing.T) {
|
||||||
createFunc, _ := createCreateResourceFunc()
|
createFunc, _ := createCreateResourceFunc()
|
||||||
pool := puddle.NewPool(createFunc, stubCloseRes)
|
pool := puddle.NewPool(createFunc, stubCloseRes, 10)
|
||||||
defer pool.Close()
|
defer pool.Close()
|
||||||
|
|
||||||
res, err := pool.Acquire(context.Background())
|
res, err := pool.Acquire(context.Background())
|
||||||
@@ -93,8 +93,7 @@ func TestPoolAcquireCreatesResourceWhenNoneAvailable(t *testing.T) {
|
|||||||
|
|
||||||
func TestPoolAcquireDoesNotCreatesResourceWhenItWouldExceedMaxSize(t *testing.T) {
|
func TestPoolAcquireDoesNotCreatesResourceWhenItWouldExceedMaxSize(t *testing.T) {
|
||||||
createFunc, createCounter := createCreateResourceFunc()
|
createFunc, createCounter := createCreateResourceFunc()
|
||||||
pool := puddle.NewPool(createFunc, stubCloseRes)
|
pool := puddle.NewPool(createFunc, stubCloseRes, 1)
|
||||||
pool.SetMaxSize(1)
|
|
||||||
|
|
||||||
wg := &sync.WaitGroup{}
|
wg := &sync.WaitGroup{}
|
||||||
|
|
||||||
@@ -119,8 +118,7 @@ func TestPoolAcquireDoesNotCreatesResourceWhenItWouldExceedMaxSize(t *testing.T)
|
|||||||
|
|
||||||
func TestPoolAcquireWithCancellableContext(t *testing.T) {
|
func TestPoolAcquireWithCancellableContext(t *testing.T) {
|
||||||
createFunc, createCounter := createCreateResourceFunc()
|
createFunc, createCounter := createCreateResourceFunc()
|
||||||
pool := puddle.NewPool(createFunc, stubCloseRes)
|
pool := puddle.NewPool(createFunc, stubCloseRes, 1)
|
||||||
pool.SetMaxSize(1)
|
|
||||||
|
|
||||||
wg := &sync.WaitGroup{}
|
wg := &sync.WaitGroup{}
|
||||||
|
|
||||||
@@ -150,7 +148,7 @@ func TestPoolAcquireReturnsErrorFromFailedResourceCreate(t *testing.T) {
|
|||||||
createFunc := func(ctx context.Context) (interface{}, error) {
|
createFunc := func(ctx context.Context) (interface{}, error) {
|
||||||
return nil, errCreateFailed
|
return nil, errCreateFailed
|
||||||
}
|
}
|
||||||
pool := puddle.NewPool(createFunc, stubCloseRes)
|
pool := puddle.NewPool(createFunc, stubCloseRes, 10)
|
||||||
|
|
||||||
res, err := pool.Acquire(context.Background())
|
res, err := pool.Acquire(context.Background())
|
||||||
assert.Equal(t, errCreateFailed, err)
|
assert.Equal(t, errCreateFailed, err)
|
||||||
@@ -159,7 +157,7 @@ func TestPoolAcquireReturnsErrorFromFailedResourceCreate(t *testing.T) {
|
|||||||
|
|
||||||
func TestPoolAcquireReusesResources(t *testing.T) {
|
func TestPoolAcquireReusesResources(t *testing.T) {
|
||||||
createFunc, createCounter := createCreateResourceFunc()
|
createFunc, createCounter := createCreateResourceFunc()
|
||||||
pool := puddle.NewPool(createFunc, stubCloseRes)
|
pool := puddle.NewPool(createFunc, stubCloseRes, 10)
|
||||||
|
|
||||||
res, err := pool.Acquire(context.Background())
|
res, err := pool.Acquire(context.Background())
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
@@ -180,7 +178,7 @@ func TestPoolAcquireContextAlreadyCanceled(t *testing.T) {
|
|||||||
createFunc := func(ctx context.Context) (interface{}, error) {
|
createFunc := func(ctx context.Context) (interface{}, error) {
|
||||||
panic("should never be called")
|
panic("should never be called")
|
||||||
}
|
}
|
||||||
pool := puddle.NewPool(createFunc, stubCloseRes)
|
pool := puddle.NewPool(createFunc, stubCloseRes, 10)
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
cancel()
|
cancel()
|
||||||
@@ -203,7 +201,7 @@ func TestPoolAcquireContextCanceledDuringCreate(t *testing.T) {
|
|||||||
}
|
}
|
||||||
return createCalls.Next(), nil
|
return createCalls.Next(), nil
|
||||||
}
|
}
|
||||||
pool := puddle.NewPool(createFunc, stubCloseRes)
|
pool := puddle.NewPool(createFunc, stubCloseRes, 10)
|
||||||
|
|
||||||
res, err := pool.Acquire(ctx)
|
res, err := pool.Acquire(ctx)
|
||||||
assert.Equal(t, context.Canceled, err)
|
assert.Equal(t, context.Canceled, err)
|
||||||
@@ -218,7 +216,7 @@ func TestPoolCloseClosesAllAvailableResources(t *testing.T) {
|
|||||||
closeCalls.Next()
|
closeCalls.Next()
|
||||||
}
|
}
|
||||||
|
|
||||||
p := puddle.NewPool(createFunc, closeFunc)
|
p := puddle.NewPool(createFunc, closeFunc, 10)
|
||||||
|
|
||||||
resources := make([]*puddle.Resource, 4)
|
resources := make([]*puddle.Resource, 4)
|
||||||
for i := range resources {
|
for i := range resources {
|
||||||
@@ -243,7 +241,7 @@ func TestPoolCloseBlocksUntilAllResourcesReleasedAndClosed(t *testing.T) {
|
|||||||
closeCalls.Next()
|
closeCalls.Next()
|
||||||
}
|
}
|
||||||
|
|
||||||
p := puddle.NewPool(createFunc, closeFunc)
|
p := puddle.NewPool(createFunc, closeFunc, 10)
|
||||||
|
|
||||||
resources := make([]*puddle.Resource, 4)
|
resources := make([]*puddle.Resource, 4)
|
||||||
for i := range resources {
|
for i := range resources {
|
||||||
@@ -270,7 +268,7 @@ func TestResourceDestroyRemovesResourceFromPool(t *testing.T) {
|
|||||||
closeCalls.Next()
|
closeCalls.Next()
|
||||||
}
|
}
|
||||||
|
|
||||||
pool := puddle.NewPool(createFunc, closeFunc)
|
pool := puddle.NewPool(createFunc, closeFunc, 10)
|
||||||
|
|
||||||
res, err := pool.Acquire(context.Background())
|
res, err := pool.Acquire(context.Background())
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
@@ -284,7 +282,7 @@ func TestResourceDestroyRemovesResourceFromPool(t *testing.T) {
|
|||||||
|
|
||||||
func TestResourceHijackRemovesResourceFromPoolButDoesNotDestroy(t *testing.T) {
|
func TestResourceHijackRemovesResourceFromPoolButDoesNotDestroy(t *testing.T) {
|
||||||
createFunc, _ := createCreateResourceFunc()
|
createFunc, _ := createCreateResourceFunc()
|
||||||
pool := puddle.NewPool(createFunc, stubCloseRes)
|
pool := puddle.NewPool(createFunc, stubCloseRes, 10)
|
||||||
|
|
||||||
res, err := pool.Acquire(context.Background())
|
res, err := pool.Acquire(context.Background())
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
@@ -297,7 +295,7 @@ func TestResourceHijackRemovesResourceFromPoolButDoesNotDestroy(t *testing.T) {
|
|||||||
|
|
||||||
func TestPoolAcquireReturnsErrorWhenPoolIsClosed(t *testing.T) {
|
func TestPoolAcquireReturnsErrorWhenPoolIsClosed(t *testing.T) {
|
||||||
createFunc, _ := createCreateResourceFunc()
|
createFunc, _ := createCreateResourceFunc()
|
||||||
pool := puddle.NewPool(createFunc, stubCloseRes)
|
pool := puddle.NewPool(createFunc, stubCloseRes, 10)
|
||||||
pool.Close()
|
pool.Close()
|
||||||
|
|
||||||
res, err := pool.Acquire(context.Background())
|
res, err := pool.Acquire(context.Background())
|
||||||
@@ -373,8 +371,7 @@ func BenchmarkPoolAcquireAndRelease(b *testing.B) {
|
|||||||
wg := &sync.WaitGroup{}
|
wg := &sync.WaitGroup{}
|
||||||
|
|
||||||
createFunc, _ := createCreateResourceFunc()
|
createFunc, _ := createCreateResourceFunc()
|
||||||
pool := puddle.NewPool(createFunc, stubCloseRes)
|
pool := puddle.NewPool(createFunc, stubCloseRes, bm.poolSize)
|
||||||
pool.SetMaxSize(bm.poolSize)
|
|
||||||
|
|
||||||
for i := 0; i < bm.clientCount; i++ {
|
for i := 0; i < bm.clientCount; i++ {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
|
|||||||
Reference in New Issue
Block a user