From 46cf76980783a2f84f9b85fe8a6cbd5cc46eba33 Mon Sep 17 00:00:00 2001 From: dyoung Date: Wed, 25 May 2022 11:28:04 +0800 Subject: [PATCH] =?UTF-8?q?use=20provider=20of=20go-rpc-provider=20for=20h?= =?UTF-8?q?ook=20CallContext/Call/BatchCallCo=E2=80=A6=20(#179)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * use provider of go-rpc-provider for hook CallContext/Call/BatchCallContext/BatchCall/Subscribe remove client_retry * remove client_retry * add context in CallRpcHandlerFunc/BatchCallRpcHandlerFunc for hook context when call/batchcall * remove comments * add changelog * set default timeout to 3s * restore default timeout to 30s * nil --- changeLog.md | 5 ++ client.go | 90 +++++++++++------------------------- client_retry.go | 86 ---------------------------------- client_test.go | 32 +++++++++++-- go.mod | 7 +-- go.sum | 12 +++-- interface.go | 12 ++--- middleware/call_rpc.go | 14 +++--- middleware/call_rpc_batch.go | 16 ++++--- 9 files changed, 91 insertions(+), 183 deletions(-) delete mode 100644 client_retry.go diff --git a/changeLog.md b/changeLog.md index c238cd0..a7d5581 100644 --- a/changeLog.md +++ b/changeLog.md @@ -1,6 +1,11 @@ Go-conflux-sdk Change Log ============ +v1.2.0 +------------ +- add context in CallRpcHandlerFunc/BatchCallRpcHandlerFunc for hook context when Call/Batchcall +> Note: It's not compatiable with previous version + V1.1.5 ------------ - support setting max connection number for client with http(s) protocal diff --git a/client.go b/client.go index cceee6c..d271fde 100644 --- a/client.go +++ b/client.go @@ -6,7 +6,6 @@ package sdk import ( "context" - "net/url" "sync/atomic" "math/big" @@ -19,8 +18,9 @@ import ( "github.com/Conflux-Chain/go-conflux-sdk/types/cfxaddress" sdkerrors "github.com/Conflux-Chain/go-conflux-sdk/types/errors" postypes "github.com/Conflux-Chain/go-conflux-sdk/types/pos" + "github.com/mcuadros/go-defaults" rpc "github.com/openweb3/go-rpc-provider" - "github.com/valyala/fasthttp" + providers "github.com/openweb3/go-rpc-provider/provider_wrapper" "github.com/Conflux-Chain/go-conflux-sdk/utils" "github.com/ethereum/go-ethereum/accounts/abi" @@ -53,11 +53,11 @@ type ClientOption struct { KeystorePath string // retry RetryCount int - RetryInterval time.Duration + RetryInterval time.Duration `default:"1s"` // timeout of request - RequestTimeout time.Duration + RequestTimeout time.Duration `default:"30s"` // Maximum number of connections may be established. The default value is 512. It's only useful for http(s) prococal - MaxConnectionNum int + MaxConnectionPerHost int } // NewClient creates an instance of Client with specified conflux node url, it will creat account manager if option.KeystorePath not empty. @@ -90,7 +90,7 @@ func NewClient(nodeURL string, option ...ClientOption) (*Client, error) { realOption = option[0] } - client, err := newClientWithRetry(nodeURL, realOption) + client, err := newClientWithOption(nodeURL, realOption) if err != nil { return nil, errors.Wrap(err, "failed to new client with retry") } @@ -117,37 +117,25 @@ func NewClientWithRPCRequester(rpcRequester RpcRequester) (*Client, error) { // NewClientWithRetry creates a retryable new instance of Client with specified conflux node url and retry options. // // the clientOption.RetryInterval will be set to 1 second if pass 0 -func newClientWithRetry(nodeURL string, clientOption ClientOption) (*Client, error) { +func newClientWithOption(nodeURL string, clientOption ClientOption) (*Client, error) { var client Client + defaults.SetDefaults(&clientOption) + client.nodeURL = nodeURL client.option = clientOption - client.callRpcHandler = middleware.CallRpcHandlerFunc(client.callRpc) - client.batchCallRpcHandler = middleware.BatchCallRpcHandlerFunc(client.batchCallRPC) + client.callRpcHandler = middleware.CallRpcHandlerFunc(client.coreCallContext) + client.batchCallRpcHandler = middleware.BatchCallRpcHandlerFunc(client.coreBatchCallContext) + client.rpcPosClient = RpcPosClient{&client} client.rpcTxpoolClient = RpcTxpoolClient{&client} client.rpcDebugClient = RpcDebugClient{&client} - client.option.setDefault() - rpcClient, err := dialRpcClient(nodeURL, clientOption) + p, err := providers.NewProviderWithOption(nodeURL, *clientOption.genProviderOption()) if err != nil { - return nil, errors.Wrap(err, "failed to dial to fullnode") - } - - if client.option.RetryCount == 0 { - client.rpcRequester = rpcClient - } else { - client.rpcRequester = &rpcClientWithRetry{ - inner: rpcClient, - retryCount: client.option.RetryCount, - interval: client.option.RetryInterval, - } - } - - _, err = client.GetNetworkID() - if err != nil { - return nil, errors.Wrap(err, "failed to get networkID") + return nil, errors.Wrap(err, "failed to new provider") } + client.rpcRequester = p if client.option.KeystorePath != "" { am := NewAccountManager(client.option.KeystorePath, *client.networkID) @@ -162,25 +150,8 @@ func newClientWithRetry(nodeURL string, clientOption ClientOption) (*Client, err return &client, nil } -func dialRpcClient(nodeURL string, clientOption ClientOption) (*rpc.Client, error) { - if u, err := url.Parse(nodeURL); err == nil { - if (u.Scheme == "http" || u.Scheme == "https") && clientOption.MaxConnectionNum > 0 { - fasthttpClient := new(fasthttp.Client) - fasthttpClient.MaxConnsPerHost = clientOption.MaxConnectionNum - return rpc.DialHTTPWithClient(nodeURL, fasthttpClient) - } - } - return rpc.Dial(nodeURL) -} - -func (co *ClientOption) setDefault() { - if co.RequestTimeout == 0 { - co.RequestTimeout = time.Second * 30 - } - // Interval 0 is meaningless and may lead full node busy, so default sets it to 1 second - if co.RetryInterval == 0 { - co.RetryInterval = time.Second - } +func (client *Client) Provider() RpcRequester { + return client.rpcRequester } // Pos returns RpcPosClient for invoke rpc with pos namespace @@ -235,14 +206,10 @@ func (client *Client) MustNewAddress(base32OrHex string) types.Address { // // You could use UseCallRpcMiddleware to add middleware for hooking CallRPC func (client *Client) CallRPC(result interface{}, method string, args ...interface{}) error { - return client.callRpcHandler.Handle(result, method, args...) + return client.callRpcHandler.Handle(context.Background(), result, method, args...) } -func (client *Client) callRpc(result interface{}, method string, args ...interface{}) error { - ctx, cancelFunc := client.genContext() - if cancelFunc != nil { - defer cancelFunc() - } +func (client *Client) coreCallContext(ctx context.Context, result interface{}, method string, args ...interface{}) error { return client.rpcRequester.CallContext(ctx, result, method, args...) } @@ -262,15 +229,10 @@ func (client *Client) UseCallRpcMiddleware(middleware middleware.CallRpcMiddlewa // // You could use UseBatchCallRpcMiddleware to add middleware for hooking BatchCallRPC func (client *Client) BatchCallRPC(b []rpc.BatchElem) error { - return client.batchCallRpcHandler.Handle(b) + return client.batchCallRpcHandler.Handle(context.Background(), b) } -func (client *Client) batchCallRPC(b []rpc.BatchElem) error { - ctx, cancelFunc := client.genContext() - if cancelFunc != nil { - defer cancelFunc() - } - +func (client *Client) coreBatchCallContext(ctx context.Context, b []rpc.BatchElem) error { err := client.rpcRequester.BatchCallContext(ctx, b) if err != nil { return err @@ -1369,9 +1331,11 @@ func get1stU64Ify(values []hexutil.Uint64) *hexutil.Uint64 { return nil } -func (client *Client) genContext() (context.Context, context.CancelFunc) { - if client.option.RequestTimeout > 0 { - return context.WithTimeout(context.Background(), client.option.RequestTimeout) +func (c *ClientOption) genProviderOption() *providers.Option { + return &providers.Option{ + RequestTimeout: c.RequestTimeout, + RetryCount: c.RetryCount, + RetryInterval: c.RetryInterval, + MaxConnectionPerHost: c.MaxConnectionPerHost, } - return context.Background(), nil } diff --git a/client_retry.go b/client_retry.go deleted file mode 100644 index e542f93..0000000 --- a/client_retry.go +++ /dev/null @@ -1,86 +0,0 @@ -package sdk - -import ( - "context" - "time" - - rpc "github.com/openweb3/go-rpc-provider" - "github.com/openweb3/go-rpc-provider/utils" - "github.com/pkg/errors" -) - -type rpcClientWithRetry struct { - inner *rpc.Client - retryCount int - interval time.Duration -} - -func (r *rpcClientWithRetry) Call(resultPtr interface{}, method string, args ...interface{}) error { - return r.CallContext(context.Background(), resultPtr, method, args...) -} - -func (r *rpcClientWithRetry) CallContext(ctx context.Context, resultPtr interface{}, method string, args ...interface{}) error { - remain := r.retryCount - for { - - err := r.inner.CallContext(ctx, resultPtr, method, args...) - if err == nil { - return nil - } - - if utils.IsRPCJSONError(err) { - return err - } - - remain-- - // fmt.Printf("remain retry count: %v\n", remain) - if remain == 0 { - return errors.Wrap(err, "rpc call timeout") - } - - if r.interval > 0 { - time.Sleep(r.interval) - } - } -} - -func (r *rpcClientWithRetry) BatchCall(b []rpc.BatchElem) error { - return r.BatchCallContext(context.Background(), b) -} - -func (r *rpcClientWithRetry) BatchCallContext(ctx context.Context, b []rpc.BatchElem) error { - err := r.inner.BatchCallContext(ctx, b) - if err == nil { - return nil - } - - if r.retryCount <= 0 { - return err - } - - remain := r.retryCount - for { - if err = r.inner.BatchCallContext(ctx, b); err == nil { - return nil - } - - remain-- - if remain == 0 { - return errors.Wrap(err, "batch rpc call timeout") - } - - if r.interval > 0 { - time.Sleep(r.interval) - } - } -} - -func (r *rpcClientWithRetry) Subscribe(ctx context.Context, namespace string, channel interface{}, args ...interface{}) (*rpc.ClientSubscription, error) { - return r.inner.Subscribe(ctx, namespace, channel, args...) -} - -func (r *rpcClientWithRetry) Close() { - if r != nil && r.inner != nil { - r.inner.Close() - } -} diff --git a/client_test.go b/client_test.go index fdb8944..a9d8432 100644 --- a/client_test.go +++ b/client_test.go @@ -9,12 +9,15 @@ package sdk // When rpc dail success // Return client instance import ( + "context" + "fmt" "testing" . "bou.ke/monkey" // "github.com/ethereum/go-ethereum/rpc" rpc "github.com/openweb3/go-rpc-provider" + providers "github.com/openweb3/go-rpc-provider/provider_wrapper" "github.com/pkg/errors" . "github.com/smartystreets/goconvey/convey" ) @@ -32,7 +35,7 @@ func _TestNewClient(t *testing.T) { }) defer guard.Unpatch() - client, err := newClientWithRetry("", ClientOption{}) + client, err := newClientWithOption("", ClientOption{}) Convey("Return error", func() { So(err, ShouldNotEqual, nil) So(client, ShouldEqual, nil) @@ -46,18 +49,15 @@ func _TestNewClient(t *testing.T) { }) defer guard.Unpatch() - client, err := newClientWithRetry("", ClientOption{}) + client, err := newClientWithOption("", ClientOption{}) // fmt.Printf("client:%+v,err:%+v", client, err) Convey("Return client instance", func() { So(err, ShouldEqual, nil) So(client, ShouldNotEqual, nil) }) - }) - }) - }) } @@ -65,3 +65,25 @@ func _TestNewClient(t *testing.T) { func TestInterfaceImplementation(t *testing.T) { var _ ClientOperator = &Client{} } + +func TestClientHookCallContext(t *testing.T) { + c := MustNewClient("https://test.confluxrpc.com") + mp := c.Provider().(*providers.MiddlewarableProvider) + mp.HookCallContext(callContextMid1) + mp.HookCallContext(callContextMid2) + c.GetStatus() +} + +func callContextMid1(f providers.CallContextFunc) providers.CallContextFunc { + return func(ctx context.Context, result interface{}, method string, args ...interface{}) error { + ctx = context.WithValue(ctx, "foo", "bar") + return f(ctx, result, method, args...) + } +} + +func callContextMid2(f providers.CallContextFunc) providers.CallContextFunc { + return func(ctx context.Context, result interface{}, method string, args ...interface{}) error { + fmt.Printf("ctx value of foo: %+v\n", ctx.Value("foo")) + return f(ctx, result, method, args...) + } +} diff --git a/go.mod b/go.mod index ae95cc6..00e52a2 100644 --- a/go.mod +++ b/go.mod @@ -8,13 +8,13 @@ require ( github.com/fatih/color v1.7.0 github.com/graph-gophers/graphql-go v1.3.0 // indirect github.com/jackpal/go-nat-pmp v1.0.2 // indirect - github.com/openweb3/go-rpc-provider v0.1.2 - github.com/openweb3/go-sdk-common v0.0.0-20220407083459-597b845413e8 + github.com/mcuadros/go-defaults v1.2.0 + github.com/openweb3/go-rpc-provider v0.2.0 + github.com/openweb3/go-sdk-common v0.0.0-20220524083215-d22d44765e44 github.com/pkg/errors v0.9.1 github.com/shopspring/decimal v1.3.1 github.com/smartystreets/goconvey v1.6.4 github.com/stretchr/testify v1.7.0 - github.com/valyala/fasthttp v1.33.0 gopkg.in/urfave/cli.v1 v1.20.0 gotest.tools v2.2.0+incompatible @@ -22,3 +22,4 @@ require ( // replace github.com/openweb3/go-sdk-common => ../go-sdk-common // replace github.com/ethereum/go-ethereum => ../../ethereum/go-ethereum +// replace github.com/openweb3/go-rpc-provider v0.1.3 => ../go-rpc-provider diff --git a/go.sum b/go.sum index 9a26faf..7c29187 100644 --- a/go.sum +++ b/go.sum @@ -299,6 +299,8 @@ github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m github.com/mattn/go-sqlite3 v1.11.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= github.com/mattn/go-tty v0.0.0-20180907095812-13ff1204f104/go.mod h1:XPvLUNfbS4fJH25nqRHfWLMa1ONC8Amw+mIA639KxkE= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= +github.com/mcuadros/go-defaults v1.2.0 h1:FODb8WSf0uGaY8elWJAkoLL0Ri6AlZ1bFlenk56oZtc= +github.com/mcuadros/go-defaults v1.2.0/go.mod h1:WEZtHEVIGYVDqkKSWBdWKUVdRyKlMfulPaGDWIVeCWY= github.com/mitchellh/mapstructure v1.4.1 h1:CpVNEelQCZBooIPDn+AR3NpivK/TIKU8bDxdASFVQag= github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/mitchellh/pointerstructure v1.2.0 h1:O+i9nHnXS3l/9Wu7r4NrEdwA2VFTicjUEN1uBnDo34A= @@ -309,6 +311,7 @@ github.com/mschoch/smat v0.0.0-20160514031455-90eadee771ae/go.mod h1:qAyveg+e4CE github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/naoina/go-stringutil v0.1.0/go.mod h1:XJ2SJL9jCtBh+P9q5btrd/Ylo8XwT/h1USek5+NqSA0= github.com/naoina/toml v0.1.2-0.20170918210437-9fafd6967416/go.mod h1:NBIhNtsFMo3G2szEBne+bO4gS192HuIYRqfvOWb4i1E= +github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= github.com/nxadm/tail v1.4.4 h1:DQuhQpB1tVlglWS2hLQ5OV6B5r8aGxSrPc5Qo6uTN78= github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U= @@ -327,10 +330,10 @@ github.com/opentracing/opentracing-go v1.0.2/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFSt github.com/opentracing/opentracing-go v1.0.3-0.20180606204148-bd9c31933947/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= github.com/opentracing/opentracing-go v1.1.0 h1:pWlfV3Bxv7k65HYwkikxat0+s3pV4bsqf19k25Ur8rU= github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= -github.com/openweb3/go-rpc-provider v0.1.2 h1:SnWWsPIc2ecXnxVkMK3wD/1SpDNJynLIHAEnL79HfvU= -github.com/openweb3/go-rpc-provider v0.1.2/go.mod h1:6rDg4znRmz1CygAxCMLRSBh/HQrf+2jo7DNk4hgvGN8= -github.com/openweb3/go-sdk-common v0.0.0-20220407083459-597b845413e8 h1:8OnulYIJyaZ63M4O+hFk9tEeKboVqrgNyY7HJyBWARU= -github.com/openweb3/go-sdk-common v0.0.0-20220407083459-597b845413e8/go.mod h1:/M7gnCteccqnoBJMBmwTXkj7akVzQIMcD8y6DFX5GmE= +github.com/openweb3/go-rpc-provider v0.2.0 h1:Avx1VugXF4Je7eoqR3aYrXIup8sIUmj6zu8y0NZF8r8= +github.com/openweb3/go-rpc-provider v0.2.0/go.mod h1:eULpoK5s1rYV0I8lXXcNRa6r1mZQMHL4OAW2gxpn8xY= +github.com/openweb3/go-sdk-common v0.0.0-20220524083215-d22d44765e44 h1:OmFM0oP0gg43Dus81HaRcGJAuKtGBpeek98+pFH1y0U= +github.com/openweb3/go-sdk-common v0.0.0-20220524083215-d22d44765e44/go.mod h1:/M7gnCteccqnoBJMBmwTXkj7akVzQIMcD8y6DFX5GmE= github.com/paulbellamy/ratecounter v0.2.0/go.mod h1:Hfx1hDpSGoqxkVVpBi/IlYD7kChlfo5C6hzIHwPqfFE= github.com/peterh/liner v1.0.1-0.20180619022028-8c1271fcf47f/go.mod h1:xIteQHvHuaLYG9IFj6mSxM0fCKrs34IrEQUhOYuGPHc= github.com/peterh/liner v1.1.1-0.20190123174540-a2c9a5303de7 h1:oYW+YCJ1pachXTQmzR3rNLYGGz4g/UgFcjb28p/viDM= @@ -634,6 +637,7 @@ google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2 gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= diff --git a/interface.go b/interface.go index 4c9580b..55e5d74 100644 --- a/interface.go +++ b/interface.go @@ -5,7 +5,6 @@ package sdk import ( - "context" "math/big" "net/http" "time" @@ -15,6 +14,7 @@ import ( postypes "github.com/Conflux-Chain/go-conflux-sdk/types/pos" "github.com/ethereum/go-ethereum/common/hexutil" rpc "github.com/openweb3/go-rpc-provider" + "github.com/openweb3/go-rpc-provider/interfaces" ) // HTTPRequester is interface for emitting a http requester @@ -188,11 +188,5 @@ type AccountManagerOperator interface { Sign(tx types.UnsignedTransaction, passphrase string) (v byte, r, s []byte, err error) } -type RpcRequester interface { - Call(resultPtr interface{}, method string, args ...interface{}) error - CallContext(ctx context.Context, result interface{}, method string, args ...interface{}) error - BatchCall(b []rpc.BatchElem) error - BatchCallContext(ctx context.Context, b []rpc.BatchElem) error - Subscribe(ctx context.Context, namespace string, channel interface{}, args ...interface{}) (*rpc.ClientSubscription, error) - Close() -} +// reserve for forward compatbility +type RpcRequester = interfaces.Provider diff --git a/middleware/call_rpc.go b/middleware/call_rpc.go index ed2a73c..25562a6 100644 --- a/middleware/call_rpc.go +++ b/middleware/call_rpc.go @@ -1,6 +1,7 @@ package middleware import ( + "context" "encoding/json" "fmt" @@ -8,25 +9,26 @@ import ( "github.com/Conflux-Chain/go-conflux-sdk/utils" "github.com/fatih/color" + providers "github.com/openweb3/go-rpc-provider/provider_wrapper" ) // CallRpcHandler represents interface of call rpc handler type CallRpcHandler interface { - Handle(result interface{}, method string, args ...interface{}) error + Handle(ctx context.Context, result interface{}, method string, args ...interface{}) error } -type CallRpcHandlerFunc func(result interface{}, method string, args ...interface{}) error +type CallRpcHandlerFunc providers.CallContextFunc // CallRpcMiddleware represents the middleware for call rpc type CallRpcMiddleware func(CallRpcHandler) CallRpcHandler -func (c CallRpcHandlerFunc) Handle(result interface{}, method string, args ...interface{}) error { - return c(result, method, args...) +func (c CallRpcHandlerFunc) Handle(ctx context.Context, result interface{}, method string, args ...interface{}) error { + return c(ctx, result, method, args...) } // CallRpcConsoleMiddleware is the middleware for console request and response when call rpc func CallRpcConsoleMiddleware(handler CallRpcHandler) CallRpcHandler { - logFn := func(result interface{}, method string, args ...interface{}) error { + logFn := func(ctx context.Context, result interface{}, method string, args ...interface{}) error { argsStr := fmt.Sprintf("%+v", args) argsJson, err := json.Marshal(args) @@ -35,7 +37,7 @@ func CallRpcConsoleMiddleware(handler CallRpcHandler) CallRpcHandler { } start := time.Now() - err = handler.Handle(result, method, args...) + err = handler.Handle(ctx, result, method, args...) duration := time.Since(start) if err == nil { diff --git a/middleware/call_rpc_batch.go b/middleware/call_rpc_batch.go index faaf985..2f0cc0d 100644 --- a/middleware/call_rpc_batch.go +++ b/middleware/call_rpc_batch.go @@ -1,34 +1,36 @@ package middleware import ( + "context" "fmt" "time" "github.com/Conflux-Chain/go-conflux-sdk/utils" "github.com/fatih/color" rpc "github.com/openweb3/go-rpc-provider" + providers "github.com/openweb3/go-rpc-provider/provider_wrapper" ) // BatchCallRpcHandler represents interface of batch call rpc handler type BatchCallRpcHandler interface { - Handle(b []rpc.BatchElem) error + Handle(ctx context.Context, b []rpc.BatchElem) error } -type BatchCallRpcHandlerFunc func(b []rpc.BatchElem) error +type BatchCallRpcHandlerFunc providers.BatchCallContextFunc // BatchCallRpcMiddleware represents the middleware for batch call rpc type BatchCallRpcMiddleware func(BatchCallRpcHandler) BatchCallRpcHandler -func (brh BatchCallRpcHandlerFunc) Handle(b []rpc.BatchElem) error { - return brh(b) +func (brh BatchCallRpcHandlerFunc) Handle(ctx context.Context, b []rpc.BatchElem) error { + return brh(ctx, b) } // BatchCallRpcConsoleMiddleware is the middleware for console request and response when batch call rpc -func BatchCallRpcConsoleMiddleware(handler BatchCallRpcHandler) BatchCallRpcHandler { - logFn := func(b []rpc.BatchElem) error { +func BatchCallRpcConsoleMiddleware(ctx context.Context, handler BatchCallRpcHandler) BatchCallRpcHandler { + logFn := func(ctx context.Context, b []rpc.BatchElem) error { start := time.Now() - err := handler.Handle(b) + err := handler.Handle(ctx, b) duration := time.Since(start) if err == nil {