-
Notifications
You must be signed in to change notification settings - Fork 624
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Transport: Add support for request retries
This patch adds the configuration parameters and supporting logic for retrying a failed request: a feature common to official Elasticsearch clients. Executable example: ```golang //+build ignore package main import ( "context" "fmt" "log" "math" "net" "net/http" "os" "os/signal" "strings" "syscall" "time" "github.com/elastic/go-elasticsearch/v8" "github.com/elastic/go-elasticsearch/v8/esapi" "github.com/elastic/go-elasticsearch/v8/estransport" "github.com/cenkalti/backoff" ) var ( _ = fmt.Print _ = context.WithTimeout _ = math.Exp _ = strings.NewReader _ = http.DefaultClient ) func main() { log.SetFlags(0) ticker := time.NewTicker(time.Second) defer ticker.Stop() aborted := make(chan os.Signal) signal.Notify(aborted, os.Interrupt, syscall.SIGTERM) retryBackoff := backoff.NewExponentialBackOff() retryBackoff.InitialInterval = time.Second _ = retryBackoff cfg := elasticsearch.Config{ Addresses: []string{ "http://localhost:9200", // "http://localhost:1000", // "http://localhost:2000", // "http://localhost:3000", "http://localhost:4000", "http://localhost:5000", // "http://localhost:6000", // "http://localhost:7000", // "http://localhost:8000", // "http://localhost:9200", // "http://localhost:9000", // "http://localhost:9201", // "http://localhost:9202", }, Logger: &estransport.ColorLogger{ Output: os.Stdout, EnableRequestBody: true, EnableResponseBody: true, }, // RetryOnStatus: []int{404}, // EnableRetryOnTimeout: true, // MaxRetries: 10, // RetryBackoff: func(i int) time.Duration { // d := time.Duration(i) * time.Second // fmt.Printf("Attempt: %d | Sleeping for %s...\n", i, d) // return d // }, // RetryBackoff: func(i int) time.Duration { // d := time.Duration(math.Exp2(float64(i))) * time.Second // fmt.Printf("Attempt: %d | Sleeping for %s...\n", i, d) // return d // }, RetryBackoff: func(i int) time.Duration { if i == 1 { retryBackoff.Reset() } d := retryBackoff.NextBackOff() fmt.Printf("Attempt: %d | Sleeping for %s...\n", i, d) return d }, // Transport: &http.Transport{ // ResponseHeaderTimeout: time.Millisecond, // }, } es, err := elasticsearch.NewClient(cfg) if err != nil { log.Fatalf("Error creating the client: %s", err) } log.Println("Client ready with URLs:", es.Transport.(*estransport.Client).URLs()) go func() { <-aborted log.Println("\nDone!") os.Exit(0) }() for { select { case <-ticker.C: var ( res *esapi.Response err error ) // res, err = es.Info() // ctx, cancel := context.WithTimeout(context.Background(), time.Nanosecond) // defer cancel() // res, err = es.Info(es.Info.WithContext(ctx)) // es.Search(es.Search.WithTimeout(time.Nanosecond)) // res, err = es.Get("test", "MISSING") res, err = es.Index("test", strings.NewReader(`{"foo":"bar"}`)) if err != nil { if e, ok := err.(net.Error); ok { log.Fatalf("Error getting response: [%T]: %s (timeout:%v)", e, e, e.Timeout()) } else { log.Fatalf("Error getting response: [%T]: %s", err, err) } } if res.IsError() { log.Fatalf("Error response: %s", res.Status()) } } } } ``` Closes #67 (cherry picked from commit 7f37b7b)
- Loading branch information
Showing
8 changed files
with
448 additions
and
73 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
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
Oops, something went wrong.