Skip to main content

Add Retries

The Speakeasy SDK Generator supports the ability to generate SDKs that will automatically retry requests that fail due to network errors or any configured HTTP Status code. This can be enabled globally for all requests or on a per request basis by using the x-speakeasy-retries extension within your OpenAPI document. The end-user of the SDK can then override the default configuration by passing in a RetryConfig object to the Operation method at runtime.

Global Retries

openapi: 3.0.3
info:
title: Swagger Petstore - OpenAPI 3.0
version: 1.0.11
servers:
- url: https://petstore3.swagger.io/api/v3
x-speakeasy-retries:
strategy: backoff
backoff:
initialInterval: 500 # 500 milliseconds
maxInterval: 60000 # 60 seconds
maxElapsedTime: 3600000 # 5 minutes
exponent: 1.5
statusCodes:
- 5XX
retryConnectionErrors: true

By adding the x-speakeasy-retries extension to the root of the OpenAPI document, the SDK Generator will generate a global retry configuration that will be used for all requests made by the SDK. The x-speakeasy-retries extension supports the following properties:

PropertyTypeDescriptionRequired
strategystringThe retry strategy to use. Currently only backoff is supported.Yes
backoffobjectThe configuration of the backoff strategy if chosen.No
backoff.initialIntervalintegerThe initial interval to use for the backoff strategy (in milliseconds).No
backoff.maxIntervalintegerThe maximum interval between retries (in milliseconds).No
backoff.maxElapsedTimeintegerThe maximum elapsed time to retry for (in milliseconds).No
backoff.exponentnumberThe exponent used to increase the interval between retries.No
statusCodesarrayThe HTTP Status codes to retry on.Yes
retryConnectionErrorsbooleanWhether to retry any connection errors.No

The statusCodes property supports passing a list of distinct HTTP Status codes to retry on, or a wildcard range to retry on for example 5XX will retry all status codes between 500 (inclusive) and 600 (exclusive).

The retryConnectionErrors property will retry any network errors that occur when making the request. This includes things like DNS resolution errors, connection refused errors, etc.

The defaults for the backoff strategy are:

  • initialInterval: 500
  • maxInterval: 60000
  • maxElapsedTime: 3600000
  • exponent: 1.5

Per Request Retries

openapi: 3.0.3
info:
title: Swagger Petstore - OpenAPI 3.0
version: 1.0.11
servers:
- url: https://petstore3.swagger.io/api/v3
paths:
/pet/findByStatus:
get:
operationId: findPetsByStatus
x-speakeasy-retries:
strategy: backoff
backoff:
initialInterval: 500 # 500 milliseconds
maxInterval: 60000 # 60 seconds
maxElapsedTime: 3600000 # 5 minutes
exponent: 1.5
statusCodes:
- 408
- 500
- 502
- 503
retryConnectionErrors: true
parameters:
- name: status
in: query
description: Status values that need to be considered for filter
required: false
explode: true
schema:
type: string
default: available
enum:
- available
- pending
- sold
responses:
'200':
description: successful operation
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Pet'
application/xml:
schema:
type: array
items:
$ref: '#/components/schemas/Pet'
'400':
description: Invalid status value

Configured the same way as the global retries, the x-speakeasy-retries extension can be added to any operation to override the global retry configuration for that specific operation or add it to a specific operation if not defined globally. This can be useful if you want to retry on a different set of HTTP Status codes for a specific operation or have a different retry configuration for intervals, etc.

Usage

s := sdk.New()
s.FindPetsByStatus(&operations.FindPetsByStatusRequest{
Retries: &utils.RetryConfig{
Strategy: "backoff",
Backoff: &utils.BackoffStrategy{
InitialInterval: 100,
MaxInterval: 10000,
MaxElapsedTime: 60000,
Exponent: 1.1,
},
RetryConnectionErrors: false,
},
})

The Retries property on the request object is a *utils.RetryConfig object. This object supports almost the same properties as the x-speakeasy-retries extension, except for reconfiguring the status codes to retry. It is completely optional and if not provided, the SDK will use the retry configuration defined in the OpenAPI document.