Skip to content

fix(circuit-breaker): invoke hooks without holding the registry lock - #1298

Open
anishpentyala wants to merge 2 commits into
go-resty:v3from
anishpentyala:fix/circuit-breaker-hook-dispatch
Open

anishpentyala wants to merge 2 commits into
go-resty:v3from
anishpentyala:fix/circuit-breaker-hook-dispatch

Conversation

@anishpentyala

Copy link
Copy Markdown

Circuit-breaker hooks run with the hook registry's read lock held. If an OnTrigger or OnStateChange callback registers another hook, registration waits for the write lock while dispatch waits for the callback. During a state transition, this also blocks the HTTP request.

Capture the append-only hook slice under the read lock, then invoke the captured hooks after unlocking. Hooks added during a callback start on the next dispatch, in registration order. This changes neither the circuit-breaker API nor its admission/state-transition logic.

Tests cover Count and Ratio breakers, trigger and state-change callbacks, current/next-dispatch ordering, concurrent registration, and a real HTTP request that registers a trigger hook from its state-change callback.

Validation on Go1.26.6 Linux/amd64:

  • The new deadlock tests fail on v3 and pass with the change.
  • Focused race tests pass20consecutive runs; both changed functions have100%statement coverage.
  • Full non-race suite with shuffle, go vet, and go build pass.
  • All526top-level tests pass with race in four disjoint batches. A monolithic race run is killed in this environment, including on the clean base without the new tests, so I am not claiming a green monolithic race run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant