breaker_machines 0.28.0 → 0.30.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +28 -0
- data/Cargo.toml +1 -1
- data/ext/breaker_machines_native/core/CHANGELOG.md +28 -0
- data/ext/breaker_machines_native/core/Cargo.toml +2 -2
- data/ext/breaker_machines_native/core/README.md +5 -1
- data/ext/breaker_machines_native/core/src/async_circuit.rs +14 -8
- data/ext/breaker_machines_native/core/src/circuit.rs +5 -1
- data/ext/breaker_machines_native/core/src/distributed.rs +45 -13
- data/ext/breaker_machines_native/core/src/error.rs +22 -2
- data/ext/breaker_machines_native/core/src/lib.rs +1 -1
- data/ext/breaker_machines_native/core/src/policy.rs +27 -13
- data/ext/breaker_machines_native/core/src/storage.rs +25 -9
- data/ext/breaker_machines_native/ffi/Cargo.toml +1 -1
- data/ext/breaker_machines_native/ffi/src/values.rs +3 -10
- data/lib/breaker_machines/circuit.rb +5 -4
- data/lib/breaker_machines/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 7b8da21ca4815b552c345186e94fdc8b65feb3915de43a994405d56420d8dee7
|
|
4
|
+
data.tar.gz: f75ff2414747ccbb058b11b75f22f0194a98eb2ba570507ffaf541b0244389aa
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: f033ded9d6a3c1c4cd801d9247940fcabc506b324300ac7d6763ea7787b227797121c487e8047cddb6b5f563637e06fa876f5a0f52fd7b7e925688f5a32806dd
|
|
7
|
+
data.tar.gz: 9f7d3b8392767a559a1ea9e82429d257f6c4c54f242af5dde71844c0dca6917dccae3ba36e34693c3085c24d7db836d77445c6544e5280f580cdd263aba7340b
|
data/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,34 @@
|
|
|
6
6
|
|
|
7
7
|
* Restore shared circuit state after state machine initialization.
|
|
8
8
|
|
|
9
|
+
## [0.30.1](https://github.com/seuros/breaker_machines/compare/breaker_machines/v0.30.0...breaker_machines/v0.30.1) (2026-11-23)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
### Bug Fixes
|
|
13
|
+
|
|
14
|
+
* sample breaker clocks outside the circuit lock ([9f7ec88](https://github.com/seuros/breaker_machines/commit/9f7ec88da8e28e8a372e4b078c27631631b7c9d1))
|
|
15
|
+
|
|
16
|
+
## [0.30.0](https://github.com/seuros/breaker_machines/compare/breaker_machines/v0.29.0...breaker_machines/v0.30.0) (2026-11-17)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
### Features
|
|
20
|
+
|
|
21
|
+
* const Recovery::new ([c81f98f](https://github.com/seuros/breaker_machines/commit/c81f98fe44626edbdab9861617f3fa392b66eb43))
|
|
22
|
+
|
|
23
|
+
## [0.29.0](https://github.com/seuros/breaker_machines/compare/breaker_machines/v0.28.1...breaker_machines/v0.29.0) (2026-11-17)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
### Features
|
|
27
|
+
|
|
28
|
+
* const Threshold::new ([a6cbc64](https://github.com/seuros/breaker_machines/commit/a6cbc64ed42cd5c51b7f617184fffa5f0ff37e05))
|
|
29
|
+
|
|
30
|
+
## [0.28.1](https://github.com/seuros/breaker_machines/compare/breaker_machines/v0.28.0...breaker_machines/v0.28.1) (2026-11-16)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
### Bug Fixes
|
|
34
|
+
|
|
35
|
+
* give StorageError a kind so store exhaustion refuses as Refusal::Exhausted ([1c83941](https://github.com/seuros/breaker_machines/commit/1c839417f7c2c864835b4a817130224111826f7d))
|
|
36
|
+
|
|
9
37
|
## [0.28.0](https://github.com/seuros/breaker_machines/compare/breaker_machines/v0.27.0...breaker_machines/v0.28.0) (2026-11-16)
|
|
10
38
|
|
|
11
39
|
|
data/Cargo.toml
CHANGED
|
@@ -10,7 +10,7 @@ license = "MIT"
|
|
|
10
10
|
|
|
11
11
|
[workspace.dependencies]
|
|
12
12
|
breaker-machines = { path = "ext/breaker_machines_native/core" }
|
|
13
|
-
matryona-machines = { version = ">=0.
|
|
13
|
+
matryona-machines = { version = ">=0.7", default-features = false }
|
|
14
14
|
magnus = { version = "0.9.3", default-features = false }
|
|
15
15
|
# Nothing here calls rb-sys directly: this is the compiled fallback that
|
|
16
16
|
# implements the stable API for a Ruby rb-sys ships no pre-generated bindings
|
|
@@ -5,6 +5,34 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [0.27.1](https://github.com/seuros/breaker_machines/compare/breaker-machines-v0.27.0...breaker-machines-v0.27.1) (2026-11-23)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* sample breaker clocks outside the circuit lock ([9f7ec88](https://github.com/seuros/breaker_machines/commit/9f7ec88da8e28e8a372e4b078c27631631b7c9d1))
|
|
14
|
+
|
|
15
|
+
## [0.27.0](https://github.com/seuros/breaker_machines/compare/breaker-machines-v0.26.0...breaker-machines-v0.27.0) (2026-11-17)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
### Features
|
|
19
|
+
|
|
20
|
+
* const Recovery::new ([c81f98f](https://github.com/seuros/breaker_machines/commit/c81f98fe44626edbdab9861617f3fa392b66eb43))
|
|
21
|
+
|
|
22
|
+
## [0.26.0](https://github.com/seuros/breaker_machines/compare/breaker-machines-v0.25.1...breaker-machines-v0.26.0) (2026-11-17)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
### Features
|
|
26
|
+
|
|
27
|
+
* const Threshold::new ([a6cbc64](https://github.com/seuros/breaker_machines/commit/a6cbc64ed42cd5c51b7f617184fffa5f0ff37e05))
|
|
28
|
+
|
|
29
|
+
## [0.25.1](https://github.com/seuros/breaker_machines/compare/breaker-machines-v0.25.0...breaker-machines-v0.25.1) (2026-11-16)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
### Bug Fixes
|
|
33
|
+
|
|
34
|
+
* give StorageError a kind so store exhaustion refuses as Refusal::Exhausted ([1c83941](https://github.com/seuros/breaker_machines/commit/1c839417f7c2c864835b4a817130224111826f7d))
|
|
35
|
+
|
|
8
36
|
## [0.25.0](https://github.com/seuros/breaker_machines/compare/breaker-machines-v0.24.0...breaker-machines-v0.25.0) (2026-11-16)
|
|
9
37
|
|
|
10
38
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[package]
|
|
2
2
|
name = "breaker-machines"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.27.1"
|
|
4
4
|
edition.workspace = true
|
|
5
5
|
rust-version.workspace = true
|
|
6
6
|
authors = ["Abdelkader Boudih <oss@seuros.com>"]
|
|
@@ -26,7 +26,7 @@ async = ["std"]
|
|
|
26
26
|
workspace = true
|
|
27
27
|
|
|
28
28
|
[dependencies]
|
|
29
|
-
chrono-machines = { version = ">=0.
|
|
29
|
+
chrono-machines = { version = ">=0.13", default-features = false }
|
|
30
30
|
periscope-machines = ">=0.5"
|
|
31
31
|
# The runtime's lifecycle machine, driven by the core's answers.
|
|
32
32
|
state-machines = { version = ">=0.34", default-features = false, optional = true }
|
|
@@ -63,7 +63,11 @@ Identities never wrap. Every transition takes the next `u64` epoch, which
|
|
|
63
63
|
fences the tickets admitted before it; once the last is taken, a call that
|
|
64
64
|
would transition is `Refusal::Exhausted` with nothing changed, while calls
|
|
65
65
|
that would not are still honoured. `MemoryStorage` treats its circuit
|
|
66
|
-
generations and probe tokens the same way, answering a `StorageError
|
|
66
|
+
generations and probe tokens the same way, answering a `StorageError` of
|
|
67
|
+
kind `StorageErrorKind::Exhausted` (a failing backend is
|
|
68
|
+
`StorageErrorKind::Backend`). `DistributedCircuitBreaker` answers the
|
|
69
|
+
exhausted kind as `Refusal::Exhausted`, and a backend failure as
|
|
70
|
+
`CircuitError::Storage`.
|
|
67
71
|
|
|
68
72
|
## The runtime (`std`, `async`)
|
|
69
73
|
|
|
@@ -109,9 +109,10 @@ impl AsyncCircuitBreaker {
|
|
|
109
109
|
/// answer the state it left the circuit in.
|
|
110
110
|
pub fn observe<E: 'static>(&self, outcome: &Result<(), E>, took: Duration) -> State {
|
|
111
111
|
let outcome = self.shared.classify(outcome, took);
|
|
112
|
+
let sampled_at = self.shared.clock.now();
|
|
112
113
|
let (transitions, state) = {
|
|
113
114
|
let mut core = self.lock();
|
|
114
|
-
let now = core.
|
|
115
|
+
let now = core.clamp_time(sampled_at);
|
|
115
116
|
(core.observe(outcome, took, now), core.lifecycle())
|
|
116
117
|
};
|
|
117
118
|
self.shared.notify(transitions.into_iter().flatten());
|
|
@@ -126,9 +127,10 @@ impl AsyncCircuitBreaker {
|
|
|
126
127
|
/// [`CircuitError::Refused`] with [`Refusal::Exhausted`] when no
|
|
127
128
|
/// transition epoch is left; nothing changed.
|
|
128
129
|
pub fn force_open(&self) -> Result<(), CircuitError<Infallible>> {
|
|
130
|
+
let sampled_at = self.shared.clock.now();
|
|
129
131
|
let transition = {
|
|
130
132
|
let mut core = self.lock();
|
|
131
|
-
let now = core.
|
|
133
|
+
let now = core.clamp_time(sampled_at);
|
|
132
134
|
core.force_open(now)
|
|
133
135
|
}
|
|
134
136
|
.map_err(|refusal| self.shared.refused(refusal))?;
|
|
@@ -191,8 +193,9 @@ impl AsyncCircuitBreaker {
|
|
|
191
193
|
/// How long until a probe may run, while the circuit is open.
|
|
192
194
|
#[must_use]
|
|
193
195
|
pub fn retry_after(&self) -> Option<Duration> {
|
|
196
|
+
let sampled_at = self.shared.clock.now();
|
|
194
197
|
let core = self.lock();
|
|
195
|
-
core.breaker.retry_after(core.
|
|
198
|
+
core.breaker.retry_after(core.clamp_time(sampled_at))
|
|
196
199
|
}
|
|
197
200
|
|
|
198
201
|
/// A copy of the decision core, for every other query.
|
|
@@ -202,9 +205,10 @@ impl AsyncCircuitBreaker {
|
|
|
202
205
|
}
|
|
203
206
|
|
|
204
207
|
fn admit_inner(&self) -> Result<AsyncTicket<'_>, FallbackContext> {
|
|
208
|
+
let sampled_at = self.shared.clock.now();
|
|
205
209
|
let admitted = {
|
|
206
210
|
let mut core = self.lock();
|
|
207
|
-
let now = core.
|
|
211
|
+
let now = core.clamp_time(sampled_at);
|
|
208
212
|
core.admit(now)
|
|
209
213
|
.map_err(|refusal| (refusal, core.breaker.retry_after(now)))
|
|
210
214
|
};
|
|
@@ -221,8 +225,8 @@ impl AsyncCircuitBreaker {
|
|
|
221
225
|
}
|
|
222
226
|
|
|
223
227
|
fn lock(&self) -> MutexGuard<'_, Core> {
|
|
224
|
-
//
|
|
225
|
-
//
|
|
228
|
+
// Sample the configurable clock before locking, then clamp against the
|
|
229
|
+
// latest instant while holding the guard.
|
|
226
230
|
self.core.lock().unwrap_or_else(PoisonError::into_inner)
|
|
227
231
|
}
|
|
228
232
|
}
|
|
@@ -251,9 +255,10 @@ impl AsyncTicket<'_> {
|
|
|
251
255
|
let took = shared.clock.now().saturating_sub(self.ticket.admitted_at());
|
|
252
256
|
// The classifier is user code: it runs before the lock is taken.
|
|
253
257
|
let outcome = shared.classify(&result, took);
|
|
258
|
+
let sampled_at = shared.clock.now();
|
|
254
259
|
let settled = {
|
|
255
260
|
let mut core = circuit.lock();
|
|
256
|
-
let now = core.
|
|
261
|
+
let now = core.clamp_time(sampled_at);
|
|
257
262
|
core.settle(&mut self.ticket, outcome, now)
|
|
258
263
|
};
|
|
259
264
|
match settled {
|
|
@@ -282,9 +287,10 @@ impl Drop for AsyncTicket<'_> {
|
|
|
282
287
|
if self.ticket.settled().is_none() {
|
|
283
288
|
// The circuit issued this ticket and it is unsettled, so the
|
|
284
289
|
// abandon is honoured; nothing is left to report from a drop.
|
|
290
|
+
let sampled_at = self.circuit.shared.clock.now();
|
|
285
291
|
let abandoned = {
|
|
286
292
|
let mut core = self.circuit.lock();
|
|
287
|
-
let now = core.
|
|
293
|
+
let now = core.clamp_time(sampled_at);
|
|
288
294
|
core.settle(&mut self.ticket, None, now)
|
|
289
295
|
};
|
|
290
296
|
debug_assert!(
|
|
@@ -108,7 +108,11 @@ impl Core {
|
|
|
108
108
|
/// The clock's reading, never behind the latest honoured instant: a
|
|
109
109
|
/// clock that steps back reads as standing still.
|
|
110
110
|
pub fn now(&self, clock: &dyn Clock) -> Duration {
|
|
111
|
-
clock.now()
|
|
111
|
+
self.clamp_time(clock.now())
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
pub fn clamp_time(&self, sampled_at: Duration) -> Duration {
|
|
115
|
+
sampled_at.max(self.breaker.latest())
|
|
112
116
|
}
|
|
113
117
|
|
|
114
118
|
/// The lifecycle machine's state.
|
|
@@ -10,8 +10,9 @@ use crate::clock::Draws;
|
|
|
10
10
|
use crate::{
|
|
11
11
|
AsyncStorageBackend, CircuitError, CircuitSnapshot, FailurePolicy, FallbackContext, Policy,
|
|
12
12
|
ProbeDecision, ProbeLease, ProbePolicy, Refusal, State, StateTransition, StorageError,
|
|
13
|
-
StorageUpdate, StoredOutcome,
|
|
13
|
+
StorageErrorKind, StorageUpdate, StoredOutcome,
|
|
14
14
|
};
|
|
15
|
+
use core::convert::Infallible;
|
|
15
16
|
use core::future::Future;
|
|
16
17
|
use core::sync::atomic::{AtomicU32, Ordering};
|
|
17
18
|
use core::time::Duration;
|
|
@@ -81,9 +82,10 @@ impl DistributedCircuitBreaker {
|
|
|
81
82
|
/// # Errors
|
|
82
83
|
///
|
|
83
84
|
/// [`CircuitError::Refused`] when the circuit refuses the call (open,
|
|
84
|
-
/// another node holds the probe lease,
|
|
85
|
-
///
|
|
86
|
-
/// [`CircuitError::
|
|
85
|
+
/// another node holds the probe lease, this node is at its concurrency
|
|
86
|
+
/// limit, or the store has no fencing identity left:
|
|
87
|
+
/// [`Refusal::Exhausted`]), [`CircuitError::Storage`] when the store
|
|
88
|
+
/// fails, [`CircuitError::Execution`] with the operation's own error.
|
|
87
89
|
pub async fn call<F, Fut, T, E: 'static>(&self, operation: F) -> Result<T, CircuitError<E>>
|
|
88
90
|
where
|
|
89
91
|
F: FnOnce() -> Fut,
|
|
@@ -104,8 +106,14 @@ impl DistributedCircuitBreaker {
|
|
|
104
106
|
/// # Errors
|
|
105
107
|
///
|
|
106
108
|
/// [`CircuitError::Storage`] when the store fails,
|
|
109
|
+
/// [`CircuitError::Refused`] with [`Refusal::Exhausted`] when the store
|
|
110
|
+
/// cannot record the outcome because no generation is left (the
|
|
111
|
+
/// operation's result is dropped, as [`CircuitBreaker`] does),
|
|
107
112
|
/// [`CircuitError::Execution`] with the operation's own error; whatever
|
|
108
|
-
/// `fallback` answers when the call is refused
|
|
113
|
+
/// `fallback` answers when the call is refused, including when electing
|
|
114
|
+
/// a probe finds no generation or probe token left.
|
|
115
|
+
///
|
|
116
|
+
/// [`CircuitBreaker`]: crate::CircuitBreaker
|
|
109
117
|
pub async fn call_with_fallback<F, Fut, G, FutG, T, E: 'static>(
|
|
110
118
|
&self,
|
|
111
119
|
operation: F,
|
|
@@ -117,13 +125,22 @@ impl DistributedCircuitBreaker {
|
|
|
117
125
|
G: FnOnce(FallbackContext) -> FutG,
|
|
118
126
|
FutG: Future<Output = Result<T, CircuitError<E>>>,
|
|
119
127
|
{
|
|
120
|
-
let (slot, generation, probe) = match self.prepare().await
|
|
121
|
-
Gate::Execute {
|
|
128
|
+
let (slot, generation, probe) = match self.prepare().await {
|
|
129
|
+
Ok(Gate::Execute {
|
|
122
130
|
slot,
|
|
123
131
|
generation,
|
|
124
132
|
probe,
|
|
125
|
-
} => (slot, generation, probe),
|
|
126
|
-
Gate::Refused(context) => return fallback(context).await,
|
|
133
|
+
}) => (slot, generation, probe),
|
|
134
|
+
Ok(Gate::Refused(context)) => return fallback(context).await,
|
|
135
|
+
// No generation or probe token left to admit with: refused like
|
|
136
|
+
// an exhausted admission, nothing changed.
|
|
137
|
+
Err(error) => match error.kind() {
|
|
138
|
+
StorageErrorKind::Exhausted => {
|
|
139
|
+
let context = self.shared.fallback_context(Refusal::Exhausted, None);
|
|
140
|
+
return fallback(context).await;
|
|
141
|
+
}
|
|
142
|
+
StorageErrorKind::Backend => return Err(CircuitError::Storage(error)),
|
|
143
|
+
},
|
|
127
144
|
};
|
|
128
145
|
let start = self.shared.clock.now();
|
|
129
146
|
let result = operation().await;
|
|
@@ -159,7 +176,7 @@ impl DistributedCircuitBreaker {
|
|
|
159
176
|
.map(Some)
|
|
160
177
|
}
|
|
161
178
|
}
|
|
162
|
-
.map_err(
|
|
179
|
+
.map_err(|error| self.storage_failed(error))?;
|
|
163
180
|
if let Some(StorageUpdate {
|
|
164
181
|
transition: Some(transition),
|
|
165
182
|
..
|
|
@@ -192,9 +209,24 @@ impl DistributedCircuitBreaker {
|
|
|
192
209
|
///
|
|
193
210
|
/// # Errors
|
|
194
211
|
///
|
|
195
|
-
///
|
|
196
|
-
|
|
197
|
-
|
|
212
|
+
/// [`CircuitError::Refused`] with [`Refusal::Exhausted`] when no
|
|
213
|
+
/// generation is left; nothing changed. [`CircuitError::Storage`] when
|
|
214
|
+
/// the store fails.
|
|
215
|
+
pub async fn reset(&self) -> Result<CircuitSnapshot, CircuitError<Infallible>> {
|
|
216
|
+
self.storage
|
|
217
|
+
.reset(&self.shared.name)
|
|
218
|
+
.await
|
|
219
|
+
.map_err(|error| self.storage_failed(error))
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/// A store's failure as the circuit answers it: an exhausted fencing
|
|
223
|
+
/// identity is the core's [`Refusal::Exhausted`], anything else a
|
|
224
|
+
/// storage failure.
|
|
225
|
+
fn storage_failed<E>(&self, error: StorageError) -> CircuitError<E> {
|
|
226
|
+
match error.kind() {
|
|
227
|
+
StorageErrorKind::Exhausted => self.shared.refused(Refusal::Exhausted),
|
|
228
|
+
StorageErrorKind::Backend => CircuitError::Storage(error),
|
|
229
|
+
}
|
|
198
230
|
}
|
|
199
231
|
|
|
200
232
|
async fn prepare(&self) -> Result<Gate<'_>, StorageError> {
|
|
@@ -57,20 +57,40 @@ pub struct FallbackContext {
|
|
|
57
57
|
pub retry_after: Option<Duration>,
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
+
/// What kind of failure a [`StorageError`] is.
|
|
61
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
62
|
+
pub enum StorageErrorKind {
|
|
63
|
+
/// The store refused because a fencing identity (circuit generation or
|
|
64
|
+
/// probe token) would have been reused; nothing changed. A
|
|
65
|
+
/// [`DistributedCircuitBreaker`](crate::DistributedCircuitBreaker)
|
|
66
|
+
/// answers it as [`Refusal::Exhausted`].
|
|
67
|
+
Exhausted,
|
|
68
|
+
/// The backend itself failed (connection, timeout, invalid record).
|
|
69
|
+
Backend,
|
|
70
|
+
}
|
|
71
|
+
|
|
60
72
|
/// A distributed storage backend's failure.
|
|
61
73
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
62
74
|
pub struct StorageError {
|
|
75
|
+
kind: StorageErrorKind,
|
|
63
76
|
message: String,
|
|
64
77
|
}
|
|
65
78
|
|
|
66
79
|
impl StorageError {
|
|
67
|
-
/// A storage error carrying the backend's message.
|
|
68
|
-
pub fn new(message: impl Into<String>) -> Self {
|
|
80
|
+
/// A storage error of `kind` carrying the backend's message.
|
|
81
|
+
pub fn new(kind: StorageErrorKind, message: impl Into<String>) -> Self {
|
|
69
82
|
Self {
|
|
83
|
+
kind,
|
|
70
84
|
message: message.into(),
|
|
71
85
|
}
|
|
72
86
|
}
|
|
73
87
|
|
|
88
|
+
/// What kind of failure this is.
|
|
89
|
+
#[must_use]
|
|
90
|
+
pub const fn kind(&self) -> StorageErrorKind {
|
|
91
|
+
self.kind
|
|
92
|
+
}
|
|
93
|
+
|
|
74
94
|
/// The backend's message.
|
|
75
95
|
#[must_use]
|
|
76
96
|
pub fn message(&self) -> &str {
|
|
@@ -54,7 +54,7 @@ pub use clock::{Clock, SystemClock};
|
|
|
54
54
|
#[cfg(feature = "async")]
|
|
55
55
|
pub use distributed::DistributedCircuitBreaker;
|
|
56
56
|
#[cfg(feature = "std")]
|
|
57
|
-
pub use error::{CircuitError, FallbackContext, StorageError};
|
|
57
|
+
pub use error::{CircuitError, FallbackContext, StorageError, StorageErrorKind};
|
|
58
58
|
#[cfg(feature = "async")]
|
|
59
59
|
pub use storage::{
|
|
60
60
|
Applied, AsyncStorageBackend, CircuitSnapshot, FailurePolicy, MemoryStorage, ProbeDecision,
|
|
@@ -20,13 +20,24 @@ impl Threshold {
|
|
|
20
20
|
/// outcomes. `None` unless at least one of `count` and `rate` is set and
|
|
21
21
|
/// `rate` is finite and in `0.0 < rate <= 1.0`.
|
|
22
22
|
#[must_use]
|
|
23
|
-
pub fn new(
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
23
|
+
pub const fn new(
|
|
24
|
+
count: Option<NonZeroU32>,
|
|
25
|
+
rate: Option<f64>,
|
|
26
|
+
minimum_calls: u32,
|
|
27
|
+
) -> Option<Self> {
|
|
28
|
+
let rate_valid = match rate {
|
|
29
|
+
Some(rate) => rate.is_finite() && rate > 0.0 && rate <= 1.0,
|
|
30
|
+
None => count.is_some(),
|
|
31
|
+
};
|
|
32
|
+
if rate_valid {
|
|
33
|
+
Some(Self {
|
|
34
|
+
count,
|
|
35
|
+
rate,
|
|
36
|
+
minimum_calls,
|
|
37
|
+
})
|
|
38
|
+
} else {
|
|
39
|
+
None
|
|
40
|
+
}
|
|
30
41
|
}
|
|
31
42
|
|
|
32
43
|
/// Failures in the window that open the circuit.
|
|
@@ -77,17 +88,20 @@ impl Recovery {
|
|
|
77
88
|
/// at a time; `success_threshold` successful probes close it and one
|
|
78
89
|
/// failed probe reopens it. `None` unless `jitter` is in `0.0..=1.0`.
|
|
79
90
|
#[must_use]
|
|
80
|
-
pub fn new(
|
|
91
|
+
pub const fn new(
|
|
81
92
|
cooldown: Duration,
|
|
82
93
|
jitter: f64,
|
|
83
94
|
success_threshold: NonZeroU32,
|
|
84
95
|
half_open_calls: NonZeroU32,
|
|
85
96
|
) -> Option<Self> {
|
|
86
|
-
|
|
87
|
-
cooldown
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
97
|
+
match Constant::new(cooldown, jitter) {
|
|
98
|
+
Some(cooldown) => Some(Self {
|
|
99
|
+
cooldown,
|
|
100
|
+
success_threshold,
|
|
101
|
+
half_open_calls,
|
|
102
|
+
}),
|
|
103
|
+
None => None,
|
|
104
|
+
}
|
|
91
105
|
}
|
|
92
106
|
|
|
93
107
|
/// The cooldown as the chrono-machines strategy that draws it.
|
|
@@ -4,13 +4,14 @@
|
|
|
4
4
|
//! [`MemoryStorage`] saturates retry deadlines and half-open probe lease
|
|
5
5
|
//! expiries at `Duration::MAX`. Its generations and probe tokens are fencing
|
|
6
6
|
//! identities and never wrap: once `u64::MAX` is taken, an operation that
|
|
7
|
-
//! needs another answers a [`StorageError`]
|
|
7
|
+
//! needs another answers a [`StorageError`] of kind
|
|
8
|
+
//! [`StorageErrorKind::Exhausted`] with nothing changed, and
|
|
8
9
|
//! operations that need none are still honoured.
|
|
9
10
|
//!
|
|
10
11
|
//! [`DistributedCircuitBreaker`]: crate::DistributedCircuitBreaker
|
|
11
12
|
|
|
12
13
|
use crate::clock::{Clock, SystemClock};
|
|
13
|
-
use crate::{Event, Outcome, State, StorageError, Threshold};
|
|
14
|
+
use crate::{Event, Outcome, State, StorageError, StorageErrorKind, Threshold};
|
|
14
15
|
use core::future::Future;
|
|
15
16
|
use core::num::NonZeroU32;
|
|
16
17
|
use core::pin::Pin;
|
|
@@ -249,17 +250,22 @@ impl MemoryCircuit {
|
|
|
249
250
|
/// The generation a transition takes. Generations fence in-flight
|
|
250
251
|
/// calls and leases, so one is never reused.
|
|
251
252
|
fn next_generation(&self) -> Result<u64, StorageError> {
|
|
252
|
-
self.snapshot
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
253
|
+
self.snapshot.generation.checked_add(1).ok_or_else(|| {
|
|
254
|
+
StorageError::new(
|
|
255
|
+
StorageErrorKind::Exhausted,
|
|
256
|
+
"every circuit generation has been used",
|
|
257
|
+
)
|
|
258
|
+
})
|
|
256
259
|
}
|
|
257
260
|
|
|
258
261
|
/// The token the next lease takes; never reused.
|
|
259
262
|
fn next_token(&self) -> Result<u64, StorageError> {
|
|
260
|
-
self.next_token
|
|
261
|
-
|
|
262
|
-
|
|
263
|
+
self.next_token.checked_add(1).ok_or_else(|| {
|
|
264
|
+
StorageError::new(
|
|
265
|
+
StorageErrorKind::Exhausted,
|
|
266
|
+
"every probe token has been used",
|
|
267
|
+
)
|
|
268
|
+
})
|
|
263
269
|
}
|
|
264
270
|
|
|
265
271
|
const fn transition(&mut self, state: State, generation: u64) {
|
|
@@ -315,6 +321,16 @@ impl MemoryStorage {
|
|
|
315
321
|
self.circuits.lock().unwrap_or_else(PoisonError::into_inner)
|
|
316
322
|
}
|
|
317
323
|
|
|
324
|
+
/// Use up `name`'s generations, for tests outside this module.
|
|
325
|
+
#[cfg(test)]
|
|
326
|
+
pub(crate) fn exhaust_generations(&self, name: &str) {
|
|
327
|
+
self.circuits()
|
|
328
|
+
.entry(String::from(name))
|
|
329
|
+
.or_default()
|
|
330
|
+
.snapshot
|
|
331
|
+
.generation = u64::MAX;
|
|
332
|
+
}
|
|
333
|
+
|
|
318
334
|
fn with_circuit<T>(
|
|
319
335
|
&self,
|
|
320
336
|
name: &str,
|
|
@@ -9,7 +9,7 @@ use core::num::NonZeroU32;
|
|
|
9
9
|
use magnus::{Error, Ruby, Value, prelude::*};
|
|
10
10
|
use matryona_machines::ruby::{
|
|
11
11
|
class_name, duration, exact_fields_with, finite, instant, integer, nanoseconds,
|
|
12
|
-
optional_positive, positive,
|
|
12
|
+
optional_instant, optional_positive, positive,
|
|
13
13
|
};
|
|
14
14
|
|
|
15
15
|
use crate::classes::classes;
|
|
@@ -188,13 +188,6 @@ pub fn validate_policy(
|
|
|
188
188
|
build_policy(ruby, threshold, window, recovery, max_concurrent).map(drop)
|
|
189
189
|
}
|
|
190
190
|
|
|
191
|
-
fn optional_duration(value: Value, name: &str) -> Result<Option<core::time::Duration>, Error> {
|
|
192
|
-
if value.is_nil() {
|
|
193
|
-
return Ok(None);
|
|
194
|
-
}
|
|
195
|
-
instant(value, name).map(Some)
|
|
196
|
-
}
|
|
197
|
-
|
|
198
191
|
/// `Status::new` of Ruby values, in order.
|
|
199
192
|
fn build_status(
|
|
200
193
|
ruby: &Ruby,
|
|
@@ -203,8 +196,8 @@ fn build_status(
|
|
|
203
196
|
retry_at: Value,
|
|
204
197
|
) -> Result<Status, Error> {
|
|
205
198
|
let state = States::parse(ruby, state)?;
|
|
206
|
-
let opened_at =
|
|
207
|
-
let retry_at =
|
|
199
|
+
let opened_at = optional_instant(opened_at, "opened_at")?;
|
|
200
|
+
let retry_at = optional_instant(retry_at, "retry_at")?;
|
|
208
201
|
Status::new(state, opened_at, retry_at).ok_or_else(|| {
|
|
209
202
|
range_error(
|
|
210
203
|
ruby,
|
|
@@ -232,15 +232,16 @@ module BreakerMachines
|
|
|
232
232
|
|
|
233
233
|
def draw = @random.rand(2**64)
|
|
234
234
|
|
|
235
|
-
#
|
|
236
|
-
#
|
|
237
|
-
#
|
|
235
|
+
# Sample the configurable clock before locking; it can call back into
|
|
236
|
+
# the circuit. Clamp the sample against the latest instant under the lock.
|
|
237
|
+
# The machine moves under the lock, and a transition is stored
|
|
238
238
|
# for other circuits of this name (unless it was adopted from them); the
|
|
239
239
|
# hooks the transitions queued run once the lock is released.
|
|
240
240
|
def decide(share: true)
|
|
241
|
+
sampled_at = @clock.call
|
|
241
242
|
transitions = @mutex.synchronize do
|
|
242
243
|
@pending = []
|
|
243
|
-
events = Array(yield([
|
|
244
|
+
events = Array(yield([sampled_at, @breaker.latest].max))
|
|
244
245
|
events.each { |event| __send__(:"#{event}!") }
|
|
245
246
|
check_agreement(events)
|
|
246
247
|
publish if share && !events.empty?
|