Skip to content

Commit

Permalink
use backoff package;
Browse files Browse the repository at this point in the history
Signed-off-by: tcar <tcar121293@gmail.com>
  • Loading branch information
tcar121293 committed Jul 31, 2024
1 parent 4a8f0f3 commit 0272fed
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
44 changes: 32 additions & 12 deletions chains/btc/uploader/ipfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ package uploader
import (
"bytes"
"encoding/json"
"errors"
"io"
"mime/multipart"
"net/http"
"time"

"github.com/rs/zerolog/log"

"github.com/ChainSafe/sygma-relayer/config/relayer"
"github.com/cenkalti/backoff/v4"
)

const MAX_RETRIES = 3
Expand Down Expand Up @@ -55,31 +59,47 @@ func (s *IPFSUploader) Upload(dataToUpload []map[string]interface{}) (string, er
req.Header.Add("Authorization", "Bearer "+s.config.AuthToken)
req.Header.Add("Content-Type", writer.FormDataContentType())

var resp *http.Response
for attempt := 1; attempt <= MAX_RETRIES; attempt++ {
client := &http.Client{}
resp, err = client.Do(req)
if err == nil && resp.StatusCode == 200 {
break
}
var ipfsResponse IPFSResponse

// Define the operation to be retried
operation := func() error {
return s.performRequest(req, &ipfsResponse)
}
expBackoff := backoff.NewExponentialBackOff()
expBackoff.MaxElapsedTime = time.Minute

time.Sleep(time.Duration(attempt) * time.Second)
notify := func(err error, duration time.Duration) {
log.Warn().Err(err).Msg("Unable to upload metadata to ipfs")
}

err = backoff.RetryNotify(operation, backoff.WithMaxRetries(expBackoff, 5), notify)
if err != nil {
return "", err
}

return ipfsResponse.IpfsHash, nil
}

func (s *IPFSUploader) performRequest(req *http.Request, ipfsResponse *IPFSResponse) error {
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()

if resp.StatusCode != 200 {
return errors.New("received non-200 status code")
}

respBody, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
return err
}

var ipfsResponse IPFSResponse
if err := json.Unmarshal(respBody, &ipfsResponse); err != nil {
return "", err
return err
}

return ipfsResponse.IpfsHash, nil
return nil
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ require (
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f // indirect
github.com/btcsuite/go-socks v0.0.0-20170105172521-4720035b7bfd // indirect
github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/consensys/bavard v0.1.13 // indirect
github.com/consensys/gnark-crypto v0.12.1 // indirect
github.com/crate-crypto/go-kzg-4844 v0.3.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ github.com/btcsuite/winsvc v1.0.0/go.mod h1:jsenWakMcC0zFBFurPLEAyrnc/teJEM1O46f
github.com/buger/jsonparser v0.0.0-20181115193947-bf1c66bbce23/go.mod h1:bbYlZJ7hK1yFx9hf58LP0zeX7UjIGs20ufpu3evjr+s=
github.com/cenkalti/backoff/v4 v4.2.1 h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqylYbM=
github.com/cenkalti/backoff/v4 v4.2.1/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/centrifuge/go-substrate-rpc-client/v4 v4.2.1 h1:io49TJ8IOIlzipioJc9pJlrjgdJvqktpUWYxVY5AUjE=
github.com/centrifuge/go-substrate-rpc-client/v4 v4.2.1/go.mod h1:k61SBXqYmnZO4frAJyH3iuqjolYrYsq79r8EstmklDY=
Expand Down

0 comments on commit 0272fed

Please sign in to comment.