retry span and states in bor

This commit is contained in:
Jaynti Kanani 2019-12-03 16:47:28 +05:30
parent 455b93aca7
commit 4b452d8a3a
No known key found for this signature in database
GPG key ID: 4396982C976BAE5E
2 changed files with 46 additions and 16 deletions

View file

@ -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
}

View file

@ -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)
}