From 4b452d8a3ade0a65994a0020b22d11899a009503 Mon Sep 17 00:00:00 2001 From: Jaynti Kanani Date: Tue, 3 Dec 2019 16:47:28 +0530 Subject: [PATCH] retry span and states in bor --- consensus/bor/bor.go | 6 ++--- consensus/bor/rest.go | 56 +++++++++++++++++++++++++++++++++---------- 2 files changed, 46 insertions(+), 16 deletions(-) diff --git a/consensus/bor/bor.go b/consensus/bor/bor.go index dd7cee90ea..a622f0d7d0 100644 --- a/consensus/bor/bor.go +++ b/consensus/bor/bor.go @@ -17,7 +17,7 @@ import ( "sync" "time" - "github.com/maticnetwork/bor" + ethereum "github.com/maticnetwork/bor" "github.com/maticnetwork/bor/accounts" "github.com/maticnetwork/bor/accounts/abi" "github.com/maticnetwork/bor/common" @@ -1038,7 +1038,7 @@ func (c *Bor) commitSpan( header *types.Header, chain core.ChainContext, ) error { - response, err := FetchFromHeimdall(c.httpClient, c.chainConfig.Bor.Heimdall, "bor", "span", strconv.FormatUint(span.ID+1, 10)) + response, err := FetchFromHeimdallWithRetry(c.httpClient, c.chainConfig.Bor.Heimdall, "bor", "span", strconv.FormatUint(span.ID+1, 10)) if err != nil { return err } @@ -1158,7 +1158,7 @@ func (c *Bor) CommitStates( // itereate through state ids for _, stateID := range stateIds { // fetch from heimdall - response, err := FetchFromHeimdall(c.httpClient, c.chainConfig.Bor.Heimdall, "clerk", "event-record", strconv.FormatUint(stateID.Uint64(), 10)) + response, err := FetchFromHeimdallWithRetry(c.httpClient, c.chainConfig.Bor.Heimdall, "clerk", "event-record", strconv.FormatUint(stateID.Uint64(), 10)) if err != nil { return err } diff --git a/consensus/bor/rest.go b/consensus/bor/rest.go index f855adc992..d68c70304e 100644 --- a/consensus/bor/rest.go +++ b/consensus/bor/rest.go @@ -7,6 +7,7 @@ import ( "net/http" "net/url" "path" + "time" ) // ResponseWithHeight defines a response object type that wraps an original @@ -16,19 +17,8 @@ type ResponseWithHeight struct { Result json.RawMessage `json:"result"` } -// FetchFromHeimdall returns data from heimdall -func FetchFromHeimdall(client http.Client, urlString string, paths ...string) (*ResponseWithHeight, error) { - u, err := url.Parse(urlString) - if err != nil { - return nil, err - } - - for _, e := range paths { - if e != "" { - u.Path = path.Join(u.Path, e) - } - } - +// internal fetch method +func internalFetch(client http.Client, u *url.URL) (*ResponseWithHeight, error) { res, err := client.Get(u.String()) if err != nil { return nil, err @@ -54,3 +44,43 @@ func FetchFromHeimdall(client http.Client, urlString string, paths ...string) (* return &response, nil } + +// FetchFromHeimdallWithRetry returns data from heimdall with retry +func FetchFromHeimdallWithRetry(client http.Client, urlString string, paths ...string) (*ResponseWithHeight, error) { + u, err := url.Parse(urlString) + if err != nil { + return nil, err + } + + for _, e := range paths { + if e != "" { + u.Path = path.Join(u.Path, e) + } + } + + for { + res, err := internalFetch(client, u) + if err == nil && res != nil { + return res, nil + } + fmt.Println("Retrying again in 5 seconds", u.String()) + time.Sleep(5 * time.Second) + + } +} + +// FetchFromHeimdall returns data from heimdall +func FetchFromHeimdall(client http.Client, urlString string, paths ...string) (*ResponseWithHeight, error) { + u, err := url.Parse(urlString) + if err != nil { + return nil, err + } + + for _, e := range paths { + if e != "" { + u.Path = path.Join(u.Path, e) + } + } + + return internalFetch(client, u) +}