Skip to content

Commit

Permalink
Merge pull request #8 from maticnetwork/span-retry
Browse files Browse the repository at this point in the history
MAT-467 retry span and states in bor
  • Loading branch information
vaibhavchellani authored Dec 3, 2019
2 parents 455b93a + 4b452d8 commit 6e9fb12
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 16 deletions.
6 changes: 3 additions & 3 deletions consensus/bor/bor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
56 changes: 43 additions & 13 deletions consensus/bor/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"net/url"
"path"
"time"
)

// ResponseWithHeight defines a response object type that wraps an original
Expand All @@ -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
Expand All @@ -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)
}

0 comments on commit 6e9fb12

Please sign in to comment.