refactor(ctx): update to latest go-jsonrpc

This commit is contained in:
frrist 2020-08-21 20:22:40 -07:00
parent 14cda27e9f
commit 0d61e0d717
12 changed files with 29 additions and 27 deletions

View File

@ -1,6 +1,7 @@
package client package client
import ( import (
"context"
"net/http" "net/http"
"net/url" "net/url"
"path" "path"
@ -14,9 +15,9 @@ import (
) )
// NewCommonRPC creates a new http jsonrpc client. // 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 var res apistruct.CommonStruct
closer, err := jsonrpc.NewMergeClient(addr, "Filecoin", closer, err := jsonrpc.NewMergeClient(ctx, addr, "Filecoin",
[]interface{}{ []interface{}{
&res.Internal, &res.Internal,
}, },
@ -27,9 +28,9 @@ func NewCommonRPC(addr string, requestHeader http.Header) (api.Common, jsonrpc.C
} }
// NewFullNodeRPC creates a new http jsonrpc client. // 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 var res apistruct.FullNodeStruct
closer, err := jsonrpc.NewMergeClient(addr, "Filecoin", closer, err := jsonrpc.NewMergeClient(ctx, addr, "Filecoin",
[]interface{}{ []interface{}{
&res.CommonStruct.Internal, &res.CommonStruct.Internal,
&res.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 // 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 var res apistruct.StorageMinerStruct
closer, err := jsonrpc.NewMergeClient(addr, "Filecoin", closer, err := jsonrpc.NewMergeClient(ctx, addr, "Filecoin",
[]interface{}{ []interface{}{
&res.CommonStruct.Internal, &res.CommonStruct.Internal,
&res.Internal, &res.Internal,
@ -53,7 +54,7 @@ func NewStorageMinerRPC(addr string, requestHeader http.Header, opts ...jsonrpc.
return &res, closer, err 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) u, err := url.Parse(addr)
if err != nil { if err != nil {
return nil, nil, err 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") u.Path = path.Join(u.Path, "../streams/v0/push")
var res apistruct.WorkerStruct var res apistruct.WorkerStruct
closer, err := jsonrpc.NewMergeClient(addr, "Filecoin", closer, err := jsonrpc.NewMergeClient(ctx, addr, "Filecoin",
[]interface{}{ []interface{}{
&res.Internal, &res.Internal,
}, },

View File

@ -213,7 +213,7 @@ func GetAPI(ctx *cli.Context) (api.Common, jsonrpc.ClientCloser, error) {
return nil, nil, err 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) { 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 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) { 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 nil, nil, err
} }
return client.NewStorageMinerRPC(addr, headers, opts...) return client.NewStorageMinerRPC(ctx.Context, addr, headers, opts...)
} }
func DaemonContext(cctx *cli.Context) context.Context { func DaemonContext(cctx *cli.Context) context.Context {

View File

@ -146,7 +146,7 @@ var runCmd = &cli.Command{
}() }()
ctx := context.Background() 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 { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

View File

@ -56,7 +56,7 @@ func main() {
height = h height = h
} }
api, closer, err := stats.GetFullNodeAPI(repo) api, closer, err := stats.GetFullNodeAPI(ctx, repo)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

2
go.mod
View File

@ -31,7 +31,7 @@ require (
github.com/filecoin-project/go-data-transfer v0.6.2 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-commcid v0.0.0-20200716160307-8f644712406f
github.com/filecoin-project/go-fil-markets v0.5.7 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-multistore v0.0.3
github.com/filecoin-project/go-padreader v0.0.0-20200210211231-548257017ca6 github.com/filecoin-project/go-padreader v0.0.0-20200210211231-548257017ca6
github.com/filecoin-project/go-paramfetch v0.0.2-0.20200701152213-3e0f0afdc261 github.com/filecoin-project/go-paramfetch v0.0.2-0.20200701152213-3e0f0afdc261

4
go.sum
View File

@ -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-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 h1:kzyMHqez8ssxchj5s9M1hkC3CTwRGh2MeglJGfUksQU=
github.com/filecoin-project/go-fil-markets v0.5.7/go.mod h1:KnvFG3kSQ77vKYSY/QdrXET81wVCBByHXjG7AyxnbUw= 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.20200822201400-474f4fdccc52 h1:FXtCp0ybqdQL9knb3OGDpkNTaBbPxgkqPeWKotUwkH0=
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/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 h1:vaRBY4YiA2UZFPK57RNuewypB8u0DzzQwqsL0XarpnI=
github.com/filecoin-project/go-multistore v0.0.3/go.mod h1:kaNqCC4IhU4B1uyr7YWFHd23TL4KM32aChS0jNkyUvQ= 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= github.com/filecoin-project/go-padreader v0.0.0-20200210211231-548257017ca6 h1:92PET+sx1Hb4W/8CgFwGuxaKbttwY+UNspYZTvXY0vs=

View File

@ -49,7 +49,7 @@ func TestReaderProxy(t *testing.T) {
defer testServ.Close() defer testServ.Close()
re := ReaderParamEncoder("http://" + testServ.Listener.Addr().String() + "/rpc/streams/v0/push") 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) require.NoError(t, err)
defer closer() defer closer()
@ -79,7 +79,7 @@ func TestNullReaderProxy(t *testing.T) {
defer testServ.Close() defer testServ.Close()
re := ReaderParamEncoder("http://" + testServ.Listener.Addr().String() + "/rpc/streams/v0/push") 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) require.NoError(t, err)
defer closer() defer closer()

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"context"
"crypto/rand" "crypto/rand"
"io" "io"
"io/ioutil" "io/ioutil"
@ -129,9 +130,9 @@ type client struct {
Nodes func() []nodeInfo Nodes func() []nodeInfo
} }
func apiClient() (*client, error) { func apiClient(ctx context.Context) (*client, error) {
c := &client{} 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 nil, err
} }
return c, nil return c, nil

View File

@ -27,7 +27,7 @@ var onCmd = &cli.Command{
Name: "on", Name: "on",
Usage: "run a command on a given node", Usage: "run a command on a given node",
Action: func(cctx *cli.Context) error { Action: func(cctx *cli.Context) error {
client, err := apiClient() client, err := apiClient(cctx.Context)
if err != nil { if err != nil {
return err return err
} }
@ -65,7 +65,7 @@ var shCmd = &cli.Command{
Name: "sh", Name: "sh",
Usage: "spawn shell with node shell variables set", Usage: "spawn shell with node shell variables set",
Action: func(cctx *cli.Context) error { Action: func(cctx *cli.Context) error {
client, err := apiClient() client, err := apiClient(cctx.Context)
if err != nil { if err != nil {
return err return err
} }

View File

@ -33,7 +33,7 @@ func connectRemoteWorker(ctx context.Context, fa api.Common, url string) (*remot
headers := http.Header{} headers := http.Header{}
headers.Add("Authorization", "Bearer "+string(token)) 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 { if err != nil {
return nil, xerrors.Errorf("creating jsonrpc client: %w", err) return nil, xerrors.Errorf("creating jsonrpc client: %w", err)
} }

View File

@ -453,7 +453,7 @@ func rpcWithBuilder(t *testing.T, b test.APIBuilder, nFull int, storage []test.S
addr := testServ.Listener.Addr() addr := testServ.Listener.Addr()
listenAddr := "ws://" + addr.String() listenAddr := "ws://" + addr.String()
var err error var err error
fulls[i].FullNode, _, err = client.NewFullNodeRPC(listenAddr, nil) fulls[i].FullNode, _, err = client.NewFullNodeRPC(context.Background(), listenAddr, nil)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -472,7 +472,7 @@ func rpcWithBuilder(t *testing.T, b test.APIBuilder, nFull int, storage []test.S
addr := testServ.Listener.Addr() addr := testServ.Listener.Addr()
listenAddr := "ws://" + addr.String() listenAddr := "ws://" + addr.String()
var err error var err error
storers[i].StorageMiner, _, err = client.NewStorageMinerRPC(listenAddr, nil) storers[i].StorageMiner, _, err = client.NewStorageMinerRPC(context.Background(), listenAddr, nil)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@ -215,11 +215,11 @@ func loadTipsets(ctx context.Context, api api.FullNode, curr *types.TipSet, lowe
return tipsets, nil 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) addr, headers, err := getAPI(repo)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
return client.NewFullNodeRPC(addr, headers) return client.NewFullNodeRPC(ctx, addr, headers)
} }