2
0

CreateResource constructs a new resource without acquiring it.

This commit is contained in:
Patrick Ellul
2020-02-03 11:14:49 +11:00
parent 807afe48a8
commit 4014e4825a
2 changed files with 64 additions and 0 deletions
+26
View File
@@ -373,6 +373,32 @@ func (p *Pool) AcquireAllIdle() []*Resource {
return resources
}
// CreateResource constructs a new resource without acquiring it.
// It goes straight in the IdlePool. It does not check against maxSize.
// It can be useful to maintain warm resources under little load.
func (p *Pool) CreateResource(ctx context.Context) error {
value, err := p.constructResourceValue(ctx)
if err != nil {
return err
}
res := &Resource{
pool: p,
creationTime: time.Now(),
status: resourceStatusIdle,
value: value,
}
p.cond.L.Lock()
p.allResources = append(p.allResources, res)
p.idleResources = append(p.idleResources, res)
p.destructWG.Add(1)
p.cond.L.Unlock()
return nil
}
// releaseAcquiredResource returns res to the the pool.
func (p *Pool) releaseAcquiredResource(res *Resource, lastUsedNano int64) {
p.cond.L.Lock()