Skip to content

Commit 2336e8e

Browse files
zhyuJakob3xD
authored andcommitted
add guides on configuring the client with retry and backoff
Signed-off-by: zhyu <[email protected]>
1 parent 019af3a commit 2336e8e

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
1010
- Adds support for OpenSearch 2.14 ([#552](https://github.com/opensearch-project/opensearch-go/pull/552))
1111
- Adds the `Caches` field to Node stats ([#572](https://github.com/opensearch-project/opensearch-go/pull/572))
1212
- Adds the `SeqNo` and `PrimaryTerm` fields in `SearchHit` ([#574](https://github.com/opensearch-project/opensearch-go/pull/574))
13+
- Adds guide on configuring the client with retry and backoff ([#540](https://github.com/opensearch-project/opensearch-go/pull/540))
1314

1415
### Changed
1516
- Security roles get response struct has its own sub structs without omitempty ([#572](https://github.com/opensearch-project/opensearch-go/pull/572))

USER_GUIDE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,3 +396,4 @@ func getCredentialProvider(accessKey, secretAccessKey, token string) aws.Credent
396396
- [Advanced Index Actions](guides/advanced_index_actions.md)
397397
- [Index Templates](guides/index_template.md)
398398
- [Data Streams](guides/data_streams.md)
399+
- [Retry and Backoff](guides/retry_backoff.md)

guides/retry_backoff.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Configure the client with retry and backoff
2+
3+
The OpenSearch client will retry on certain errors, such as `503 Service Unavailable`. And it will retry right after receiving the error. You can customize the retry behavior.
4+
5+
## Setup
6+
7+
Let's create a client instance:
8+
9+
```go
10+
package main
11+
12+
import (
13+
"context"
14+
"fmt"
15+
"os"
16+
"time"
17+
18+
"github.com/opensearch-project/opensearch-go/v4/opensearchapi"
19+
)
20+
21+
func main() {
22+
if err := example(); err != nil {
23+
fmt.Println(fmt.Sprintf("Error: %s", err))
24+
os.Exit(1)
25+
}
26+
}
27+
28+
func example() error {
29+
client, err := opensearchapi.NewClient(opensearchapi.Config{
30+
// Retry on 429 TooManyRequests statuses as well (502, 503, 504 are default values)
31+
//
32+
RetryOnStatus: []int{502, 503, 504, 429},
33+
34+
// A simple incremental backoff function
35+
//
36+
RetryBackoff: func(i int) time.Duration { return time.Duration(i) * 100 * time.Millisecond },
37+
38+
// Retry up to 5 attempts (1 initial + 4 retries)
39+
//
40+
MaxRetries: 4,
41+
})
42+
if err != nil {
43+
return err
44+
}
45+
```
46+
47+
If you do not want to wait too long when the server is not responsive, then control the total duration of the requests with a context. The on-going request and the backoff will be canceled when the context is canceled.
48+
49+
```go
50+
rootCtx := context.Background()
51+
ctx := context.WithTimeout(rootCtx, time.Second)
52+
53+
infoResp, err := client.Info(ctx, nil)
54+
return nil
55+
}
56+
```

0 commit comments

Comments
 (0)