|
18 | 18 |
|
19 | 19 | package utils |
20 | 20 |
|
21 | | -// DefaultGRPCServiceConfig provides automatic retry configuration for transient gRPC errors. |
22 | | -// This config is applied to all remote gRPC connections to handle network flakiness in CI |
23 | | -// and other environments. |
24 | | -// |
25 | | -// Retries on: |
26 | | -// - UNAVAILABLE: Service temporarily unavailable (e.g., node restarting) |
27 | | -// - RESOURCE_EXHAUSTED: Rate limiting from remote node |
28 | | -// - UNKNOWN: Connection failures, DNS issues, and other network errors |
29 | | -// |
30 | | -// Note: We only retry on clearly transient network/availability errors. |
31 | | -// We do NOT retry on INTERNAL (programming errors), ABORTED (conflicts), |
32 | | -// or DEADLINE_EXCEEDED (to avoid cascading failures on slow services). |
33 | | -const DefaultGRPCServiceConfig = `{ |
34 | | - "methodConfig": [{ |
35 | | - "name": [{"service": ""}], |
36 | | - "retryPolicy": { |
37 | | - "maxAttempts": 5, |
38 | | - "initialBackoff": "0.1s", |
39 | | - "maxBackoff": "30s", |
40 | | - "backoffMultiplier": 2, |
41 | | - "retryableStatusCodes": ["UNAVAILABLE", "RESOURCE_EXHAUSTED", "UNKNOWN"] |
| 21 | +import ( |
| 22 | + "context" |
| 23 | + "time" |
| 24 | + |
| 25 | + "google.golang.org/grpc" |
| 26 | + "google.golang.org/grpc/codes" |
| 27 | + "google.golang.org/grpc/status" |
| 28 | +) |
| 29 | + |
| 30 | +const ( |
| 31 | + defaultMaxAttempts = 10 |
| 32 | + defaultInitialBackoff = 1 * time.Second |
| 33 | + defaultMaxBackoff = 30 * time.Second |
| 34 | + defaultBackoffFactor = 2.0 |
| 35 | +) |
| 36 | + |
| 37 | +// DefaultGRPCRetryInterceptor returns a unary client interceptor that retries |
| 38 | +// transient failures with exponential backoff. Unlike native gRPC retries, this |
| 39 | +// ignores server pushback headers (grpc-retry-pushback-ms) so rate-limited calls |
| 40 | +// will retry client-side rather than fail immediately. |
| 41 | +func DefaultGRPCRetryInterceptor() grpc.DialOption { |
| 42 | + return grpc.WithChainUnaryInterceptor(retryInterceptor) |
| 43 | +} |
| 44 | + |
| 45 | +func retryInterceptor( |
| 46 | + ctx context.Context, |
| 47 | + method string, |
| 48 | + req, reply any, |
| 49 | + cc *grpc.ClientConn, |
| 50 | + invoker grpc.UnaryInvoker, |
| 51 | + opts ...grpc.CallOption, |
| 52 | +) error { |
| 53 | + var lastErr error |
| 54 | + backoff := defaultInitialBackoff |
| 55 | + |
| 56 | + for attempt := 0; attempt < defaultMaxAttempts; attempt++ { |
| 57 | + if attempt > 0 { |
| 58 | + // Wait before retry |
| 59 | + select { |
| 60 | + case <-time.After(backoff): |
| 61 | + case <-ctx.Done(): |
| 62 | + return ctx.Err() |
| 63 | + } |
| 64 | + // Exponential backoff with cap |
| 65 | + backoff = time.Duration(float64(backoff) * defaultBackoffFactor) |
| 66 | + if backoff > defaultMaxBackoff { |
| 67 | + backoff = defaultMaxBackoff |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + lastErr = invoker(ctx, method, req, reply, cc, opts...) |
| 72 | + if lastErr == nil { |
| 73 | + return nil |
| 74 | + } |
| 75 | + |
| 76 | + // Check if error is retryable |
| 77 | + code := status.Code(lastErr) |
| 78 | + if !isRetryableCode(code) { |
| 79 | + return lastErr |
42 | 80 | } |
43 | | - }] |
44 | | -}` |
| 81 | + } |
| 82 | + |
| 83 | + return lastErr |
| 84 | +} |
| 85 | + |
| 86 | +func isRetryableCode(code codes.Code) bool { |
| 87 | + switch code { |
| 88 | + case codes.Unavailable, codes.ResourceExhausted, codes.Unknown: |
| 89 | + return true |
| 90 | + default: |
| 91 | + return false |
| 92 | + } |
| 93 | +} |
| 94 | + |
0 commit comments