-
Notifications
You must be signed in to change notification settings - Fork 2
feat: add Executor hook for pluggable resilience #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
49a558a
feat: add Executor hook for pluggable resilience (circuit breaking, r…
ankurs 1421b8e
fix: replace tautological errors.Is(err, err) with strings.Contains c…
ankurs 03874f1
docs: show method-filtering pattern in SetDefaultExecutor example
ankurs 8909c8f
docs: per-method circuit breakers with different limits
ankurs 9375274
docs: fix Executor doc — allow multiple fn calls for retries
ankurs 26c4bdd
docs: regenerate README to sync Executor doc (allow multiple fn calls)
ankurs 3a7330d
fix: remove req/reply from panic error and log to avoid leaking sensi…
ankurs d26f5d3
fix: include examples module in make test
ankurs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| package examples_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "sync" | ||
| "time" | ||
|
|
||
| "github.com/failsafe-go/failsafe-go" | ||
| "github.com/failsafe-go/failsafe-go/bulkhead" | ||
| "github.com/failsafe-go/failsafe-go/circuitbreaker" | ||
| "github.com/go-coldbrew/interceptors" | ||
| ) | ||
|
|
||
| // ExampleSetDefaultExecutor demonstrates setting up a circuit breaker for | ||
| // specific gRPC methods using failsafe-go. The executor receives the method | ||
| // name, so you can filter which methods get circuit breaking. | ||
| func ExampleSetDefaultExecutor() { | ||
| cb := circuitbreaker.NewBuilder[any](). | ||
| WithFailureThreshold(5). | ||
| WithDelay(5 * time.Second). | ||
| WithSuccessThreshold(2). | ||
| Build() | ||
|
|
||
| // Only apply circuit breaking to specific methods | ||
| protected := map[string]bool{ | ||
| "/payment.Service/Charge": true, | ||
| "/payment.Service/Refund": true, | ||
| } | ||
|
|
||
| interceptors.SetDefaultExecutor(func(ctx context.Context, method string, fn func(ctx context.Context) error) error { | ||
| if !protected[method] { | ||
| return fn(ctx) // passthrough for non-protected methods | ||
| } | ||
| return failsafe.With[any](cb).WithContext(ctx).Run(func() error { | ||
| return fn(ctx) | ||
| }) | ||
| }) | ||
|
|
||
| fmt.Println("method-filtered circuit breaker configured") | ||
| // Output: method-filtered circuit breaker configured | ||
| } | ||
|
|
||
| // ExampleSetDefaultExecutor_perMethod demonstrates per-method circuit breakers | ||
| // with different limits. Each method gets its own circuit breaker with | ||
| // tuning appropriate for that method's characteristics. | ||
| func ExampleSetDefaultExecutor_perMethod() { | ||
| type cbConfig struct { | ||
| failureThreshold uint | ||
| delay time.Duration | ||
| } | ||
|
|
||
| // Different limits per method | ||
| configs := map[string]cbConfig{ | ||
| "/payment.Service/Charge": {failureThreshold: 3, delay: 10 * time.Second}, // sensitive — trip fast, recover slow | ||
| "/payment.Service/Refund": {failureThreshold: 3, delay: 10 * time.Second}, | ||
| "/user.Service/GetUser": {failureThreshold: 10, delay: 5 * time.Second}, // tolerant — allow more failures | ||
| "/feed.Service/GetFeed": {failureThreshold: 10, delay: 5 * time.Second}, | ||
| } | ||
|
|
||
| var ( | ||
| mu sync.Mutex | ||
| breakers = make(map[string]circuitbreaker.CircuitBreaker[any]) | ||
| ) | ||
|
|
||
| interceptors.SetDefaultExecutor(func(ctx context.Context, method string, fn func(ctx context.Context) error) error { | ||
| cfg, ok := configs[method] | ||
| if !ok { | ||
| return fn(ctx) // no circuit breaker for unconfigured methods | ||
| } | ||
|
|
||
| mu.Lock() | ||
| cb, exists := breakers[method] | ||
| if !exists { | ||
| cb = circuitbreaker.NewBuilder[any](). | ||
| WithFailureThreshold(cfg.failureThreshold). | ||
| WithDelay(cfg.delay). | ||
| Build() | ||
| breakers[method] = cb | ||
| } | ||
| mu.Unlock() | ||
|
|
||
| return failsafe.With[any](cb).WithContext(ctx).Run(func() error { | ||
| return fn(ctx) | ||
| }) | ||
| }) | ||
|
|
||
| fmt.Println("per-method circuit breakers configured") | ||
| // Output: per-method circuit breakers configured | ||
| } | ||
|
|
||
| // ExampleSetDefaultExecutor_bulkhead demonstrates composing a circuit breaker | ||
| // with a bulkhead (concurrency limiter) using failsafe-go. | ||
| func ExampleSetDefaultExecutor_bulkhead() { | ||
| cb := circuitbreaker.NewBuilder[any](). | ||
| WithFailureThreshold(5). | ||
| WithDelay(5 * time.Second). | ||
| Build() | ||
|
|
||
| bh := bulkhead.New[any](200) | ||
|
|
||
| // Policies execute right-to-left: bulkhead limits concurrency, | ||
| // circuit breaker wraps the result. | ||
| interceptors.SetDefaultExecutor(func(ctx context.Context, method string, fn func(ctx context.Context) error) error { | ||
| return failsafe.With[any](cb, bh).WithContext(ctx).Run(func() error { | ||
| return fn(ctx) | ||
| }) | ||
| }) | ||
|
|
||
| fmt.Println("circuit breaker + bulkhead configured") | ||
| // Output: circuit breaker + bulkhead configured | ||
| } | ||
|
|
||
| // ExampleWithoutExecutor demonstrates disabling the executor for specific RPCs. | ||
| // This is useful for health checks or internal loopback connections that should | ||
| // not be circuit-broken. | ||
| func ExampleWithoutExecutor() { | ||
| _ = interceptors.WithoutExecutor() | ||
| fmt.Println("executor disabled for this call") | ||
| // Output: executor disabled for this call | ||
| } | ||
|
|
||
| // ExampleWithExecutor demonstrates setting a custom per-service executor | ||
| // with different circuit breaker tuning. | ||
| func ExampleWithExecutor() { | ||
| paymentCB := circuitbreaker.NewBuilder[any](). | ||
| WithFailureThreshold(3). // more sensitive | ||
| WithDelay(10 * time.Second). // longer recovery | ||
| Build() | ||
|
|
||
| _ = interceptors.WithExecutor(func(ctx context.Context, method string, fn func(ctx context.Context) error) error { | ||
| return failsafe.With[any](paymentCB).WithContext(ctx).Run(func() error { | ||
| return fn(ctx) | ||
| }) | ||
| }) | ||
|
|
||
| fmt.Println("per-service executor configured") | ||
| // Output: per-service executor configured | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| module github.com/go-coldbrew/interceptors/examples | ||
|
|
||
| go 1.25.9 | ||
|
|
||
| require ( | ||
| github.com/failsafe-go/failsafe-go v0.9.6 | ||
| github.com/go-coldbrew/interceptors v0.1.25 | ||
| ) | ||
|
|
||
| require ( | ||
| buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.36.11-20260209202127-80ab13bee0bf.1 // indirect | ||
| buf.build/go/protovalidate v1.1.3 // indirect | ||
| cel.dev/expr v0.25.1 // indirect | ||
| github.com/adhocore/gronx v1.19.6 // indirect | ||
| github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5 // indirect | ||
| github.com/airbrake/gobrake/v5 v5.6.2 // indirect | ||
| github.com/antlr4-go/antlr/v4 v4.13.1 // indirect | ||
| github.com/beorn7/perks v1.0.1 // indirect | ||
| github.com/bits-and-blooms/bitset v1.24.4 // indirect | ||
| github.com/caio/go-tdigest/v4 v4.0.1 // indirect | ||
| github.com/cespare/xxhash/v2 v2.3.0 // indirect | ||
| github.com/getsentry/sentry-go v0.43.0 // indirect | ||
| github.com/go-coldbrew/errors v0.2.14 // indirect | ||
| github.com/go-coldbrew/log v0.3.2 // indirect | ||
| github.com/go-coldbrew/options v0.3.0 // indirect | ||
| github.com/go-coldbrew/tracing v0.2.2 // indirect | ||
| github.com/golang/protobuf v1.5.4 // indirect | ||
| github.com/google/cel-go v0.27.0 // indirect | ||
| github.com/google/uuid v1.6.0 // indirect | ||
| github.com/gopherjs/gopherjs v1.20.1 // indirect | ||
| github.com/gorilla/websocket v1.5.3 // indirect | ||
| github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus v1.1.0 // indirect | ||
| github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.3.3 // indirect | ||
| github.com/grpc-ecosystem/grpc-gateway/v2 v2.28.0 // indirect | ||
| github.com/jonboulle/clockwork v0.3.0 // indirect | ||
| github.com/jtolds/gls v4.20.0+incompatible // indirect | ||
| github.com/k2io/hookingo v1.0.6 // indirect | ||
| github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect | ||
| github.com/newrelic/go-agent/v3 v3.42.0 // indirect | ||
| github.com/newrelic/go-agent/v3/integrations/nrgrpc v1.4.7 // indirect | ||
| github.com/pkg/errors v0.9.1 // indirect | ||
| github.com/prometheus/client_golang v1.23.2 // indirect | ||
| github.com/prometheus/client_model v0.6.2 // indirect | ||
| github.com/prometheus/common v0.67.5 // indirect | ||
| github.com/prometheus/procfs v0.20.1 // indirect | ||
| github.com/rollbar/rollbar-go v1.4.8 // indirect | ||
| github.com/smarty/assertions v1.16.0 // indirect | ||
| go.opentelemetry.io/otel v1.43.0 // indirect | ||
| go.opentelemetry.io/otel/trace v1.43.0 // indirect | ||
| go.yaml.in/yaml/v2 v2.4.4 // indirect | ||
| golang.org/x/exp v0.0.0-20250813145105-42675adae3e6 // indirect | ||
| golang.org/x/net v0.52.0 // indirect | ||
| golang.org/x/sys v0.42.0 // indirect | ||
| golang.org/x/text v0.35.0 // indirect | ||
| golang.org/x/time v0.15.0 // indirect | ||
| google.golang.org/genproto/googleapis/api v0.0.0-20260319201613-d00831a3d3e7 // indirect | ||
| google.golang.org/genproto/googleapis/rpc v0.0.0-20260319201613-d00831a3d3e7 // indirect | ||
| google.golang.org/grpc v1.79.3 // indirect | ||
| google.golang.org/protobuf v1.36.11 // indirect | ||
| ) | ||
|
|
||
| replace github.com/go-coldbrew/interceptors => ../ |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.