breaker_machines 0.28.0 → 0.28.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 +7 -0
- data/ext/breaker_machines_native/core/CHANGELOG.md +7 -0
- data/ext/breaker_machines_native/core/Cargo.toml +1 -1
- data/ext/breaker_machines_native/core/README.md +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/storage.rs +25 -9
- data/ext/breaker_machines_native/ffi/Cargo.toml +1 -1
- 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: 79510c6d240a00bb19ad211eb51fd2c9f3deaa363a07acdaba53d03f86aa629d
|
|
4
|
+
data.tar.gz: 2aa407fd07f2803db15af0ea21a91c92c6f8908b2764c208322423c815832567
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 62f2d9e28706100b8f300ef125234f6cb3026455616ffdd4fb5c320595256c3f0c3c05d4e1653bf3eec374e44670b942206c4c208930d82484c3295fe7067ff4
|
|
7
|
+
data.tar.gz: 3727f0dfe59c285aa982e8a48c52858c68287790864beec9adbe0b0fbc4e5bfaa5e30b449cf49ca249874e5e0387bf92f3a1f33050b19219b5a6820749505337
|
data/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,13 @@
|
|
|
6
6
|
|
|
7
7
|
* Restore shared circuit state after state machine initialization.
|
|
8
8
|
|
|
9
|
+
## [0.28.1](https://github.com/seuros/breaker_machines/compare/breaker_machines/v0.28.0...breaker_machines/v0.28.1) (2026-11-16)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
### Bug Fixes
|
|
13
|
+
|
|
14
|
+
* give StorageError a kind so store exhaustion refuses as Refusal::Exhausted ([1c83941](https://github.com/seuros/breaker_machines/commit/1c839417f7c2c864835b4a817130224111826f7d))
|
|
15
|
+
|
|
9
16
|
## [0.28.0](https://github.com/seuros/breaker_machines/compare/breaker_machines/v0.27.0...breaker_machines/v0.28.0) (2026-11-16)
|
|
10
17
|
|
|
11
18
|
|
|
@@ -5,6 +5,13 @@ 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.25.1](https://github.com/seuros/breaker_machines/compare/breaker-machines-v0.25.0...breaker-machines-v0.25.1) (2026-11-16)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* give StorageError a kind so store exhaustion refuses as Refusal::Exhausted ([1c83941](https://github.com/seuros/breaker_machines/commit/1c839417f7c2c864835b4a817130224111826f7d))
|
|
14
|
+
|
|
8
15
|
## [0.25.0](https://github.com/seuros/breaker_machines/compare/breaker-machines-v0.24.0...breaker-machines-v0.25.0) (2026-11-16)
|
|
9
16
|
|
|
10
17
|
|
|
@@ -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
|
|
|
@@ -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,
|
|
@@ -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,
|