add client-side circuit breaker interceptor for gRPC clients#550
Open
k4leung4 wants to merge 4 commits into
Open
add client-side circuit breaker interceptor for gRPC clients#550k4leung4 wants to merge 4 commits into
k4leung4 wants to merge 4 commits into
Conversation
Signed-off-by: Kenny Leung <kleung@chainguard.dev>
0e69e20 to
70befdc
Compare
- Fix ReadyToTrip: >= 5 (not > 5) to match documented "5 consecutive failures" behavior - Add codes.Canceled to success list — cancellation is typically client-initiated (context timeout), not a server failure - Add OnStateChange logging via clog for production observability - Add doc comment on StreamClientInterceptor noting that only stream establishment is tracked (not Send/Recv errors) - Use defensive type assertion in stream interceptor - Add TestStreamClientInterceptor_TripsAndFailsFast - Add Canceled, Aborted, DataLoss to IsSuccessful test table Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Use clog.InfoContextf with context.Background() instead of clog.Infof so the log entry goes through the structured logger configured at process startup. OnStateChange fires outside any request context, so context.Background() is the correct scope. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
eslerm
reviewed
Mar 27, 2026
eslerm
left a comment
There was a problem hiding this comment.
🤖: Solid circuit breaker implementation — no correctness bugs, good test coverage with real in-process server. Two comment nits.
| } | ||
| // Treat client-side errors as successes (the server didn't fail). | ||
| // Canceled is included because cancellation is typically initiated | ||
| // by the client (context timeout or user abort), not a server failure. |
There was a problem hiding this comment.
🤖: Comment says "context timeout" but codes.Canceled is explicit context cancellation (e.g., ctx.Cancel()), not a timeout. Timeouts produce codes.DeadlineExceeded which is already handled separately above. Consider: "cancelled by the client (explicit cancellation), not a server failure."
|
|
||
| settings := DefaultSettings("test") | ||
| settings.ReadyToTrip = func(counts gobreaker.Counts) bool { | ||
| return counts.ConsecutiveFailures > 2 // Trip after 2 for faster test |
There was a problem hiding this comment.
🤖: Comment says "Trip after 2" but the condition is > 2, which trips after 3 consecutive failures. Should be "Trip after 3" or change to > 1.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add a new
pkg/interceptors/circuitbreakerpackage that provides gRPCclient interceptors backed by
sony/gobreaker. When a downstream servicereturns too many consecutive errors, the circuit opens and calls fail fast
with
codes.Unavailableinstead of adding load to the failing service.Motivation
Addresses CUS-239 /
CUS-172 /
chainguard-dev/internal-dev#18988.
During INC-44, all services continued retrying the overwhelmed load balancer,
amplifying the cascade. A circuit breaker would have stopped the cascade
within seconds by failing fast once the downstream was detected as unhealthy.
Package API
Default settings
Error classification
Client-side errors (the server didn't fail) are classified as successes
and do not count toward tripping the breaker:
Unauthenticated, FailedPrecondition, OutOfRange, Unimplemented, Canceled
Canceledis classified as a success because cancellation is typicallyclient-initiated (context timeout or user abort), not a server failure.
Server-side errors that count as failures:
Unknown, Aborted, DataLoss
Observability
State transitions (closed → open → half-open → closed) are logged via
clog.InfoContextfwithcontext.Background()(circuit-level events,not request-scoped).
Stream interceptor limitation
The stream interceptor only tracks stream establishment errors.
Errors on
Send/Recvafter the stream is established are not tracked,so a downstream that accepts connections but fails on every message will
not trip the breaker. This is an inherent limitation of the gRPC stream
interceptor model.
Tests
TestCircuitBreaker_TripsAfterConsecutiveFailures— circuit opens afterN server errors, subsequent calls fail fast with Unavailable
TestCircuitBreaker_ClientErrorsDoNotTrip— NotFound etc don't tripTestCircuitBreaker_RecoversThroughHalfOpen— open → half-open → probesucceeds → closed
TestStreamClientInterceptor_TripsAndFailsFast— stream call failsfast when circuit is open
TestDefaultSettings_IsSuccessful— table-driven test of errorclassification across 16 gRPC codes
Follow-up in mono
After bumping go-grpc-kit, enable the circuit breaker per-client in
api-impl/cmd/backend/main.go'sconfigureClients(). Start with theIAM datastore client (highest traffic, most critical) and expand.
Test plan
go test ./pkg/interceptors/circuitbreaker/ -v— 5/5 pass🤖 Generated with Claude Code