diff --git a/thriftbp/client_middlewares.go b/thriftbp/client_middlewares.go index 349afb446..1d56ee939 100644 --- a/thriftbp/client_middlewares.go +++ b/thriftbp/client_middlewares.go @@ -4,6 +4,7 @@ import ( "context" "errors" "log/slog" + "reflect" "slices" "strconv" "sync" @@ -295,9 +296,13 @@ func Retry(defaults ...retry.Option) thrift.ClientMiddleware { return thrift.WrappedTClient{ Wrapped: func(ctx context.Context, method string, args, result thrift.TStruct) (thrift.ResponseMeta, error) { var lastMeta thrift.ResponseMeta + resultValue := reflect.Indirect(reflect.ValueOf(result)) return lastMeta, retrybp.Do( ctx, func() error { + if resultValue.Kind() == reflect.Struct && resultValue.CanSet() { + resultValue.SetZero() + } var err error lastMeta, err = next.Call(ctx, method, args, result) return getClientError(result, err) diff --git a/thriftbp/retry_result_test.go b/thriftbp/retry_result_test.go new file mode 100644 index 000000000..95b3c81b1 --- /dev/null +++ b/thriftbp/retry_result_test.go @@ -0,0 +1,42 @@ +package thriftbp_test + +import ( + "context" + "testing" + + "github.com/apache/thrift/lib/go/thrift" + "github.com/avast/retry-go" + baseplatethrift "github.com/reddit/baseplate.go/internal/gen-go/reddit/baseplate" + "github.com/reddit/baseplate.go/thriftbp" +) + +func TestRetryReturnsSuccessAfterException(t *testing.T) { + var result retryResult + calls := 0 + client := thrift.WrapClient(thrift.WrappedTClient{ + Wrapped: func(_ context.Context, _ string, _, response thrift.TStruct) (thrift.ResponseMeta, error) { + calls++ + r := response.(*retryResult) + if calls == 1 { + r.Error = &baseplatethrift.Error{Retryable: thrift.Pointer(true)} + } else { + r.Success = thrift.Pointer(true) + } + return thrift.ResponseMeta{}, nil + }, + }, thriftbp.Retry(retry.Attempts(2), retry.Delay(0))) + + _, err := client.Call(context.Background(), "test", nil, &result) + if err != nil || calls != 2 { + t.Fatalf("calls = %d, error = %v, want success after two calls", calls, err) + } + if result.Error != nil || result.Success == nil || !*result.Success { + t.Fatalf("result = %+v, want success without the previous exception", result) + } +} + +type retryResult struct { + thrift.TStruct + Success *bool + Error *baseplatethrift.Error +}