From d5655ae2faf97f20672600733b5b6e25a6175152 Mon Sep 17 00:00:00 2001 From: Lukasz Raczylo Date: Wed, 8 Jun 2022 19:33:34 +0100 Subject: [PATCH] Well.. This escalated quickly. --- gql.go | 16 +++++++++++++--- query.go | 25 +++++++++++++------------ query_test.go | 2 +- 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/gql.go b/gql.go index 1034abf..9235389 100644 --- a/gql.go +++ b/gql.go @@ -23,6 +23,7 @@ import ( "github.com/allegro/bigcache/v3" jsoniter "github.com/json-iterator/go" "github.com/lukaszraczylo/go-simple-graphql/pkg/logging" + retry "github.com/sethvargo/go-retry" "golang.org/x/net/http2" ) @@ -38,7 +39,8 @@ type GraphQL struct { CacheStore *bigcache.BigCache RetriesEnable bool RetriesNumber int - RetriesDelay int + RetriesDelay time.Duration + BackoffSetup retry.Backoff } func pickGraphqlEndpoint() (graphqlEndpoint string) { @@ -86,7 +88,7 @@ func NewConnection() *GraphQL { ReadIdleTimeout: 30 * time.Second, DisableCompression: false, AllowHTTP: true, - PingTimeout: 10 * time.Second, + PingTimeout: 5 * time.Second, TLSClientConfig: &tls.Config{ InsecureSkipVerify: true, }, @@ -98,6 +100,7 @@ func NewConnection() *GraphQL { RetriesEnable: false, RetriesNumber: 1, RetriesDelay: 250, + BackoffSetup: nil, } var err error retriesEnable, retriesEnableExists := os.LookupEnv("RETRIES_ENABLE") @@ -115,9 +118,16 @@ func NewConnection() *GraphQL { } retriesDelay, retriesDelayExists := os.LookupEnv("RETRIES_DELAY") if retriesDelayExists { - g.RetriesDelay, err = strconv.Atoi(retriesDelay) + retDel, err := strconv.Atoi(retriesDelay) + if err != nil { + panic("Invalid value for RETRIES_DELAY") + } + g.RetriesDelay = time.Duration(retDel) * time.Millisecond } } + g.BackoffSetup = retry.NewExponential(g.RetriesDelay) + g.BackoffSetup = retry.WithMaxRetries(uint64(g.RetriesNumber), g.BackoffSetup) + return &g } diff --git a/query.go b/query.go index 8e3e043..ddba1f2 100644 --- a/query.go +++ b/query.go @@ -9,7 +9,6 @@ import ( "io" "io/ioutil" "net/http" - "time" "github.com/lukaszraczylo/pandati" retry "github.com/sethvargo/go-retry" @@ -67,20 +66,22 @@ func (g *GraphQL) Query(queryContent string, queryVariables interface{}, queryHe } httpRequest, err := http.NewRequest("POST", g.Endpoint, bytes.NewBuffer(query)) - //httpRequest.Header.Add("Content-Type", "application/json") + // httpRequest.Header.Set("Content-Type", "application/json") + if err != nil { + g.Log.Error("Unable to create the request", map[string]interface{}{"_error": err.Error()}) + return "", err + } + for header, value := range queryHeaders { httpRequest.Header.Add(fmt.Sprintf("%v", header), fmt.Sprintf("%v", value)) } var httpResponse *http.Response - backoff := retry.NewExponential(time.Duration(g.RetriesDelay)) - backoff = retry.WithMaxRetries(uint64(g.RetriesNumber), backoff) - - retry.Do(ctx, backoff, func(ctx context.Context) error { + retry.Do(ctx, g.BackoffSetup, func(ctx context.Context) error { httpResponse, err = g.HttpClient.Do(httpRequest) if err != nil { - g.Log.Error("Unable to send the query", map[string]interface{}{"_error": err.Error()}) + g.Log.Debug("Unable to send the query RETRY", map[string]interface{}{"_error": err.Error()}) return err } if httpResponse.StatusCode <= 200 && httpResponse.StatusCode >= 204 { @@ -92,19 +93,19 @@ func (g *GraphQL) Query(queryContent string, queryVariables interface{}, queryHe body, err = ioutil.ReadAll(httpResponse.Body) if err != nil { - g.Log.Critical("Unable to read the response", map[string]interface{}{"_error": err.Error()}) - return retry.RetryableError(errors.New(fmt.Sprintf("body parsing %v", httpResponse.StatusCode))) + g.Log.Debug("Unable to read the response", map[string]interface{}{"_error": err.Error()}) + return retry.RetryableError(errors.New(fmt.Sprintf("RETRY: body parsing %v", httpResponse.StatusCode))) } err = json.Unmarshal(body, &queryResult) if err != nil { - g.Log.Error("Unable to unmarshal the query", map[string]interface{}{"_error": err.Error()}) - return retry.RetryableError(errors.New(fmt.Sprintf("body unmarshal %v", httpResponse.StatusCode))) + g.Log.Debug("Unable to unmarshal the query", map[string]interface{}{"_error": err.Error()}) + return retry.RetryableError(errors.New(fmt.Sprintf("RETRY: body unmarshal %v", httpResponse.StatusCode))) } return nil }) - if !pandati.IsZero(queryResult.Errors) || err != nil { + if !pandati.IsZero(queryResult.Errors) { g.Log.Error("Query returned error", map[string]interface{}{"_query": queryContent, "_variables": queryVariables, "_error": fmt.Sprintf("%v", queryResult.Errors), "_response_code": httpResponse.StatusCode}) return "", fmt.Errorf("%v", queryResult.Errors) } diff --git a/query_test.go b/query_test.go index ec11c58..751f426 100644 --- a/query_test.go +++ b/query_test.go @@ -143,7 +143,7 @@ func (suite *TestSuite) Test_GraphQL_Query() { }, { name: "Valid query to https endpoint", - endpoint: "https://telegram-bot.app/v1/graphql", + endpoint: "https://hasura.local/v1/graphql", isLocal: false, args: args{ queryContent: `query packages_prices {