CreateResource cannot overflow pool
This commit is contained in:
@@ -588,42 +588,48 @@ func (p *Pool[T]) AcquireAllIdle() []*Resource[T] {
|
|||||||
return idle
|
return idle
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateResource constructs a new resource without acquiring it.
|
// CreateResource constructs a new resource without acquiring it. It goes straight in the IdlePool. If the pool is full
|
||||||
// It goes straight in the IdlePool. It does not check against maxSize.
|
// it returns an error. It can be useful to maintain warm resources under little load.
|
||||||
// It can be useful to maintain warm resources under little load.
|
|
||||||
func (p *Pool[T]) CreateResource(ctx context.Context) error {
|
func (p *Pool[T]) CreateResource(ctx context.Context) error {
|
||||||
|
if !p.acquireSem.TryAcquire(1) {
|
||||||
|
return ErrNotAvailable
|
||||||
|
}
|
||||||
|
|
||||||
p.mux.Lock()
|
p.mux.Lock()
|
||||||
if p.closed {
|
if p.closed {
|
||||||
|
p.acquireSem.Release(1)
|
||||||
p.mux.Unlock()
|
p.mux.Unlock()
|
||||||
return ErrClosedPool
|
return ErrClosedPool
|
||||||
}
|
}
|
||||||
p.destructWG.Add(1)
|
|
||||||
|
if len(p.allResources) >= int(p.maxSize) {
|
||||||
|
p.acquireSem.Release(1)
|
||||||
|
p.mux.Unlock()
|
||||||
|
return ErrNotAvailable
|
||||||
|
}
|
||||||
|
|
||||||
|
res := p.createNewResource()
|
||||||
p.mux.Unlock()
|
p.mux.Unlock()
|
||||||
|
|
||||||
value, err := p.constructor(ctx)
|
value, err := p.constructor(ctx)
|
||||||
|
p.mux.Lock()
|
||||||
|
defer p.mux.Unlock()
|
||||||
|
defer p.acquireSem.Release(1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
p.allResources.remove(res)
|
||||||
p.destructWG.Done()
|
p.destructWG.Done()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
res := &Resource[T]{
|
res.value = value
|
||||||
pool: p,
|
res.status = resourceStatusIdle
|
||||||
creationTime: time.Now(),
|
|
||||||
status: resourceStatusIdle,
|
|
||||||
value: value,
|
|
||||||
lastUsedNano: nanotime(),
|
|
||||||
poolResetCount: p.resetCount,
|
|
||||||
}
|
|
||||||
|
|
||||||
p.mux.Lock()
|
|
||||||
defer p.mux.Unlock()
|
|
||||||
|
|
||||||
// If closed while constructing resource then destroy it and return an error
|
// If closed while constructing resource then destroy it and return an error
|
||||||
if p.closed {
|
if p.closed {
|
||||||
go p.destructResourceValue(res.value)
|
go p.destructResourceValue(res.value)
|
||||||
return ErrClosedPool
|
return ErrClosedPool
|
||||||
}
|
}
|
||||||
p.allResources.append(res)
|
|
||||||
p.idleResources.Push(res)
|
p.idleResources.Push(res)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -448,6 +448,28 @@ func TestPoolCreateResourceReturnsErrorWhenClosedWhileCreatingResource(t *testin
|
|||||||
assert.Equal(t, puddle.ErrClosedPool, err)
|
assert.Equal(t, puddle.ErrClosedPool, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPoolCreateResourceReturnsErrorWhenPoolFull(t *testing.T) {
|
||||||
|
constructor, _ := createConstructor()
|
||||||
|
pool, err := puddle.NewPool(&puddle.Config[int]{Constructor: constructor, Destructor: stubDestructor, MaxSize: 2})
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer pool.Close()
|
||||||
|
|
||||||
|
err = pool.CreateResource(context.Background())
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
stats := pool.Stat()
|
||||||
|
assert.EqualValues(t, 1, stats.IdleResources())
|
||||||
|
|
||||||
|
err = pool.CreateResource(context.Background())
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
stats = pool.Stat()
|
||||||
|
assert.EqualValues(t, 2, stats.IdleResources())
|
||||||
|
|
||||||
|
err = pool.CreateResource(context.Background())
|
||||||
|
require.Error(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
func TestPoolCloseClosesAllIdleResources(t *testing.T) {
|
func TestPoolCloseClosesAllIdleResources(t *testing.T) {
|
||||||
constructor, _ := createConstructor()
|
constructor, _ := createConstructor()
|
||||||
|
|
||||||
@@ -990,6 +1012,13 @@ func TestStress(t *testing.T) {
|
|||||||
stat := pool.Stat()
|
stat := pool.Stat()
|
||||||
assert.NotNil(t, stat)
|
assert.NotNil(t, stat)
|
||||||
},
|
},
|
||||||
|
// CreateResource
|
||||||
|
func() {
|
||||||
|
err := pool.CreateResource(context.Background())
|
||||||
|
if err != nil && !errors.Is(err, puddle.ErrClosedPool) && !errors.Is(err, puddle.ErrNotAvailable) {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
workerCount := int(poolSize) * 2
|
workerCount := int(poolSize) * 2
|
||||||
|
|||||||
Reference in New Issue
Block a user