From 0d61e0d717247ae5da87709696b2addfe6142634 Mon Sep 17 00:00:00 2001 From: frrist Date: Fri, 21 Aug 2020 20:22:40 -0700 Subject: [PATCH] refactor(ctx): update to latest go-jsonrpc --- api/client/client.go | 17 +++++++++-------- cli/cmd.go | 6 +++--- cmd/lotus-pcr/main.go | 2 +- cmd/lotus-stats/main.go | 2 +- go.mod | 2 +- go.sum | 4 ++-- lib/rpcenc/reader_test.go | 4 ++-- lotuspond/api.go | 5 +++-- lotuspond/main.go | 4 ++-- node/impl/remoteworker.go | 2 +- node/test/builder.go | 4 ++-- tools/stats/rpc.go | 4 ++-- 12 files changed, 29 insertions(+), 27 deletions(-) diff --git a/api/client/client.go b/api/client/client.go index b55d6bca4..cd915acf0 100644 --- a/api/client/client.go +++ b/api/client/client.go @@ -1,6 +1,7 @@ package client import ( + "context" "net/http" "net/url" "path" @@ -14,9 +15,9 @@ import ( ) // NewCommonRPC creates a new http jsonrpc client. -func NewCommonRPC(addr string, requestHeader http.Header) (api.Common, jsonrpc.ClientCloser, error) { +func NewCommonRPC(ctx context.Context, addr string, requestHeader http.Header) (api.Common, jsonrpc.ClientCloser, error) { var res apistruct.CommonStruct - closer, err := jsonrpc.NewMergeClient(addr, "Filecoin", + closer, err := jsonrpc.NewMergeClient(ctx, addr, "Filecoin", []interface{}{ &res.Internal, }, @@ -27,9 +28,9 @@ func NewCommonRPC(addr string, requestHeader http.Header) (api.Common, jsonrpc.C } // NewFullNodeRPC creates a new http jsonrpc client. -func NewFullNodeRPC(addr string, requestHeader http.Header) (api.FullNode, jsonrpc.ClientCloser, error) { +func NewFullNodeRPC(ctx context.Context, addr string, requestHeader http.Header) (api.FullNode, jsonrpc.ClientCloser, error) { var res apistruct.FullNodeStruct - closer, err := jsonrpc.NewMergeClient(addr, "Filecoin", + closer, err := jsonrpc.NewMergeClient(ctx, addr, "Filecoin", []interface{}{ &res.CommonStruct.Internal, &res.Internal, @@ -39,9 +40,9 @@ func NewFullNodeRPC(addr string, requestHeader http.Header) (api.FullNode, jsonr } // NewStorageMinerRPC creates a new http jsonrpc client for miner -func NewStorageMinerRPC(addr string, requestHeader http.Header, opts ...jsonrpc.Option) (api.StorageMiner, jsonrpc.ClientCloser, error) { +func NewStorageMinerRPC(ctx context.Context, addr string, requestHeader http.Header, opts ...jsonrpc.Option) (api.StorageMiner, jsonrpc.ClientCloser, error) { var res apistruct.StorageMinerStruct - closer, err := jsonrpc.NewMergeClient(addr, "Filecoin", + closer, err := jsonrpc.NewMergeClient(ctx, addr, "Filecoin", []interface{}{ &res.CommonStruct.Internal, &res.Internal, @@ -53,7 +54,7 @@ func NewStorageMinerRPC(addr string, requestHeader http.Header, opts ...jsonrpc. return &res, closer, err } -func NewWorkerRPC(addr string, requestHeader http.Header) (api.WorkerAPI, jsonrpc.ClientCloser, error) { +func NewWorkerRPC(ctx context.Context, addr string, requestHeader http.Header) (api.WorkerAPI, jsonrpc.ClientCloser, error) { u, err := url.Parse(addr) if err != nil { return nil, nil, err @@ -69,7 +70,7 @@ func NewWorkerRPC(addr string, requestHeader http.Header) (api.WorkerAPI, jsonrp u.Path = path.Join(u.Path, "../streams/v0/push") var res apistruct.WorkerStruct - closer, err := jsonrpc.NewMergeClient(addr, "Filecoin", + closer, err := jsonrpc.NewMergeClient(ctx, addr, "Filecoin", []interface{}{ &res.Internal, }, diff --git a/cli/cmd.go b/cli/cmd.go index 265f6ee4c..c00b4925e 100644 --- a/cli/cmd.go +++ b/cli/cmd.go @@ -213,7 +213,7 @@ func GetAPI(ctx *cli.Context) (api.Common, jsonrpc.ClientCloser, error) { return nil, nil, err } - return client.NewCommonRPC(addr, headers) + return client.NewCommonRPC(ctx.Context, addr, headers) } func GetFullNodeAPI(ctx *cli.Context) (api.FullNode, jsonrpc.ClientCloser, error) { @@ -222,7 +222,7 @@ func GetFullNodeAPI(ctx *cli.Context) (api.FullNode, jsonrpc.ClientCloser, error return nil, nil, err } - return client.NewFullNodeRPC(addr, headers) + return client.NewFullNodeRPC(ctx.Context, addr, headers) } func GetStorageMinerAPI(ctx *cli.Context, opts ...jsonrpc.Option) (api.StorageMiner, jsonrpc.ClientCloser, error) { @@ -231,7 +231,7 @@ func GetStorageMinerAPI(ctx *cli.Context, opts ...jsonrpc.Option) (api.StorageMi return nil, nil, err } - return client.NewStorageMinerRPC(addr, headers, opts...) + return client.NewStorageMinerRPC(ctx.Context, addr, headers, opts...) } func DaemonContext(cctx *cli.Context) context.Context { diff --git a/cmd/lotus-pcr/main.go b/cmd/lotus-pcr/main.go index d86eb60e9..ec1b166db 100644 --- a/cmd/lotus-pcr/main.go +++ b/cmd/lotus-pcr/main.go @@ -146,7 +146,7 @@ var runCmd = &cli.Command{ }() ctx := context.Background() - api, closer, err := stats.GetFullNodeAPI(cctx.String("lotus-path")) + api, closer, err := stats.GetFullNodeAPI(cctx.Context, cctx.String("lotus-path")) if err != nil { log.Fatal(err) } diff --git a/cmd/lotus-stats/main.go b/cmd/lotus-stats/main.go index ddc33b7ac..3ca139b7d 100644 --- a/cmd/lotus-stats/main.go +++ b/cmd/lotus-stats/main.go @@ -56,7 +56,7 @@ func main() { height = h } - api, closer, err := stats.GetFullNodeAPI(repo) + api, closer, err := stats.GetFullNodeAPI(ctx, repo) if err != nil { log.Fatal(err) } diff --git a/go.mod b/go.mod index ab9ecb083..7358c61f1 100644 --- a/go.mod +++ b/go.mod @@ -31,7 +31,7 @@ require ( github.com/filecoin-project/go-data-transfer v0.6.2 github.com/filecoin-project/go-fil-commcid v0.0.0-20200716160307-8f644712406f github.com/filecoin-project/go-fil-markets v0.5.7 - github.com/filecoin-project/go-jsonrpc v0.1.2-0.20200817153016-2ea5cbaf5ec0 + github.com/filecoin-project/go-jsonrpc v0.1.2-0.20200822201400-474f4fdccc52 github.com/filecoin-project/go-multistore v0.0.3 github.com/filecoin-project/go-padreader v0.0.0-20200210211231-548257017ca6 github.com/filecoin-project/go-paramfetch v0.0.2-0.20200701152213-3e0f0afdc261 diff --git a/go.sum b/go.sum index dff6be3e9..d4bb088c7 100644 --- a/go.sum +++ b/go.sum @@ -244,8 +244,8 @@ github.com/filecoin-project/go-fil-commcid v0.0.0-20200716160307-8f644712406f h1 github.com/filecoin-project/go-fil-commcid v0.0.0-20200716160307-8f644712406f/go.mod h1:Eaox7Hvus1JgPrL5+M3+h7aSPHc0cVqpSxA+TxIEpZQ= github.com/filecoin-project/go-fil-markets v0.5.7 h1:kzyMHqez8ssxchj5s9M1hkC3CTwRGh2MeglJGfUksQU= github.com/filecoin-project/go-fil-markets v0.5.7/go.mod h1:KnvFG3kSQ77vKYSY/QdrXET81wVCBByHXjG7AyxnbUw= -github.com/filecoin-project/go-jsonrpc v0.1.2-0.20200817153016-2ea5cbaf5ec0 h1:/GT3V+3f+H5w5odb7LcCWJ1zPw8H8m9TsGQcU0cGSHo= -github.com/filecoin-project/go-jsonrpc v0.1.2-0.20200817153016-2ea5cbaf5ec0/go.mod h1:XBBpuKIMaXIIzeqzO1iucq4GvbF8CxmXRFoezRh+Cx4= +github.com/filecoin-project/go-jsonrpc v0.1.2-0.20200822201400-474f4fdccc52 h1:FXtCp0ybqdQL9knb3OGDpkNTaBbPxgkqPeWKotUwkH0= +github.com/filecoin-project/go-jsonrpc v0.1.2-0.20200822201400-474f4fdccc52/go.mod h1:XBBpuKIMaXIIzeqzO1iucq4GvbF8CxmXRFoezRh+Cx4= github.com/filecoin-project/go-multistore v0.0.3 h1:vaRBY4YiA2UZFPK57RNuewypB8u0DzzQwqsL0XarpnI= github.com/filecoin-project/go-multistore v0.0.3/go.mod h1:kaNqCC4IhU4B1uyr7YWFHd23TL4KM32aChS0jNkyUvQ= github.com/filecoin-project/go-padreader v0.0.0-20200210211231-548257017ca6 h1:92PET+sx1Hb4W/8CgFwGuxaKbttwY+UNspYZTvXY0vs= diff --git a/lib/rpcenc/reader_test.go b/lib/rpcenc/reader_test.go index d6289c150..3aef2bb54 100644 --- a/lib/rpcenc/reader_test.go +++ b/lib/rpcenc/reader_test.go @@ -49,7 +49,7 @@ func TestReaderProxy(t *testing.T) { defer testServ.Close() re := ReaderParamEncoder("http://" + testServ.Listener.Addr().String() + "/rpc/streams/v0/push") - closer, err := jsonrpc.NewMergeClient("ws://"+testServ.Listener.Addr().String()+"/rpc/v0", "ReaderHandler", []interface{}{&client}, nil, re) + closer, err := jsonrpc.NewMergeClient(context.Background(), "ws://"+testServ.Listener.Addr().String()+"/rpc/v0", "ReaderHandler", []interface{}{&client}, nil, re) require.NoError(t, err) defer closer() @@ -79,7 +79,7 @@ func TestNullReaderProxy(t *testing.T) { defer testServ.Close() re := ReaderParamEncoder("http://" + testServ.Listener.Addr().String() + "/rpc/streams/v0/push") - closer, err := jsonrpc.NewMergeClient("ws://"+testServ.Listener.Addr().String()+"/rpc/v0", "ReaderHandler", []interface{}{&client}, nil, re) + closer, err := jsonrpc.NewMergeClient(context.Background(), "ws://"+testServ.Listener.Addr().String()+"/rpc/v0", "ReaderHandler", []interface{}{&client}, nil, re) require.NoError(t, err) defer closer() diff --git a/lotuspond/api.go b/lotuspond/api.go index e61372a87..a02357919 100644 --- a/lotuspond/api.go +++ b/lotuspond/api.go @@ -1,6 +1,7 @@ package main import ( + "context" "crypto/rand" "io" "io/ioutil" @@ -129,9 +130,9 @@ type client struct { Nodes func() []nodeInfo } -func apiClient() (*client, error) { +func apiClient(ctx context.Context) (*client, error) { c := &client{} - if _, err := jsonrpc.NewClient("ws://"+listenAddr+"/rpc/v0", "Pond", c, nil); err != nil { + if _, err := jsonrpc.NewClient(ctx, "ws://"+listenAddr+"/rpc/v0", "Pond", c, nil); err != nil { return nil, err } return c, nil diff --git a/lotuspond/main.go b/lotuspond/main.go index 1df35f4ba..ec01e8b42 100644 --- a/lotuspond/main.go +++ b/lotuspond/main.go @@ -27,7 +27,7 @@ var onCmd = &cli.Command{ Name: "on", Usage: "run a command on a given node", Action: func(cctx *cli.Context) error { - client, err := apiClient() + client, err := apiClient(cctx.Context) if err != nil { return err } @@ -65,7 +65,7 @@ var shCmd = &cli.Command{ Name: "sh", Usage: "spawn shell with node shell variables set", Action: func(cctx *cli.Context) error { - client, err := apiClient() + client, err := apiClient(cctx.Context) if err != nil { return err } diff --git a/node/impl/remoteworker.go b/node/impl/remoteworker.go index f46f7206a..8111413ba 100644 --- a/node/impl/remoteworker.go +++ b/node/impl/remoteworker.go @@ -33,7 +33,7 @@ func connectRemoteWorker(ctx context.Context, fa api.Common, url string) (*remot headers := http.Header{} headers.Add("Authorization", "Bearer "+string(token)) - wapi, closer, err := client.NewWorkerRPC(url, headers) + wapi, closer, err := client.NewWorkerRPC(context.TODO(), url, headers) if err != nil { return nil, xerrors.Errorf("creating jsonrpc client: %w", err) } diff --git a/node/test/builder.go b/node/test/builder.go index b98ed6b15..6fbec16f9 100644 --- a/node/test/builder.go +++ b/node/test/builder.go @@ -453,7 +453,7 @@ func rpcWithBuilder(t *testing.T, b test.APIBuilder, nFull int, storage []test.S addr := testServ.Listener.Addr() listenAddr := "ws://" + addr.String() var err error - fulls[i].FullNode, _, err = client.NewFullNodeRPC(listenAddr, nil) + fulls[i].FullNode, _, err = client.NewFullNodeRPC(context.Background(), listenAddr, nil) if err != nil { t.Fatal(err) } @@ -472,7 +472,7 @@ func rpcWithBuilder(t *testing.T, b test.APIBuilder, nFull int, storage []test.S addr := testServ.Listener.Addr() listenAddr := "ws://" + addr.String() var err error - storers[i].StorageMiner, _, err = client.NewStorageMinerRPC(listenAddr, nil) + storers[i].StorageMiner, _, err = client.NewStorageMinerRPC(context.Background(), listenAddr, nil) if err != nil { t.Fatal(err) } diff --git a/tools/stats/rpc.go b/tools/stats/rpc.go index 965005f0d..503476b24 100644 --- a/tools/stats/rpc.go +++ b/tools/stats/rpc.go @@ -215,11 +215,11 @@ func loadTipsets(ctx context.Context, api api.FullNode, curr *types.TipSet, lowe return tipsets, nil } -func GetFullNodeAPI(repo string) (api.FullNode, jsonrpc.ClientCloser, error) { +func GetFullNodeAPI(ctx context.Context, repo string) (api.FullNode, jsonrpc.ClientCloser, error) { addr, headers, err := getAPI(repo) if err != nil { return nil, nil, err } - return client.NewFullNodeRPC(addr, headers) + return client.NewFullNodeRPC(ctx, addr, headers) }