From 8210300ac1717159d5d5bc2d2272e07b47c793a8 Mon Sep 17 00:00:00 2001 From: Carl Cortright Date: Tue, 26 Jul 2022 21:36:20 -0400 Subject: [PATCH 1/2] push retries --- .idea/.gitignore | 8 ++++++++ .idea/ethrpc.iml | 10 ++++++++++ .idea/misc.xml | 6 ++++++ .idea/modules.xml | 8 ++++++++ .idea/vcs.xml | 6 ++++++ ethrpc.go | 36 +++++++++++++++++++++++++++--------- 6 files changed, 65 insertions(+), 9 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/ethrpc.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/ethrpc.iml b/.idea/ethrpc.iml new file mode 100644 index 0000000..25ed3f6 --- /dev/null +++ b/.idea/ethrpc.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..639900d --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..f8fcda6 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/ethrpc.go b/ethrpc.go index 5118b42..4b128b9 100644 --- a/ethrpc.go +++ b/ethrpc.go @@ -9,6 +9,7 @@ import ( "math/big" "net/http" "os" + "time" ) // EthError - ethereum error @@ -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 } // 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) @@ -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 } From a1b21d69c069a4b65df5a39fb29333e5337098a8 Mon Sep 17 00:00:00 2001 From: Carl Cortright Date: Tue, 26 Jul 2022 21:37:24 -0400 Subject: [PATCH 2/2] ready to go --- .gitignore | 4 +++- .idea/.gitignore | 8 -------- .idea/ethrpc.iml | 10 ---------- .idea/misc.xml | 6 ------ .idea/modules.xml | 8 -------- .idea/vcs.xml | 6 ------ 6 files changed, 3 insertions(+), 39 deletions(-) delete mode 100644 .idea/.gitignore delete mode 100644 .idea/ethrpc.iml delete mode 100644 .idea/misc.xml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/vcs.xml diff --git a/.gitignore b/.gitignore index 2e60621..2fe90e2 100644 --- a/.gitignore +++ b/.gitignore @@ -23,4 +23,6 @@ _testmain.go *.test *.prof -bin/* \ No newline at end of file +bin/* + +.idea \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 13566b8..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Editor-based HTTP Client requests -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/.idea/ethrpc.iml b/.idea/ethrpc.iml deleted file mode 100644 index 25ed3f6..0000000 --- a/.idea/ethrpc.iml +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - - - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index 639900d..0000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index f8fcda6..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 35eb1dd..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file