Maybe conditionally executes thunk based on the Breaker concurrency and queue parameters. If the concurrency limit and queue capacity are already consumed, Maybe returns immediately without calling thunk. If the thunk was executed, Maybe returns true, else false.
(ctx context.Context, thunk func())
| 132 | // already consumed, Maybe returns immediately without calling thunk. If |
| 133 | // the thunk was executed, Maybe returns true, else false. |
| 134 | func (b *Breaker) Maybe(ctx context.Context, thunk func()) error { |
| 135 | if !b.tryAcquirePending() { |
| 136 | return ErrRequestQueueFull |
| 137 | } |
| 138 | |
| 139 | defer b.releasePending() |
| 140 | |
| 141 | // Wait for capacity in the active queue. |
| 142 | if err := b.sem.acquire(ctx); err != nil { |
| 143 | return err |
| 144 | } |
| 145 | // Defer releasing capacity in the active. |
| 146 | // It's safe to ignore the error returned by release since we |
| 147 | // make sure the semaphore is only manipulated here and acquire |
| 148 | // + release calls are equally paired. |
| 149 | defer b.sem.release() |
| 150 | |
| 151 | // Do the thing. |
| 152 | thunk() |
| 153 | // Report success |
| 154 | return nil |
| 155 | } |
| 156 | |
| 157 | // InFlight returns the number of requests currently in flight in this breaker. |
| 158 | func (b *Breaker) InFlight() int64 { |