Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion internal/api/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (c *Client) Do(ctx context.Context, method string, path string, clientID st
return nil, apiErr
}

if attempt == c.retryMaxAttempts {
if !isRetryableMethod(method) || attempt == c.retryMaxAttempts {
return nil, apiErr
}

Expand Down Expand Up @@ -193,6 +193,15 @@ func isTransientError(err *Error) bool {
return false
}

func isRetryableMethod(method string) bool {
switch method {
case http.MethodGet, http.MethodHead, http.MethodOptions:
return true
default:
return false
}
}

func (c *Client) retryDelay(attempt int, err *Error) time.Duration {
if err != nil && err.Type == ErrorTypeRateLimit && err.RetryAfter > 0 {
return time.Duration(err.RetryAfter) * time.Second
Expand Down
1 change: 1 addition & 0 deletions internal/api/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ func TestClient_Do(t *testing.T) {
// Create client with test server URL
client := NewClientWithOptions(Options{
BaseURL: server.URL,
Sleep: func(_ context.Context, _ time.Duration) error { return nil },
})
ctx := context.Background()

Expand Down
43 changes: 43 additions & 0 deletions internal/api/retry_circuit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,49 @@ func TestClientDo_RetriesTransientErrorAndSucceeds(t *testing.T) {
}
}

func TestClientDo_DoesNotRetryMutatingMethods(t *testing.T) {
t.Parallel()

methods := []string{http.MethodPost, http.MethodPut, http.MethodPatch, http.MethodDelete}
for _, method := range methods {
method := method
t.Run(method, func(t *testing.T) {
t.Parallel()

var calls int32
httpClient := &http.Client{
Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) {
atomic.AddInt32(&calls, 1)
return &http.Response{
StatusCode: http.StatusInternalServerError,
Status: "500 Internal Server Error",
Body: io.NopCloser(strings.NewReader(`{"error":"temporary failure"}`)),
Header: make(http.Header),
Request: req,
}, nil
}),
}

client := NewClientWithOptions(Options{
BaseURL: "https://api.test",
HTTPClient: httpClient,
RetryMaxAttempts: 3,
CircuitFailureThreshold: 10,
CircuitOpenDuration: time.Second,
Sleep: func(_ context.Context, _ time.Duration) error { return nil },
})

_, err := client.Do(context.Background(), method, "/api/test", "client", "secret", []byte(`{"ok":true}`))
if err == nil {
t.Fatal("expected transient error")
}
if got := atomic.LoadInt32(&calls); got != 1 {
t.Fatalf("unexpected call count: got=%d want=1", got)
}
})
}
}

func TestClientDo_CircuitBreakerOpensAfterFailures(t *testing.T) {
t.Parallel()

Expand Down
Loading