Skip to content

Commit b601edd

Browse files
edits
1 parent 7cc3622 commit b601edd

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

articles/storage/blobs/storage-retry-policy-go.md

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,44 @@ The following table lists the fields available to configure in a [RetryOptions](
2727
| `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 |
2828
| `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. |
2929
| `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. | |
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+
```
3347

3448
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:
3549

3650
```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},
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+
},
4560
},
46-
}
4761
}
4862

49-
cred, err := azidentity.NewDefaultAzureCredential(nil)
50-
client := blob.NewClient("http://<storage-account-name>.blob.core.windows.net/<container-name>/<blob-name>", cred, &options)
63+
credential, err := azidentity.NewDefaultAzureCredential(nil)
64+
handleError(err)
65+
66+
client, err := azblob.NewClient(accountURL, credential, &options)
67+
handleError(err)
5168
```
5269

5370
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.

0 commit comments

Comments
 (0)