Skip to content

Commit f6192cc

Browse files
authored
Merge pull request #277624 from pauljewellmsft/retry-go
Add retry article for Go dev guide
2 parents ee9ab39 + a40ab12 commit f6192cc

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

articles/storage/blobs/TOC.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -991,6 +991,10 @@ items:
991991
href: storage-blob-delete-go.md
992992
- name: Manage blob properties and metadata
993993
href: storage-blob-properties-metadata-go.md
994+
- name: Client library configuration options
995+
items:
996+
- name: Implement a retry policy
997+
href: storage-retry-policy-go.md
994998
- name: Test with a storage emulator
995999
items:
9961000
- name: Use the Azurite open-source emulator
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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/26/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 to configure in a [RetryOptions](https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azcore/policy#RetryOptions) 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 field 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 doesn't 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's no maximum. | 60 seconds |
31+
| `StatusCodes` | []int | Optional. Specifies the HTTP status codes that indicate the operation should be retried. Specifying values replaces 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 isn't `nil`. A return value of true means the retry policy should retry. | |
33+
34+
To work with the code example in this article, add the following `import` paths to your code:
35+
36+
```go
37+
import (
38+
"context"
39+
"time"
40+
41+
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
42+
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
43+
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
44+
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
45+
)
46+
```
47+
48+
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), include it in a [ClientOptions](https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azcore/policy#ClientOptions) instance, and create a new client object:
49+
50+
```go
51+
options := azblob.ClientOptions{
52+
ClientOptions: azcore.ClientOptions{
53+
Retry: policy.RetryOptions{
54+
MaxRetries: 10,
55+
TryTimeout: time.Minute * 15,
56+
RetryDelay: time.Second * 1,
57+
MaxRetryDelay: time.Second * 60,
58+
StatusCodes: []int{408, 429, 500, 502, 503, 504},
59+
},
60+
},
61+
}
62+
63+
credential, err := azidentity.NewDefaultAzureCredential(nil)
64+
handleError(err)
65+
66+
client, err := azblob.NewClient(accountURL, credential, &options)
67+
handleError(err)
68+
```
69+
70+
In this example, each service request issued from `client` uses the retry options as defined in the `RetryOptions` struct. This policy applies to client requests. You can configure various retry strategies for service clients based on the needs of your app.
71+
72+
## Related content
73+
74+
- For architectural guidance and general best practices for retry policies, see [Transient fault handling](/azure/architecture/best-practices/transient-faults).
75+
- For guidance on implementing a retry pattern for transient failures, see [Retry pattern](/azure/architecture/patterns/retry).

0 commit comments

Comments
 (0)