Skip to content

Commit e39bcab

Browse files
docs: note Go SDK retry cancellation preserves HTTP error (#382)
Co-authored-by: inference-gateway-maintainer[bot] <246577062+inference-gateway-maintainer[bot]@users.noreply.github.com>
1 parent baa09a0 commit e39bcab

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

sdks.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,6 +1326,25 @@ client := sdk.NewClient(&sdk.ClientOptions{
13261326

13271327
Leave `RetryConfig` nil to inherit the defaults above, or set `Enabled: false` to turn retries off entirely.
13281328

1329+
#### Cancellation preserves the underlying error
1330+
1331+
Cancelling the request mid-retry keeps the failure that triggered the backoff. If the context is cancelled (or its deadline expires) while the retry loop is sleeping between attempts, the returned error wraps both the last transport or HTTP error that caused the retry and the cancellation cause, formatted as `<last error> (cancelled while retrying: <context error>)`. `errors.Is(err, context.Canceled)` and `errors.Is(err, context.DeadlineExceeded)` still report the cancellation, and the underlying HTTP status stays readable in the error message and through `errors.Is` / `errors.As`. Bounding a slow request by cancelling its context no longer collapses every failure into a bare `context canceled` / `context deadline exceeded` and hides why the request was being retried.
1332+
1333+
```go
1334+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1335+
defer cancel()
1336+
1337+
resp, err := client.GenerateContent(ctx, sdk.Openai, "openai/gpt-4o", messages)
1338+
if err != nil {
1339+
// The deadline can fire mid-retry, but the HTTP 500 that triggered the
1340+
// backoff is still visible, e.g.:
1341+
// "... HTTP 500 ... (cancelled while retrying: context deadline exceeded)".
1342+
if errors.Is(err, context.DeadlineExceeded) {
1343+
log.Printf("gave up mid-retry, underlying failure preserved: %v", err)
1344+
}
1345+
}
1346+
```
1347+
13291348
### Client options
13301349

13311350
`NewClient` takes a `ClientOptions` struct; only `BaseURL` is required.

0 commit comments

Comments
 (0)