Skip to content

SMOODEV-950: Circuit breaker — rate-based detection + onStateChange in Rust/Python/Go#76

Merged
brentrager merged 1 commit into
mainfrom
SMOODEV-950-cb-rate-state-change
May 12, 2026
Merged

SMOODEV-950: Circuit breaker — rate-based detection + onStateChange in Rust/Python/Go#76
brentrager merged 1 commit into
mainfrom
SMOODEV-950-cb-rate-state-change

Conversation

@brentrager
Copy link
Copy Markdown
Contributor

Summary

Surfaces a subset of TS's mollitia-style breaker knobs in the other ports:

  • Python: new failure_rate_threshold + sliding_window_size (rate-based tripping) and on_state_change callback on CircuitBreakerOptions.
  • Rust: CircuitBreaker::with_failure_rate_threshold(...) + with_on_state_change(...). CircuitState is now Copy for ergonomic pass-by-value into callbacks. New CircuitStateChangeCallback exported.
  • Go: ClientBuilder.WithCircuitBreakerStateChange(fn) exposes the existing sony/gobreaker OnStateChange as a first-class builder method.

Test plan

  • Python — uv run pytest (121 passed; 3 new)
  • Rust — cargo test (110 passed; 3 new)
  • Go — go test ./... (95 passed; 1 new)

…Rust/Python/Go

Surface a subset of TS's mollitia-style breaker knobs across the other ports:

Python
------
- New `failure_rate_threshold` + `sliding_window_size` on
  `CircuitBreakerOptions`. When set, the breaker tracks the most recent
  outcomes and trips when the failure ratio meets the threshold (after
  `failure_threshold` minimum samples have been observed).
- New `on_state_change: (from_state, to_state) -> None` callback fires on
  every transition (Closed/Open/HalfOpen). User-callback errors are
  swallowed so they cannot corrupt the breaker's internal bookkeeping.
- `CircuitStateChangeCallback` type alias exported.

Rust
----
- `CircuitBreaker::with_failure_rate_threshold(threshold, window)` builder
  enables rate-based detection.
- `CircuitBreaker::with_on_state_change(callback)` registers a state-change
  callback. Callback type:
  `Arc<dyn Fn(CircuitState, CircuitState) + Send + Sync>`.
- `CircuitState` is now `Copy` (for ergonomic pass-by-value in the callback).
- New `CircuitStateChangeCallback` type exported at crate root.

Go
--
- New `ClientBuilder.WithCircuitBreakerStateChange(fn)` helper exposes the
  underlying sony/gobreaker `OnStateChange` at the top-level builder API.
  (The callback was already reachable via `WithCircuitBreaker(name, opts)`,
  but not surfaced as a first-class builder method — SMOODEV-950 explicitly
  called this out.)

Tests
-----
- Python: 3 new cases covering on_state_change firing on all transitions,
  rate-based threshold tripping, and the minimum-sample suppression.
- Rust: 3 new unit tests in `circuit_breaker.rs`.
- Go: 1 new builder test covering `WithCircuitBreakerStateChange`.
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 12, 2026

🦋 Changeset detected

Latest commit: 0573cb4

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@smooai/fetch Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@brentrager brentrager merged commit ab2588b into main May 12, 2026
1 check failed
@brentrager brentrager deleted the SMOODEV-950-cb-rate-state-change branch May 12, 2026 17:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant