From 5ec4bfa35d7827148b542e96a77fc222133a43d5 Mon Sep 17 00:00:00 2001 From: Vegard Stikbakke Date: Fri, 7 Jun 2024 10:28:22 +0200 Subject: [PATCH] Use retryable client again (#11) As mentioned in #10, we were not seeing context propagation work. Turns out we just needed to create the request with the context correctly. --- client/duneapi/client.go | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/client/duneapi/client.go b/client/duneapi/client.go index 3179f20..c4fbd17 100644 --- a/client/duneapi/client.go +++ b/client/duneapi/client.go @@ -6,11 +6,11 @@ import ( "encoding/json" "fmt" "log/slog" - "net/http" "sync" "time" "github.com/duneanalytics/blockchain-ingester/models" + "github.com/hashicorp/go-retryablehttp" "github.com/klauspost/compress/zstd" ) @@ -28,10 +28,8 @@ type BlockchainIngester interface { } type client struct { - log *slog.Logger - // TODO: Use retryable client - // httpClient *retryablehttp.Client - httpClient *http.Client + log *slog.Logger + httpClient *retryablehttp.Client cfg Config compressor *zstd.Encoder bufPool *sync.Pool @@ -44,16 +42,14 @@ func New(log *slog.Logger, cfg Config) (*client, error) { // revive:disable-line if err != nil { return nil, err } - // TODO: Use retryable client - // httpClient := retryablehttp.NewClient() - // httpClient.RetryMax = MaxRetries - // httpClient.Logger = log - // httpClient.CheckRetry = retryablehttp.DefaultRetryPolicy - // httpClient.Backoff = retryablehttp.LinearJitterBackoff + httpClient := retryablehttp.NewClient() + httpClient.RetryMax = MaxRetries + httpClient.Logger = log + httpClient.CheckRetry = retryablehttp.DefaultRetryPolicy + httpClient.Backoff = retryablehttp.LinearJitterBackoff return &client{ log: log, - httpClient: &http.Client{}, - // httpClient: httpClient, + httpClient: httpClient, cfg: cfg, compressor: comp, bufPool: &sync.Pool{ @@ -128,9 +124,7 @@ func (c *client) sendRequest(ctx context.Context, request BlockchainIngestReques url := fmt.Sprintf("%s/beta/blockchain/%s/ingest", c.cfg.URL, c.cfg.BlockchainName) c.log.Debug("Sending request", "url", url) - // TODO: Use retryable client - // req, err := retryablehttp.NewRequest("POST", url, bytes.NewReader(request.Payload)) - req, err := http.NewRequest("POST", url, bytes.NewReader(request.Payload)) + req, err := retryablehttp.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(request.Payload)) if err != nil { return err }