1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//! Bulkhead — bounded concurrency per endpoint group.
//!
//! The circuit breaker protects against a dependency that *fails*; the
//! bulkhead protects against one that is merely *slow*. Without it, a single
//! laggy downstream lets in-flight requests pile up until every worker on the
//! pod is parked on that dependency — the classic slow-loris cascade that a
//! breaker never trips on (nothing is erroring, everything is just waiting).
//!
//! `tokio::sync::Semaphore` is a lock-free waiter queue, and `try_acquire`
//! never waits at all — saturation answers `503` immediately so the caller
//! retries on a healthier replica, instead of queueing latency on this one.
//!
//! ```ignore
//! static PAYMENT: Bulkhead = Bulkhead::new("payment", 32);
//!
//! async fn create_order(ctx: RequestContext /* ... */) -> Result<Json<Value>, HttpException> {
//! let _permit = PAYMENT.try_enter()?; // 503 when saturated
//! payment.charge(/* ... */).await // permit released on drop
//! }
//! ```
use ;
use crateError;