From b8306e6239a7d561cff284ea3d677869c9be8e43 Mon Sep 17 00:00:00 2001 From: "coderabbitai[bot]" <136622811+coderabbitai[bot]@users.noreply.github.com> Date: Tue, 27 Jan 2026 06:50:25 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=9D=20Add=20docstrings=20to=20`add-exc?= =?UTF-8?q?lded-rpc-code-from-hystrix`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Docstrings generation was requested by @ankurs. * https://github.com/go-coldbrew/interceptors/pull/13#issuecomment-3803380349 The following files were modified: * `interceptors.go` * `options.go` --- interceptors.go | 16 ++++++++++++++-- options.go | 21 ++++++++++++++++++--- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/interceptors.go b/interceptors.go index 197c05c..de22490 100644 --- a/interceptors.go +++ b/interceptors.go @@ -24,6 +24,7 @@ import ( "github.com/newrelic/go-agent/v3/integrations/nrgrpc" newrelic "github.com/newrelic/go-agent/v3/newrelic" "google.golang.org/grpc" + "google.golang.org/grpc/status" ) var ( @@ -331,7 +332,11 @@ func GRPCClientInterceptor(options ...grpc_opentracing.Option) grpc.UnaryClientI return grpc_opentracing.UnaryClientInterceptor(options...) } -// HystrixClientInterceptor is the interceptor that intercepts all client requests and adds hystrix info to them +// HystrixClientInterceptor returns a unary client interceptor that executes the RPC inside a Hystrix command. +// +// The interceptor applies provided default and per-call client options to configure Hystrix behavior (for example the command name, disabled flag, excluded errors, and excluded gRPC status codes). +// If Hystrix is disabled via options, the RPC is invoked directly. If the underlying RPC returns an error that matches any configured excluded error or whose gRPC status code matches any configured excluded code, Hystrix fallback is skipped and the RPC error is returned. +// Panics raised during the RPC invocation are captured and reported to the notifier before being converted into an error. If the RPC itself returns an error, that error is returned; otherwise any error produced by Hystrix is returned. func HystrixClientInterceptor(defaultOpts ...grpc.CallOption) grpc.UnaryClientInterceptor { return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { options := clientOptions{ @@ -373,6 +378,13 @@ func HystrixClientInterceptor(defaultOpts ...grpc.CallOption) grpc.UnaryClientIn return nil } } + if st, ok := status.FromError(invokerErr); ok { + for _, code := range options.excludedCodes { + if st.Code() == code { + return nil + } + } + } return invokerErr }, nil) if invokerErr != nil { @@ -449,4 +461,4 @@ func TraceIdInterceptor() grpc.UnaryServerInterceptor { } return handler(ctx, req) } -} +} \ No newline at end of file diff --git a/options.go b/options.go index 11147d7..3e5e2fc 100644 --- a/options.go +++ b/options.go @@ -1,6 +1,9 @@ package interceptors -import "google.golang.org/grpc" +import ( + "google.golang.org/grpc" + "google.golang.org/grpc/codes" +) type clientOption interface { grpc.CallOption @@ -11,6 +14,7 @@ type clientOptions struct { hystrixName string disableHystrix bool excludedErrors []error + excludedCodes []codes.Code } type optionCarrier struct { @@ -22,7 +26,8 @@ func (h *optionCarrier) process(co *clientOptions) { h.processor(co) } -//WithHystrixName changes the hystrix name to be used in the client interceptors +// WithHystrixName creates a clientOption that sets the Hystrix command name used by client interceptors. +// If name is empty, the existing Hystrix name is left unchanged. func WithHystrixName(name string) clientOption { return &optionCarrier{ processor: func(co *clientOptions) { @@ -51,7 +56,8 @@ func WithHystrix() clientOption { } } -// WithHystrixExcludedErrors sets the errors that should be excluded from hystrix circuit breaker +// WithHystrixExcludedErrors returns a clientOption that adds the provided errors to the list of errors +// excluded from the Hystrix circuit breaker. func WithHystrixExcludedErrors(errors ...error) clientOption { return &optionCarrier{ processor: func(co *clientOptions) { @@ -59,3 +65,12 @@ func WithHystrixExcludedErrors(errors ...error) clientOption { }, } } + +// WithHystrixExcludedCodes returns a clientOption that appends the provided gRPC codes to the list of codes excluded from the Hystrix circuit breaker. +func WithHystrixExcludedCodes(codes ...codes.Code) clientOption { + return &optionCarrier{ + processor: func(co *clientOptions) { + co.excludedCodes = append(co.excludedCodes, codes...) + }, + } +} \ No newline at end of file