pub fn increment_breaker(
key: &str,
delta: BreakerDelta,
window: i64,
) -> Result<Breaker, RemoteStateError>
Expand description
Increments a circuit breaker, returning the generation count, success count, failure count, consecutive success count, consecutive failure count, and expiration time.
The plugin is responsible for performing all circuit-breaking logic with the counter values it receives. The host environment does as little as possible to maximize how much control the plugin has over the behavior of the breaker.
In order for this function to succeed, a plugin’s configuration must explicitly declare a permission grant for the prefix of the key being requested. This function will panic if permission has not been granted.
§Arguments
key
- The key name corresponding to the state counter.delta
- The amount to increase the success or failure counter by.window
- How long each period should be in seconds.
§Examples
ⓘ
use bulwark_wasm_sdk::*;
struct CircuitBreaker;
#[bulwark_plugin]
impl Handlers for CircuitBreaker {
fn on_response_decision() -> Result {
if let Some(ip) = get_client_ip() {
let key = format!("client.ip:{ip}");
// "failure" could be determined by other methods besides status code
let failure = get_response().map(|r| r.status().as_u16() >= 500).unwrap_or(true);
let breaker = increment_breaker(
&key,
if !failure {
BreakerDelta::Success(1)
} else {
BreakerDelta::Failure(1)
},
60 * 60, // 1 hour
)?;
// use breaker here
}
Ok(())
}
}