Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(sdk-go): keep a 429 body read failure instead of retrying

Yudhi Armyndharis0022b5326c56
diff --git a/sdk/go/client_test.go b/sdk/go/client_test.go
index ad58db5e3..c4c10f73f 100644
--- a/sdk/go/client_test.go
+++ b/sdk/go/client_test.go
@@ -2157,3 +2157,104 @@ func TestRetryDoesNotResendBeforeOversizedRetryAfter(t *testing.T) {
}
}
}
+
+type responseReadErrorBody struct {
+ reads, closed int
+}
+
+func (b *responseReadErrorBody) Read(p []byte) (int, error) {
+ b.reads++
+ return copy(p, `{"code":`), io.ErrUnexpectedEOF
+}
+
+func (b *responseReadErrorBody) Close() error {
+ b.closed++
+ return nil
+}
+
+func TestRetryPreservesResponseBodyReadError(t *testing.T) {
+ for _, status := range []int{200, 429} {
+ t.Run(fmt.Sprint(status), func(t *testing.T) {
+ body := &responseReadErrorBody{}
+ var calls int32
+ c := newTestClient(t, RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
+ atomic.AddInt32(&calls, 1)
+ return &http.Response{StatusCode: status, Header: http.Header{"Retry-After": {"3600"}}, Body: body, Request: req}, nil
+ }), WithTimeout(500*time.Millisecond), WithRetry(DefaultRetryPolicy()))
+ err := c.Do(context.Background(), "GET", "/api/x", nil, nil, nil)
+ if !errors.Is(err, io.ErrUnexpectedEOF) {
+ t.Errorf("error = %v, want preserved io.ErrUnexpectedEOF", err)
+ }
+ if calls != 1 || body.reads != 1 || body.closed != 1 {
+ t.Errorf("calls = %d, reads = %d, closes = %d; want one each", calls, body.reads, body.closed)
+ }
+ })
+ }
+}
+
+func TestRetryPreservesResponseBodyTimeout(t *testing.T) {
+ for _, status := range []int{200, 429} {
+ for _, callerDeadline := range []bool{false, true} {
+ t.Run(fmt.Sprintf("%d/caller=%v", status, callerDeadline), func(t *testing.T) {
+ var calls int32
+ started := make(chan struct{}, 1)
+ finished := make(chan struct{}, 1)
+ release := make(chan struct{})
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ atomic.AddInt32(&calls, 1)
+ w.Header().Set("Content-Type", "application/json")
+ w.WriteHeader(status)
+ _, _ = w.Write([]byte(`{"code":`))
+ w.(http.Flusher).Flush()
+ select {
+ case started <- struct{}{}:
+ default:
+ }
+ select {
+ case <-r.Context().Done():
+ case <-release:
+ }
+ select {
+ case finished <- struct{}{}:
+ default:
+ }
+ }))
+ t.Cleanup(srv.Close)
+ t.Cleanup(func() { close(release) })
+ ctx := context.Background()
+ wantTimeout := 125 * time.Millisecond
+ opts := []Option{WithRetry(DefaultRetryPolicy())}
+ if callerDeadline {
+ var cancel context.CancelFunc
+ ctx, cancel = context.WithTimeout(ctx, wantTimeout)
+ defer cancel()
+ wantTimeout = 0
+ } else {
+ opts = append(opts, WithTimeout(wantTimeout))
+ }
+ c, err := New(srv.URL, "k", opts...)
+ if err != nil {
+ t.Fatal(err)
+ }
+ err = c.Do(ctx, "GET", "/api/x", nil, nil, nil)
+ select {
+ case <-started:
+ default:
+ t.Fatal("the response body was not flushed")
+ }
+ var te *TimeoutError
+ if !errors.As(err, &te) || te.Timeout != wantTimeout {
+ t.Errorf("error = %v, want a TimeoutError with budget %s", err, wantTimeout)
+ }
+ if got := atomic.LoadInt32(&calls); got != 1 {
+ t.Errorf("calls = %d, want one", got)
+ }
+ select {
+ case <-finished:
+ case <-time.After(2 * time.Second):
+ t.Error("cancellation did not release the server handler")
+ }
+ })
+ }
+ }
+}
diff --git a/sdk/go/retry.go b/sdk/go/retry.go
index 399660120..3ecf2bdb0 100644
--- a/sdk/go/retry.go
+++ b/sdk/go/retry.go
@@ -176,21 +176,22 @@ func retryAfterHeader(h http.Header) (time.Duration, bool) {
// isSendPacingRefusal reports whether resp is the gateway's send-pacing 429,
// which must not be retried before the body's retryAfterSeconds. It reads the
-// body and puts the bytes back, so the caller still gets the full error.
-func isSendPacingRefusal(resp *http.Response) bool {
+// body and puts the bytes back, so the caller still gets the full error. A body that cannot be read
+// to the end is returned as an error, not replaced by the part that arrived.
+func isSendPacingRefusal(resp *http.Response) (bool, error) {
if resp.StatusCode != http.StatusTooManyRequests || resp.Body == nil {
- return false
+ return false, nil
}
data, err := io.ReadAll(resp.Body)
_ = resp.Body.Close()
- resp.Body = io.NopCloser(bytes.NewReader(data))
if err != nil {
- return false
+ return false, err
}
+ resp.Body = io.NopCloser(bytes.NewReader(data))
var body struct {
Code string `json:"code"`
}
- return json.Unmarshal(data, &body) == nil && body.Code == "SEND_PACING_LIMITED"
+ return json.Unmarshal(data, &body) == nil && body.Code == "SEND_PACING_LIMITED", nil
}
// retryMiddleware retries network errors and retryable statuses per policy,
@@ -229,8 +230,14 @@ func retryMiddleware(p RetryPolicy, log Logger) Middleware {
// after the gateway already sent the message, so replaying
// a POST on those would double-send. A send-pacing 429 is
// usually not transient, so it goes back to the caller.
- retryable = retryableForMethod(req.Method, resp.StatusCode) &&
- attempt < p.MaxRetries && !isSendPacingRefusal(resp)
+ retryable = retryableForMethod(req.Method, resp.StatusCode) && attempt < p.MaxRetries
+ if retryable {
+ pacing, readErr := isSendPacingRefusal(resp)
+ if readErr != nil {
+ return nil, readErr
+ }
+ retryable = !pacing
+ }
}
if !retryable || attempt >= p.MaxRetries {
return resp, err