-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…540) * fix request context cancellation is ignored when retryBackoff (#539) Signed-off-by: zhyu <[email protected]> * fix unexpected sleep after the last retry Signed-off-by: zhyu <[email protected]> * add guides on configuring the client with retry and backoff Signed-off-by: zhyu <[email protected]> * Update guides/retry_backoff.md Co-authored-by: Jakob <[email protected]> Signed-off-by: Yu Zhang <[email protected]> --------- Signed-off-by: zhyu <[email protected]> Signed-off-by: Yu Zhang <[email protected]> Co-authored-by: Jakob <[email protected]>
- Loading branch information
Showing
5 changed files
with
152 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Configure the client with retry and backoff | ||
|
||
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. | ||
|
||
## Setup | ||
|
||
Let's create a client instance: | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
"github.com/opensearch-project/opensearch-go/v4/opensearchapi" | ||
) | ||
|
||
func main() { | ||
if err := example(); err != nil { | ||
fmt.Println(fmt.Sprintf("Error: %s", err)) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func example() error { | ||
client, err := opensearchapi.NewClient(opensearchapi.Config{ | ||
// Retry on 429 TooManyRequests statuses as well (502, 503, 504 are default values) | ||
RetryOnStatus: []int{502, 503, 504, 429}, | ||
|
||
// A simple incremental backoff function | ||
RetryBackoff: func(i int) time.Duration { return time.Duration(i) * 100 * time.Millisecond }, | ||
|
||
// Retry up to 5 attempts (1 initial + 4 retries) | ||
MaxRetries: 4, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
``` | ||
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. | ||
```go | ||
rootCtx := context.Background() | ||
ctx := context.WithTimeout(rootCtx, time.Second) | ||
|
||
infoResp, err := client.Info(ctx, nil) | ||
return nil | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters