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
7 changes: 7 additions & 0 deletions internal/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ func (c *Client) do(req *http.Request) (*http.Response, error) {
)
for attempt := 0; attempt <= maxRetries; attempt++ {
if attempt > 0 {
if req.GetBody != nil {
body, err := req.GetBody()
if err != nil {
return nil, fmt.Errorf("failed to reset request body: %w", err)
}
req.Body = body
}
delay := time.Duration(1<<(attempt-1)) * baseDelay
jitter := time.Duration(rand.Int64N(int64(delay / 2)))
sleep(delay + jitter)
Expand Down
39 changes: 39 additions & 0 deletions internal/api/client_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,52 @@
package api

import (
"bytes"
"io"
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"
"time"
)

func TestDo_RetryResetsBody(t *testing.T) {
origSleep := sleep
sleep = func(time.Duration) {}
defer func() { sleep = origSleep }()

var bodies []string
var attempts atomic.Int32
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, _ := io.ReadAll(r.Body)
bodies = append(bodies, string(b))
n := attempts.Add(1)
if n == 1 {
w.WriteHeader(http.StatusInternalServerError)
} else {
w.WriteHeader(http.StatusOK)
}
}))
defer server.Close()

client := NewClient(server.URL, "test-key")
req, _ := client.newRequest(http.MethodPost, "/", bytes.NewReader([]byte(`{"hello":"world"}`)))
resp, err := client.do(req)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
defer resp.Body.Close()

if attempts.Load() != 2 {
t.Fatalf("expected 2 attempts, got %d", attempts.Load())
}
for i, body := range bodies {
if body != `{"hello":"world"}` {
t.Errorf("attempt %d body = %q, want non-empty JSON", i+1, body)
}
}
}

func TestNewRequest(t *testing.T) {
client := NewClient("https://example.com/api/v1", "test-key")

Expand Down
Loading