Call invokes the callback with a resource from the pool.
(callback func(Src) error)
| 66 | |
| 67 | // Call invokes the callback with a resource from the pool. |
| 68 | func (c *classic) Call(callback func(Src) error) result.VoidResult { |
| 69 | var src Src |
| 70 | for { |
| 71 | c.RLock() |
| 72 | if c.closed { |
| 73 | c.RUnlock() |
| 74 | return result.TryErrVoid(closedError) |
| 75 | } |
| 76 | select { |
| 77 | case src = <-c.srcs: |
| 78 | c.RUnlock() |
| 79 | if !src.Usable() { |
| 80 | c.del(src) |
| 81 | continue |
| 82 | } |
| 83 | default: |
| 84 | c.RUnlock() |
| 85 | err := c.incAuto() |
| 86 | if err != nil { |
| 87 | return result.TryErrVoid(err) |
| 88 | } |
| 89 | runtime.Gosched() |
| 90 | continue |
| 91 | } |
| 92 | break |
| 93 | } |
| 94 | defer func() { |
| 95 | if p := recover(); p != nil { |
| 96 | _ = fmt.Errorf("%v", p) |
| 97 | } |
| 98 | c.recover(src) |
| 99 | }() |
| 100 | return result.RetVoid(callback(src)) |
| 101 | } |
| 102 | |
| 103 | // Close destroys the pool and releases all resources. |
| 104 | func (c *classic) Close() { |