breaker_machines 0.28.1 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 79510c6d240a00bb19ad211eb51fd2c9f3deaa363a07acdaba53d03f86aa629d
4
- data.tar.gz: 2aa407fd07f2803db15af0ea21a91c92c6f8908b2764c208322423c815832567
3
+ metadata.gz: 7b8da21ca4815b552c345186e94fdc8b65feb3915de43a994405d56420d8dee7
4
+ data.tar.gz: f75ff2414747ccbb058b11b75f22f0194a98eb2ba570507ffaf541b0244389aa
5
5
  SHA512:
6
- metadata.gz: 62f2d9e28706100b8f300ef125234f6cb3026455616ffdd4fb5c320595256c3f0c3c05d4e1653bf3eec374e44670b942206c4c208930d82484c3295fe7067ff4
7
- data.tar.gz: 3727f0dfe59c285aa982e8a48c52858c68287790864beec9adbe0b0fbc4e5bfaa5e30b449cf49ca249874e5e0387bf92f3a1f33050b19219b5a6820749505337
6
+ metadata.gz: f033ded9d6a3c1c4cd801d9247940fcabc506b324300ac7d6763ea7787b227797121c487e8047cddb6b5f563637e06fa876f5a0f52fd7b7e925688f5a32806dd
7
+ data.tar.gz: 9f7d3b8392767a559a1ea9e82429d257f6c4c54f242af5dde71844c0dca6917dccae3ba36e34693c3085c24d7db836d77445c6544e5280f580cdd263aba7340b
data/CHANGELOG.md CHANGED
@@ -6,6 +6,27 @@
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
+
9
30
  ## [0.28.1](https://github.com/seuros/breaker_machines/compare/breaker_machines/v0.28.0...breaker_machines/v0.28.1) (2026-11-16)
10
31
 
11
32
 
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.6", default-features = false }
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,27 @@ 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
+
8
29
  ## [0.25.1](https://github.com/seuros/breaker_machines/compare/breaker-machines-v0.25.0...breaker-machines-v0.25.1) (2026-11-16)
9
30
 
10
31
 
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "breaker-machines"
3
- version = "0.25.1"
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.12", default-features = false }
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 }
@@ -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.now(&*self.shared.clock);
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.now(&*self.shared.clock);
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.now(&*self.shared.clock))
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.now(&*self.shared.clock);
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
- // The lock guards plain data that no user code runs under, so a
225
- // poisoned lock holds a consistent value.
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.now(&*shared.clock);
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.now(&*self.circuit.shared.clock);
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().max(self.breaker.latest())
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.
@@ -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(count: Option<NonZeroU32>, rate: Option<f64>, minimum_calls: u32) -> Option<Self> {
24
- let rate_valid = rate.is_none_or(|rate| rate.is_finite() && rate > 0.0 && rate <= 1.0);
25
- (rate_valid && (count.is_some() || rate.is_some())).then_some(Self {
26
- count,
27
- rate,
28
- minimum_calls,
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
- Some(Self {
87
- cooldown: Constant::new(cooldown, jitter)?,
88
- success_threshold,
89
- half_open_calls,
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.
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "breaker_machines_native"
3
- version = "0.5.27"
3
+ version = "0.5.30"
4
4
  edition.workspace = true
5
5
  rust-version.workspace = true
6
6
  license.workspace = true
@@ -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 = optional_duration(opened_at, "opened_at")?;
207
- let retry_at = optional_duration(retry_at, "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
- # Decide under the lock: the block runs native calls with the clock read
236
- # inside it and answers the lifecycle events replaying the Breaker's
237
- # answers. The machine moves under the lock, and a transition is stored
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([@clock.call, @breaker.latest].max))
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?
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BreakerMachines
4
- VERSION = '0.28.1'
4
+ VERSION = '0.30.1'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: breaker_machines
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.28.1
4
+ version: 0.30.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Abdelkader Boudih