> This page is for SDK, version Current (default).
> For other versions, use one of these documentation indexes:
> - Current (default): https://buildwithfern.com/learn/sdks/current/llms.txt
> - Legacy: https://buildwithfern.com/learn/sdks/legacy/llms.txt

> For clean Markdown content of this page, append .md to this URL. For the complete documentation index, see https://buildwithfern.com/learn/llms.txt.

# 指数退避重试

> 了解如何在 Fern SDK 中配置指数退避的自动重试。控制重试限制并自定义可重试状态码。

> **企业功能**
>
> 此功能仅适用于[企业计划](https://buildwithfern.com/pricing)。如需开始使用，请联系 [support@buildwithfern.com](mailto:support@buildwithfern.com)。

Fern SDK 将自动使用指数退避重试失败的请求。只要请求被认为是可重试的，且重试次数未超过配置的重试限制，请求就会被重试。

### 可重试状态码

当返回以下任何 HTTP 状态码时，请求被认为是可重试的：

* [408](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/408) (超时)
* [429](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429) (请求过多)
* [5XX](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500) (内部服务器错误)

注意，您也可以配置可重试状态码列表。例如，如果您想从可重试状态码列表中移除 `429` 状态码，您可以这样做。

### 覆盖重试限制

默认情况下，SDK 将最多重试失败的请求 2 次。SDK 用户可以在实例化客户端时覆盖全局默认重试限制。

#### TypeScript

```ts {4}
import { ImdbClient } from "imdb";

const client = new ImdbClient({
  maxRetries: 1 // overrides the default retry limit to 1
});
```

#### Python

```python {4, 8}
from imdb.client import Imdb, AsyncImdb

client = Imdb({
  max_retries: 1 # overrides the default retry limit to 1
})

async_client = AsyncImdb({
  max_retries: 1 # overrides the default retry limit to 1
})
```

#### Java

```java {4}
import com.imdb.ImdbClient;

ImdbClient client = new ImdbClient.Builder()
  .maxRetries(1) // overrides the default retry limit to 1
  .build();
```

#### Go

```go {7}
  import (
    imdbclient "github.com/fern-workos/workos-go/client"
    "github.com/fern-workos/workos-go/option"
  )

  client := imdbclient.NewClient(
    option.WithMaxAttempts(1), // overrides the default retry limit to 1
  )
```

#### C\#

```csharp {4}
using Imdb;

var client = new ImdbClient(new ClientOptions {
  MaxRetries = 1 // overrides the default retry limit to 1
});
```

#### Swift

```swift {5}
import Imdb

let client = ImdbClient(
  baseURL: "https://api.imdb.com",
  maxRetries: 1 // overrides the default retry limit to 1
)
```

#### PHP

```php {4}
use Imdb\ImdbClient;

$client = new ImdbClient([
  "maxRetries" => 1 // overrides the default retry limit to 1
]);
```

也可以在每个请求的基础上覆盖重试限制。

#### TypeScript

```ts {2}
  client.movie.get("tt0111161", {
    maxRetries: 3 // overrides the default retry limit to 3
  });
```

#### Python

```python {2}
  client.movie.get("tt0111161", {
    max_retries: 3 // overrides the default retry limit to 3
  })
```

#### Java

```java {2}
  client.movie().get("tt0111161", RequestOptions.builder()
    .maxRetries(3) // overrides the default retry limit to 3
    .build());
```

#### Go

```go {4}
  response, err := client.Movies.Get(
    ctx,
    "tt0111161",
    option.WithMaxAttempts(1),
  )
```

#### C\#

```csharp {2}
  var movie = await client.Movie.GetAsync("tt0111161", new RequestOptions {
    MaxRetries = 3 // overrides the default retry limit to 3
  });
```

#### Swift

```swift {2}
  let movie = try await client.movie.get("tt0111161", requestOptions: RequestOptions(
    maxRetries: 3 // overrides the default retry limit to 3
  ))
```

#### PHP

```php {2}
  $movie = $client->movie->get("tt0111161", [
    "maxRetries" => 3 // overrides the default retry limit to 3
  ]);
```