Key Components
struct AppState {
downstream_url: String,
circuit: Arc<CircuitBreaker>,
bulkhead: Arc<Semaphore>,
fallback_cache: Arc<RwLock<Option<CachedResponse>>>,
metrics: Arc<Metrics>,
}
async fn proxy_handler(State(state): State<AppState>) -> impl IntoResponse {
let _permit = state.bulkhead
.try_acquire()
.map_err(|_| StatusCode::SERVICE_UNAVAILABLE)?;
state.circuit.call(|| fetch_downstream(&state)).await
.unwrap_or_else(|_| state.fallback_cache.read().unwrap().clone().into_response())
}