tryAcquirePending tries to acquire a slot on the pending "queue".
()
| 81 | |
| 82 | // tryAcquirePending tries to acquire a slot on the pending "queue". |
| 83 | func (b *Breaker) tryAcquirePending() bool { |
| 84 | // This is an atomic version of: |
| 85 | // |
| 86 | // if inFlight == totalSlots { |
| 87 | // return false |
| 88 | // } else { |
| 89 | // inFlight++ |
| 90 | // return true |
| 91 | // } |
| 92 | // |
| 93 | // We can't just use an atomic increment as we need to check if we're |
| 94 | // "allowed" to increment first. Since a Load and a CompareAndSwap are |
| 95 | // not done atomically, we need to retry until the CompareAndSwap succeeds |
| 96 | // (it fails if we're raced to it) or if we don't fulfill the condition |
| 97 | // anymore. |
| 98 | for { |
| 99 | cur := b.inFlight.Load() |
| 100 | if cur == b.totalSlots { |
| 101 | return false |
| 102 | } |
| 103 | if b.inFlight.CAS(cur, cur+1) { |
| 104 | return true |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | // releasePending releases a slot on the pending "queue". |
| 110 | func (b *Breaker) releasePending() { |