2
0

Add *Pool.Remove(res)

This commit is contained in:
Jack Christensen
2018-12-23 16:40:06 -06:00
parent bc74a79c98
commit 778ac737e5
3 changed files with 187 additions and 1 deletions
+32
View File
@@ -305,3 +305,35 @@ func (p *Pool) Return(res interface{}) {
p.cond.L.Unlock()
p.cond.Signal()
}
// Remove removes res from the pool and closes it. If res is not part of the
// pool Remove will panic.
func (p *Pool) Remove(res interface{}) {
p.cond.L.Lock()
defer p.cond.L.Unlock()
rw, present := p.allResources[res]
if !present {
panic("Remove called on resource that does not belong to pool")
}
delete(p.allResources, rw.resource)
// close the resource in the background
go func() {
err := p.closeRes(res)
if err != nil {
p.cond.L.Lock()
p.backgroundErrorHandler(err)
p.cond.L.Unlock()
}
}()
// Maintain min pool size (unless pool is already closed)
if !p.closed {
for len(p.allResources) < p.minSize {
createResChan, createErrChan := p.startCreate()
p.backgroundFinishCreate(createResChan, createErrChan)
}
}
}