Rename max resource uses to max resource checkouts
This commit is contained in:
@@ -5,7 +5,6 @@ Puddle is a generic resource pool library for Go.
|
|||||||
## TODO
|
## TODO
|
||||||
|
|
||||||
* Max resource lifetime
|
* Max resource lifetime
|
||||||
* Max resource uses
|
|
||||||
* Max resource idle time
|
* Max resource idle time
|
||||||
* Resource keep alive
|
* Resource keep alive
|
||||||
* Resource health check - keep alive and health check might be same thing
|
* Resource health check - keep alive and health check might be same thing
|
||||||
|
|||||||
@@ -39,13 +39,13 @@ type resourceWrapper struct {
|
|||||||
type Pool struct {
|
type Pool struct {
|
||||||
cond *sync.Cond
|
cond *sync.Cond
|
||||||
|
|
||||||
allResources map[interface{}]*resourceWrapper
|
allResources map[interface{}]*resourceWrapper
|
||||||
availableResources []*resourceWrapper
|
availableResources []*resourceWrapper
|
||||||
minSize int
|
minSize int
|
||||||
maxSize int
|
maxSize int
|
||||||
maxResourceDuration time.Duration
|
maxResourceDuration time.Duration
|
||||||
maxResourceUses uint64
|
maxResourceCheckouts uint64
|
||||||
closed bool
|
closed bool
|
||||||
|
|
||||||
createRes CreateFunc
|
createRes CreateFunc
|
||||||
closeRes CloseFunc
|
closeRes CloseFunc
|
||||||
@@ -58,7 +58,7 @@ func NewPool(createRes CreateFunc, closeRes CloseFunc) *Pool {
|
|||||||
allResources: make(map[interface{}]*resourceWrapper),
|
allResources: make(map[interface{}]*resourceWrapper),
|
||||||
maxSize: maxInt,
|
maxSize: maxInt,
|
||||||
maxResourceDuration: math.MaxInt64,
|
maxResourceDuration: math.MaxInt64,
|
||||||
maxResourceUses: math.MaxUint64,
|
maxResourceCheckouts: math.MaxUint64,
|
||||||
createRes: createRes,
|
createRes: createRes,
|
||||||
closeRes: closeRes,
|
closeRes: closeRes,
|
||||||
backgroundErrorHandler: func(error) {},
|
backgroundErrorHandler: func(error) {},
|
||||||
@@ -150,21 +150,21 @@ func (p *Pool) SetMaxResourceDuration(d time.Duration) {
|
|||||||
p.cond.L.Unlock()
|
p.cond.L.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// MaxResourceUses returns the current maximum uses per resource of the pool.
|
// MaxResourceCheckouts returns the current maximum uses per resource of the pool.
|
||||||
func (p *Pool) MaxResourceUses() uint64 {
|
func (p *Pool) MaxResourceCheckouts() uint64 {
|
||||||
p.cond.L.Lock()
|
p.cond.L.Lock()
|
||||||
n := p.maxResourceUses
|
n := p.maxResourceCheckouts
|
||||||
p.cond.L.Unlock()
|
p.cond.L.Unlock()
|
||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetMaxResourceUses sets the maximum maximum resource duration of the pool. It panics if n < 1.
|
// SetMaxResourceCheckouts sets the maximum maximum resource duration of the pool. It panics if n < 1.
|
||||||
func (p *Pool) SetMaxResourceUses(n uint64) {
|
func (p *Pool) SetMaxResourceCheckouts(n uint64) {
|
||||||
if n < 0 {
|
if n < 0 {
|
||||||
panic("pool MaxResourceUses cannot be < 1")
|
panic("pool MaxResourceCheckouts cannot be < 1")
|
||||||
}
|
}
|
||||||
p.cond.L.Lock()
|
p.cond.L.Lock()
|
||||||
p.maxResourceUses = n
|
p.maxResourceCheckouts = n
|
||||||
p.cond.L.Unlock()
|
p.cond.L.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -352,7 +352,7 @@ func (p *Pool) Return(res interface{}) {
|
|||||||
|
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
if now.Sub(rw.creationTime) > p.maxResourceDuration {
|
if now.Sub(rw.creationTime) > p.maxResourceDuration {
|
||||||
} else if p.maxResourceUses <= rw.checkoutCount { // use <= instead of == as maxResourceUses may be lowered while pool is in use
|
} else if p.maxResourceCheckouts <= rw.checkoutCount { // use <= instead of == as maxResourceCheckouts may be lowered while pool is in use
|
||||||
} else {
|
} else {
|
||||||
closeResource = false
|
closeResource = false
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -212,12 +212,12 @@ func TestPoolReturnClosesAndRemovesResourceIfOlderThanMaxDuration(t *testing.T)
|
|||||||
assert.Equal(t, 1, closeCalls.Value())
|
assert.Equal(t, 1, closeCalls.Value())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPoolReturnClosesAndRemovesResourceIfMoreUsesThanMaxResourceUses(t *testing.T) {
|
func TestPoolReturnClosesAndRemovesResourceWhenResourceCheckoutCountIsMaxResourceCheckouts(t *testing.T) {
|
||||||
createFunc, _ := createCreateResourceFunc()
|
createFunc, _ := createCreateResourceFunc()
|
||||||
closeFunc, closeCalls, closeCallsChan := createCloseResourceFuncWithNotifierChan()
|
closeFunc, closeCalls, closeCallsChan := createCloseResourceFuncWithNotifierChan()
|
||||||
|
|
||||||
pool := puddle.NewPool(createFunc, closeFunc)
|
pool := puddle.NewPool(createFunc, closeFunc)
|
||||||
pool.SetMaxResourceUses(1)
|
pool.SetMaxResourceCheckouts(1)
|
||||||
|
|
||||||
res, err := pool.Get(context.Background())
|
res, err := pool.Get(context.Background())
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|||||||
Reference in New Issue
Block a user