MCPcopy
hub / github.com/cortexlabs/cortex / Maybe

Method Maybe

pkg/proxy/breaker.go:134–155  ·  view source on GitHub ↗

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())

Source from the content-addressed store, hash-verified

132// already consumed, Maybe returns immediately without calling thunk. If
133// the thunk was executed, Maybe returns true, else false.
134func (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.
158func (b *Breaker) InFlight() int64 {

Callers 4

BenchmarkBreakerMaybeFunction · 0.95
HandlerFunction · 0.80
requestWithContextMethod · 0.80
tryMethod · 0.80

Calls 4

tryAcquirePendingMethod · 0.95
releasePendingMethod · 0.95
acquireMethod · 0.80
releaseMethod · 0.80

Tested by 2

BenchmarkBreakerMaybeFunction · 0.76
requestWithContextMethod · 0.64