Open Source Library · September 7, 2026 · 6 min read
In development

fluxgo: Adaptive Circuit Breaker for Go

An adaptive circuit breaker for Go that moves its failure threshold with real-time latency instead of a fixed number. It follows gobreaker’s API, so adopting it takes little code change.This project is still in development. Here's the case study and progress so far.
GoGithub Action
fluxgo: Adaptive Circuit Breaker for Go

fluxgo: Adaptive Circuit Breaker for Go

Open source · Go library · GitHub · Go Reference fluxgo is an adaptive circuit breaker for Go services. It follows the calling pattern of gobreaker (Execute, State, Counts) but computes its failure-rate threshold from live latency signals instead of using a fixed number. The circuit opens earlier when latency degrades and stays tolerant when the downstream is healthy.

Background

Microservices call each other over the network, and one slow dependency can drag down every service that waits on it. A circuit breaker cuts that chain by failing fast once a downstream looks unhealthy. Most Go implementations, including gobreaker, decide with a fixed failure ratio that is chosen once and never changes. fluxgo started as thesis research on whether a threshold that reacts to latency can stop cascading failures sooner.

Problem

  • Fixed thresholds react late. A static failure ratio says nothing about a downstream that is getting slower but has not started failing yet.
  • Slow is not the same as failed. A request that succeeds in 3 seconds instead of 30 milliseconds still ties up a caller, yet a count-based breaker records it as a success.
  • Cold starts cause false trips. Latency signals are noisy until enough traffic has been observed, so an adaptive breaker can trip on its first requests.
  • A recovered service gets punished. Stale latency from the incident can trip the circuit again right after it closes.

Solution

fluxgo scales the trip threshold by how far latency has drifted from a healthy baseline:
  • Adaptive threshold. A degradation ratio Dt combines an EWMA of latency and a rolling P95/P99, and the threshold becomes θt = clamp(ThetaBase / Dt, ThetaMin, ThetaMax). When latency doubles, the circuit trips at half the failure rate.
  • Slow request classification. A successful request slower than the healthy P95 × 3.0 counts as degraded in the internal failure rate, while the caller still gets the original result.
  • Warmup and anti-flapping. A warmup baseline avoids cold-start false trips, and a minimum dwell time stops the circuit from oscillating.
  • Drop-in calling pattern. Execute, State, and Counts match gobreaker, so existing call sites change very little.

My Role

Sole author and maintainer.
  • Designed the adaptive threshold model and the state machine (Closed, Open, Half-Open).
  • Implemented the EWMA, rolling percentile, outcome window, baseline estimator, and threshold calculator as separate components.
  • Built the test suite with a fake clock, scenario tests, benchmarks, and a comparison benchmark against gobreaker.
  • Set up CI with GitHub Actions and a Makefile for lint, test, coverage, benchmark, and fuzz targets.
  • Wrote the documentation and changelog, including the known limitations.

Architecture

fluxgo is a single Go package where each signal lives in its own file:
  • breaker.go and state_machine.go: the public CircuitBreaker and the Closed, Open, Half-Open transitions.
  • ewma.go and rolling_percentile.go: latency signals updated on every call.
  • outcome_window.go and metric_collector.go: count-based failure window and the data behind Snapshot().
  • baseline_estimator.go: captures the healthy baseline (Sbase, Pbase) during warmup.
  • threshold_calculator.go: turns the signals into the adaptive θt.
  • bench/: a separate module that benchmarks fluxgo against gobreaker, so the library itself has no dependency on it.

Technical Challenges

  1. Total failure was detected too slowly. Problem: After a long healthy period, the failure rate is computed over a window of 100 outcomes, so a complete outage needed about 60 failed requests to reach θt, and failed requests do not feed the latency signals. Solution: Added ConsecutiveFailures (default 5), which opens the circuit after that many errors in a row, so a total failure is detected as fast as in gobreaker. Slow requests do not count toward it, which keeps a traffic spike from tripping it.
  2. Degradation without errors never tripped. Problem: With the current P95 as the slow-request reference, the reference rises together with latency, so a downstream that slowed down without returning errors was never classified as slow. Solution: Made the healthy baseline P95 the default reference (SlowRefBaseline, margin 3.0), and kept the old behavior available as SlowRefRecent.
  3. A recovered service tripped again from stale data. Problem: After Half-Open turns Closed, the EWMA and percentile still described the incident. Solution: Reset them on recovery, keep the baseline, and use the static ThetaBase until enough new samples arrive. Latencies of requests admitted before the transition are ignored.
  4. Half-Open could reject requests forever. Problem: If every probe slot is taken and the downstream hangs, callers would get ErrTooManyRequests indefinitely. Solution: Return the circuit to Open after RecoveryTimeout, ignore a late probe result, and admit a new probe after the next timeout.

Features

  • Adaptive threshold: dynamic θt from EWMA and P95/P99, clamped between ThetaMin and ThetaMax.
  • Slow request classification: baseline or recent reference, configurable margin, visible as DegradedRequests.
  • Warmup baseline: at least 30 seconds by default, and until the rolling window is full.
  • Anti-flapping: MinDwellTime after every transition.
  • Consecutive failure rule: opens the circuit after N errors in a row, regardless of the failure rate.
  • Custom success rules: IsSuccessful marks errors such as context.Canceled as non-failures.
  • Observability: Snapshot() exposes state, EWMA, P95/P99, θt, failure rate, degraded requests, and warmup status.
  • gobreaker-style API: Execute, State, Counts, OnStateChange, ErrOpenState, and ErrTooManyRequests.

Tech Stack

Library
  • Go 1.21+
  • Standard library only, no runtime dependencies
Quality
  • go test with a fake clock, scenario tests, benchmarks, and fuzzing
  • GitHub Actions CI, Makefile targets for fmt, vet, lint, cover, bench, and fuzz
Testbed
  • Go microservices (gateway, auth, user, notification) used for the thesis experiments
  • CB_MODE environment variable to switch between fluxgo and gobreaker at startup

Results

  • Runs with 0 allocations on the hot path (Apple M4, Go 1.27, 10 threads).
  • Execute() in the Closed state takes about 160 ns serial and 290 ns parallel, compared with about 70 ns and 250 ns for gobreaker v1.0.0.
  • A fast reject in the Open state costs about 120 ns, the same as gobreaker.
  • The test suite has 147 test functions across 14 test files.

Lessons Learned

  • A latency signal alone cannot open a circuit. A failure signal is still needed, so slow requests are counted as failures instead of being ignored.
  • Every adaptive rule needs a safe fallback for the moments when its data is unreliable, such as warmup and right after recovery.
  • A rule that makes the breaker safer can still cost something. MinDwellTime prevents flapping but slightly delays a re-trip, so it is documented as a deliberate deviation.

Future Improvements

  • Baseline recapture. The baseline is captured once, so a permanent latency change after a deployment needs a higher margin or a new breaker.
  • Cheaper percentile updates. The rolling percentile update is O(n) in the window size, which also makes Execute() O(n).
  • Stable P99. P99 with a 100-sample window is unstable, so a larger default window would make it usable.
  • More experiment scenarios on the testbed: normal load, traffic spike, gradual degradation, and sudden failure.
More projects: NetSee · DuckLive Let's build something together: Email · LinkedIn
Open to work

Let's build something together

Have a role, internship or project in mind? I'd love to hear about it.
Click to copy