Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ _testmain.go
*.test
*.prof

bin/*
bin/*

.idea
36 changes: 27 additions & 9 deletions ethrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"math/big"
"net/http"
"os"
"time"
)

// EthError - ethereum error
Expand Down Expand Up @@ -37,18 +38,21 @@ type ethRequest struct {

// EthRPC - Ethereum rpc client
type EthRPC struct {
url string
client httpClient
log logger
Debug bool
url string
client httpClient
log logger
Debug bool
retries int
retryBackoff int // milliseconds
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why milliseconds? Golang has the type time.Duration

}

// New create new rpc client with given url
func New(url string, options ...func(rpc *EthRPC)) *EthRPC {
rpc := &EthRPC{
url: url,
client: http.DefaultClient,
log: log.New(os.Stderr, "", log.LstdFlags),
url: url,
client: http.DefaultClient,
log: log.New(os.Stderr, "", log.LstdFlags),
retries: 0,
}
for _, option := range options {
option(rpc)
Expand All @@ -57,13 +61,27 @@ func New(url string, options ...func(rpc *EthRPC)) *EthRPC {
return rpc
}

// NewEthRPC create new rpc client with given url
// NewEthRPC create new rpc client with given url and retries
func NewEthRPC(url string, options ...func(rpc *EthRPC)) *EthRPC {
return New(url, options...)
}

func (rpc *EthRPC) call(method string, target interface{}, params ...interface{}) error {
result, err := rpc.Call(method, params...)

// Retry the request in case it fails
retries := rpc.retries
var err error
var result json.RawMessage
for retries <= 0 {
result, err = rpc.Call(method, params...)
if err == nil {
break
}

time.Sleep(time.Duration(rpc.retryBackoff) * time.Millisecond)
retries -= 1
}

if err != nil {
return err
}
Expand Down