Name project "Puddle"
pool was inconvenient as a name because the obvious name for a variable is pool. But that caused a name collision with the package.
This commit is contained in:
@@ -1,10 +1,9 @@
|
|||||||
# Pool
|
# Puddle
|
||||||
|
|
||||||
is a generic resource pool.
|
Puddle is a generic resource pool library for Go.
|
||||||
|
|
||||||
## TODO
|
## TODO
|
||||||
|
|
||||||
* Permanent name
|
|
||||||
* Error reporting for async errors
|
* Error reporting for async errors
|
||||||
* Min pool size
|
* Min pool size
|
||||||
* Max resource lifetime
|
* Max resource lifetime
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package pool
|
package puddle
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@@ -39,7 +39,7 @@ type Pool struct {
|
|||||||
closeRes CloseFunc
|
closeRes CloseFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(create CreateFunc, closeRes CloseFunc) *Pool {
|
func NewPool(create CreateFunc, closeRes CloseFunc) *Pool {
|
||||||
return &Pool{
|
return &Pool{
|
||||||
cond: sync.NewCond(new(sync.Mutex)),
|
cond: sync.NewCond(new(sync.Mutex)),
|
||||||
allResources: make(map[interface{}]*resourceWrapper),
|
allResources: make(map[interface{}]*resourceWrapper),
|
||||||
|
|||||||
+17
-17
@@ -1,4 +1,4 @@
|
|||||||
package pool_test
|
package puddle_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/jackc/pool"
|
"github.com/jackc/puddle"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
@@ -41,7 +41,7 @@ func TestPoolGetCreatesResourceWhenNoneAvailable(t *testing.T) {
|
|||||||
createFunc := func() (interface{}, error) {
|
createFunc := func() (interface{}, error) {
|
||||||
return createCalls.Next(), nil
|
return createCalls.Next(), nil
|
||||||
}
|
}
|
||||||
pool := pool.New(createFunc, stubCloseRes)
|
pool := puddle.NewPool(createFunc, stubCloseRes)
|
||||||
|
|
||||||
res, err := pool.Get(context.Background())
|
res, err := pool.Get(context.Background())
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
@@ -55,7 +55,7 @@ func TestPoolGetDoesNotCreatesResourceWhenItWouldExceedMaxSize(t *testing.T) {
|
|||||||
createFunc := func() (interface{}, error) {
|
createFunc := func() (interface{}, error) {
|
||||||
return createCalls.Next(), nil
|
return createCalls.Next(), nil
|
||||||
}
|
}
|
||||||
pool := pool.New(createFunc, stubCloseRes)
|
pool := puddle.NewPool(createFunc, stubCloseRes)
|
||||||
pool.SetMaxSize(1)
|
pool.SetMaxSize(1)
|
||||||
|
|
||||||
wg := &sync.WaitGroup{}
|
wg := &sync.WaitGroup{}
|
||||||
@@ -84,7 +84,7 @@ func TestPoolGetReturnsErrorFromFailedResourceCreate(t *testing.T) {
|
|||||||
createFunc := func() (interface{}, error) {
|
createFunc := func() (interface{}, error) {
|
||||||
return nil, errCreateFailed
|
return nil, errCreateFailed
|
||||||
}
|
}
|
||||||
pool := pool.New(createFunc, stubCloseRes)
|
pool := puddle.NewPool(createFunc, stubCloseRes)
|
||||||
|
|
||||||
res, err := pool.Get(context.Background())
|
res, err := pool.Get(context.Background())
|
||||||
assert.Equal(t, errCreateFailed, err)
|
assert.Equal(t, errCreateFailed, err)
|
||||||
@@ -96,7 +96,7 @@ func TestPoolGetReusesResources(t *testing.T) {
|
|||||||
createFunc := func() (interface{}, error) {
|
createFunc := func() (interface{}, error) {
|
||||||
return createCalls.Next(), nil
|
return createCalls.Next(), nil
|
||||||
}
|
}
|
||||||
pool := pool.New(createFunc, stubCloseRes)
|
pool := puddle.NewPool(createFunc, stubCloseRes)
|
||||||
|
|
||||||
res, err := pool.Get(context.Background())
|
res, err := pool.Get(context.Background())
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
@@ -117,7 +117,7 @@ func TestPoolGetContextAlreadyCanceled(t *testing.T) {
|
|||||||
createFunc := func() (interface{}, error) {
|
createFunc := func() (interface{}, error) {
|
||||||
panic("should never be called")
|
panic("should never be called")
|
||||||
}
|
}
|
||||||
pool := pool.New(createFunc, stubCloseRes)
|
pool := puddle.NewPool(createFunc, stubCloseRes)
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
cancel()
|
cancel()
|
||||||
@@ -135,7 +135,7 @@ func TestPoolGetContextCanceledDuringCreate(t *testing.T) {
|
|||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
return createCalls.Next(), nil
|
return createCalls.Next(), nil
|
||||||
}
|
}
|
||||||
pool := pool.New(createFunc, stubCloseRes)
|
pool := puddle.NewPool(createFunc, stubCloseRes)
|
||||||
|
|
||||||
res, err := pool.Get(ctx)
|
res, err := pool.Get(ctx)
|
||||||
assert.Equal(t, context.Canceled, err)
|
assert.Equal(t, context.Canceled, err)
|
||||||
@@ -147,7 +147,7 @@ func TestPoolReturnPanicsIfResourceNotPartOfPool(t *testing.T) {
|
|||||||
createFunc := func() (interface{}, error) {
|
createFunc := func() (interface{}, error) {
|
||||||
return createCalls.Next(), nil
|
return createCalls.Next(), nil
|
||||||
}
|
}
|
||||||
pool := pool.New(createFunc, stubCloseRes)
|
pool := puddle.NewPool(createFunc, stubCloseRes)
|
||||||
|
|
||||||
assert.Panics(t, func() { pool.Return(42) })
|
assert.Panics(t, func() { pool.Return(42) })
|
||||||
}
|
}
|
||||||
@@ -164,7 +164,7 @@ func TestPoolCloseClosesAllAvailableResources(t *testing.T) {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
p := pool.New(createFunc, closeFunc)
|
p := puddle.NewPool(createFunc, closeFunc)
|
||||||
|
|
||||||
resources := make([]interface{}, 4)
|
resources := make([]interface{}, 4)
|
||||||
for i := range resources {
|
for i := range resources {
|
||||||
@@ -194,7 +194,7 @@ func TestPoolReturnClosesResourcePoolIsAlreadyClosed(t *testing.T) {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
p := pool.New(createFunc, closeFunc)
|
p := puddle.NewPool(createFunc, closeFunc)
|
||||||
|
|
||||||
resources := make([]interface{}, 4)
|
resources := make([]interface{}, 4)
|
||||||
for i := range resources {
|
for i := range resources {
|
||||||
@@ -218,11 +218,11 @@ func TestPoolGetReturnsErrorWhenPoolIsClosed(t *testing.T) {
|
|||||||
createFunc := func() (interface{}, error) {
|
createFunc := func() (interface{}, error) {
|
||||||
return createCalls.Next(), nil
|
return createCalls.Next(), nil
|
||||||
}
|
}
|
||||||
p := pool.New(createFunc, stubCloseRes)
|
pool := puddle.NewPool(createFunc, stubCloseRes)
|
||||||
p.Close()
|
pool.Close()
|
||||||
|
|
||||||
res, err := p.Get(context.Background())
|
res, err := pool.Get(context.Background())
|
||||||
assert.Equal(t, pool.ErrClosedPool, err)
|
assert.Equal(t, puddle.ErrClosedPool, err)
|
||||||
assert.Nil(t, res)
|
assert.Nil(t, res)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -231,7 +231,7 @@ func BenchmarkPoolGetAndReturnNoContention(b *testing.B) {
|
|||||||
createFunc := func() (interface{}, error) {
|
createFunc := func() (interface{}, error) {
|
||||||
return createCalls.Next(), nil
|
return createCalls.Next(), nil
|
||||||
}
|
}
|
||||||
pool := pool.New(createFunc, stubCloseRes)
|
pool := puddle.NewPool(createFunc, stubCloseRes)
|
||||||
|
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
res, err := pool.Get(context.Background())
|
res, err := pool.Get(context.Background())
|
||||||
@@ -250,7 +250,7 @@ func BenchmarkPoolGetAndReturnHeavyContention(b *testing.B) {
|
|||||||
createFunc := func() (interface{}, error) {
|
createFunc := func() (interface{}, error) {
|
||||||
return createCalls.Next(), nil
|
return createCalls.Next(), nil
|
||||||
}
|
}
|
||||||
pool := pool.New(createFunc, stubCloseRes)
|
pool := puddle.NewPool(createFunc, stubCloseRes)
|
||||||
pool.SetMaxSize(poolSize)
|
pool.SetMaxSize(poolSize)
|
||||||
|
|
||||||
doneChan := make(chan struct{})
|
doneChan := make(chan struct{})
|
||||||
|
|||||||
Reference in New Issue
Block a user