Skip to content

Commit 18827e2

Browse files
Add retry article for Go dev guide
1 parent e5d391d commit 18827e2

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
title: Implement a retry policy using the Azure Storage client module for Go
3+
titleSuffix: Azure Storage
4+
description: Learn about retry policies and how to implement them for Blob Storage. This article helps you set up a retry policy for Blob Storage requests using the Azure Storage client module for Go.
5+
author: pauljewellmsft
6+
ms.author: pauljewell
7+
ms.service: azure-blob-storage
8+
ms.topic: how-to
9+
ms.date: 06/10/2024
10+
ms.custom: devx-track-go, devguide-go
11+
---
12+
13+
# Implement a retry policy with Go
14+
15+
Any application that runs in the cloud or communicates with remote services and resources must be able to handle transient faults. It's common for these applications to experience faults due to a momentary loss of network connectivity, a request timeout when a service or resource is busy, or other factors. Developers should build applications to handle transient faults transparently to improve stability and resiliency.
16+
17+
In this article, you learn how to use the Azure Storage client module for Go to configure a retry policy for an application that connects to Azure Blob Storage. Retry policies define how the application handles failed requests, and should always be tuned to match the business requirements of the application and the nature of the failure.
18+
19+
## Configure retry options
20+
21+
Retry policies for Blob Storage are configured programmatically, offering control over how retry options are applied to various service requests and scenarios. For example, a web app issuing requests based on user interaction might implement a policy with fewer retries and shorter delays to increase responsiveness and notify the user when an error occurs. Alternatively, an app or component running batch requests in the background might increase the number of retries and use an exponential backoff strategy to allow the request time to complete successfully.
22+
23+
The following table lists the fields available for a [RetryOptions](/java/api/com.azure.storage.common.policy.requestretryoptions) instance, along with the type, a brief description, and the default value if you make no changes. You should be proactive in tuning the values of these properties to meet the needs of your app.
24+
25+
| Property | Type | Description | Default value |
26+
| --- | --- | --- | --- |
27+
| `MaxRetries` | `int32` | Optional. Specifies the maximum number of attempts a failed operation is retried before producing an error. A value less than zero means one try and no retries. | 3 |
28+
| `TryTimeout` | `time.Duration` | Optional. Indicates the maximum time allowed for any single try of an HTTP request. Specify a value greater than zero to enable. Note: Setting this to a small value might cause premature HTTP request timeouts. | Disabled by default. |
29+
| `RetryDelay` | `time.Duration` | Optional. Specifies the initial amount of delay to use before retrying an operation. The value is used only if the HTTP response does not contain a Retry-After header. The delay increases exponentially with each retry up to the maximum specified by MaxRetryDelay. A value less than zero means no delay between retries. | 4 seconds |
30+
| `MaxRetryDelay` | `time.Duration` | Optional. Specifies the maximum delay allowed before retrying an operation. Typically, the value is greater than or equal to the value specified in `RetryDelay`. A value less than zero means there is no maximum. | 60 seconds |
31+
| `StatusCodes` | []int | Optional. Specifies the HTTP status codes that indicate the operation should be retried. Specifying values will replace the default values. Specifying an empty slice disables retries for HTTP status codes. | 408 - http.StatusRequestTimeout</br>429 - http.StatusTooManyRequests</br>500 - http.StatusInternalServerError</br>502 - http.StatusBadGateway</br>503 - http.StatusServiceUnavailable</br>504 - http.StatusGatewayTimeout |
32+
| `ShouldRetry` | `func(*http.Response, error) bool` | Optional. evaluates if the retry policy should retry the request. When specified, the function overrides comparison against the list of HTTP status codes and error checking within the retry policy. `Context` and `NonRetriable` errors remain evaluated before calling `ShouldRetry`. The `*http.Response` and `error` parameters are mutually exclusive, that is, if one is nil, the other is not nil. A return value of true means the retry policy should retry. | |
33+
34+
In the following code example, we configure the retry options in an instance of [RetryOptions](https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azcore/policy#RetryOptions) and pass it to `BlobServiceClientBuilder` to create a client object:
35+
36+
```go
37+
options := blob.ClientOptions{
38+
ClientOptions: azcore.ClientOptions{
39+
Retry: policy.RetryOptions{
40+
MaxRetries: 10,
41+
TryTimeout: time.Minute * 15,
42+
RetryDelay: time.Second * 1,
43+
MaxRetryDelay: time.Second * 60,
44+
StatusCodes: []int{408, 429, 500, 502, 503, 504},
45+
},
46+
}
47+
}
48+
49+
cred, err := azidentity.NewDefaultAzureCredential(nil)
50+
client := blob.NewClient("http://<storage-account-name>.blob.core.windows.net/<container-name>/<blob-name>", cred, &options)
51+
```
52+
53+
In this example, each service request issued from the `BlobServiceClient` object uses the retry options as defined in the `RetryOptions` instance. This policy applies to client requests. You can configure various retry strategies for service clients based on the needs of your app.
54+
55+
## Related content
56+
57+
- For architectural guidance and general best practices for retry policies, see [Transient fault handling](/azure/architecture/best-practices/transient-faults).
58+
- For guidance on implementing a retry pattern for transient failures, see [Retry pattern](/azure/architecture/patterns/retry).

0 commit comments

Comments
 (0)