From 7a53e2a75c869da388c03cbf24c7aac24f55373d Mon Sep 17 00:00:00 2001 From: sebasti810 Date: Fri, 19 Jul 2024 14:20:06 +0200 Subject: [PATCH 1/8] fix: 3575 --- api/rpc/client/client.go | 34 +++++++++++++++++++++++++++++----- cmd/rpc.go | 18 ++++++++++++++++-- 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/api/rpc/client/client.go b/api/rpc/client/client.go index 56b4a54d19..b9e37cca15 100644 --- a/api/rpc/client/client.go +++ b/api/rpc/client/client.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "net/http" + "time" "github.com/filecoin-project/go-jsonrpc" @@ -41,6 +42,18 @@ type Client struct { closer multiClientCloser } +type ClientOption func(*clientConfig) + +type clientConfig struct { + timeout time.Duration +} + +func WithTimeout(timeout time.Duration) ClientOption { + return func(c *clientConfig) { + c.timeout = timeout + } +} + // multiClientCloser is a wrapper struct to close clients across multiple namespaces. type multiClientCloser struct { closers []jsonrpc.ClientCloser @@ -65,22 +78,33 @@ func (c *Client) Close() { // NewClient creates a new Client with one connection per namespace with the // given token as the authorization token. -func NewClient(ctx context.Context, addr, token string) (*Client, error) { +func NewClient(ctx context.Context, addr, token string, opts ...ClientOption) (*Client, error) { + config := &clientConfig{} + for _, opt := range opts { + opt(config) + } + authHeader := http.Header{perms.AuthKey: []string{fmt.Sprintf("Bearer %s", token)}} - return newClient(ctx, addr, authHeader) + return newClient(ctx, addr, authHeader, config) } -func newClient(ctx context.Context, addr string, authHeader http.Header) (*Client, error) { - var multiCloser multiClientCloser +func newClient(ctx context.Context, addr string, authHeader http.Header, config *clientConfig) (*Client, error) { var client Client + var multiCloser multiClientCloser + + httpClient := &http.Client{ + Timeout: config.timeout, + } + for name, module := range moduleMap(&client) { - closer, err := jsonrpc.NewClient(ctx, addr, name, module, authHeader) + closer, err := jsonrpc.NewMergeClient(ctx, addr, name, []interface{}{module}, authHeader, jsonrpc.WithHTTPClient(httpClient)) if err != nil { return nil, err } multiCloser.register(closer) } + client.closer = multiCloser return &client, nil } diff --git a/cmd/rpc.go b/cmd/rpc.go index 230b51508b..5e66325bbd 100644 --- a/cmd/rpc.go +++ b/cmd/rpc.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "path/filepath" + "time" "github.com/spf13/cobra" flag "github.com/spf13/pflag" @@ -18,6 +19,7 @@ import ( var ( requestURL string authTokenFlag string + timeoutFlag time.Duration ) func RPCFlags() *flag.FlagSet { @@ -37,13 +39,20 @@ func RPCFlags() *flag.FlagSet { "Authorization token", ) + fset.DurationVar( + &timeoutFlag, + "timeout", + 0, + "Timeout for RPC requests (e.g. 30s, 1m)", + ) + storeFlag := NodeFlags().Lookup(nodeStoreFlag) fset.AddFlag(storeFlag) return fset } func InitClient(cmd *cobra.Command, _ []string) error { - if authTokenFlag == "" { + if authTokenFlag == "" { rootErrMsg := "cant access the auth token" storePath, err := getStorePath(cmd) @@ -73,7 +82,12 @@ func InitClient(cmd *cobra.Command, _ []string) error { } } - client, err := rpc.NewClient(cmd.Context(), requestURL, authTokenFlag) + var opts []rpc.ClientOption + if timeoutFlag > 0 { + opts = append(opts, rpc.WithTimeout(timeoutFlag)) + } + + client, err := rpc.NewClient(cmd.Context(), requestURL, authTokenFlag, opts...) if err != nil { return err } From e18f3acc12ffc34ea80ea932bc3d6d8c2db961c9 Mon Sep 17 00:00:00 2001 From: sebasti810 Date: Mon, 22 Jul 2024 08:57:39 +0200 Subject: [PATCH 2/8] fix(rpc): resolve linter issues and improve code formatting --- api/rpc/client/client.go | 15 +++++++++++---- cmd/rpc.go | 14 +++++++------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/api/rpc/client/client.go b/api/rpc/client/client.go index b9e37cca15..29ad384b9e 100644 --- a/api/rpc/client/client.go +++ b/api/rpc/client/client.go @@ -42,13 +42,13 @@ type Client struct { closer multiClientCloser } -type ClientOption func(*clientConfig) +type RPCClientOption func(*clientConfig) type clientConfig struct { timeout time.Duration } -func WithTimeout(timeout time.Duration) ClientOption { +func WithTimeout(timeout time.Duration) RPCClientOption { return func(c *clientConfig) { c.timeout = timeout } @@ -78,7 +78,7 @@ func (c *Client) Close() { // NewClient creates a new Client with one connection per namespace with the // given token as the authorization token. -func NewClient(ctx context.Context, addr, token string, opts ...ClientOption) (*Client, error) { +func NewClient(ctx context.Context, addr, token string, opts ...RPCClientOption) (*Client, error) { config := &clientConfig{} for _, opt := range opts { opt(config) @@ -97,7 +97,14 @@ func newClient(ctx context.Context, addr string, authHeader http.Header, config } for name, module := range moduleMap(&client) { - closer, err := jsonrpc.NewMergeClient(ctx, addr, name, []interface{}{module}, authHeader, jsonrpc.WithHTTPClient(httpClient)) + closer, err := jsonrpc.NewMergeClient( + ctx, + addr, + name, + []interface{}{module}, + authHeader, + jsonrpc.WithHTTPClient(httpClient), + ) if err != nil { return nil, err } diff --git a/cmd/rpc.go b/cmd/rpc.go index 5e66325bbd..8991b41aed 100644 --- a/cmd/rpc.go +++ b/cmd/rpc.go @@ -40,11 +40,11 @@ func RPCFlags() *flag.FlagSet { ) fset.DurationVar( - &timeoutFlag, - "timeout", - 0, - "Timeout for RPC requests (e.g. 30s, 1m)", - ) + &timeoutFlag, + "timeout", + 0, + "Timeout for RPC requests (e.g. 30s, 1m)", + ) storeFlag := NodeFlags().Lookup(nodeStoreFlag) fset.AddFlag(storeFlag) @@ -52,7 +52,7 @@ func RPCFlags() *flag.FlagSet { } func InitClient(cmd *cobra.Command, _ []string) error { - if authTokenFlag == "" { + if authTokenFlag == "" { rootErrMsg := "cant access the auth token" storePath, err := getStorePath(cmd) @@ -82,7 +82,7 @@ func InitClient(cmd *cobra.Command, _ []string) error { } } - var opts []rpc.ClientOption + var opts []rpc.RPCClientOption if timeoutFlag > 0 { opts = append(opts, rpc.WithTimeout(timeoutFlag)) } From 2f62c98edba19b91ab12d3b58866a960d4e3cc33 Mon Sep 17 00:00:00 2001 From: sebasti810 Date: Fri, 6 Sep 2024 17:33:49 +0200 Subject: [PATCH 3/8] refactor(api): using context instead of an extra option --- api/rpc/client/client.go | 34 +++++----------------------------- cmd/rpc.go | 6 +++--- 2 files changed, 8 insertions(+), 32 deletions(-) diff --git a/api/rpc/client/client.go b/api/rpc/client/client.go index 29ad384b9e..7f1fb1a4c1 100644 --- a/api/rpc/client/client.go +++ b/api/rpc/client/client.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "net/http" - "time" "github.com/filecoin-project/go-jsonrpc" @@ -42,18 +41,6 @@ type Client struct { closer multiClientCloser } -type RPCClientOption func(*clientConfig) - -type clientConfig struct { - timeout time.Duration -} - -func WithTimeout(timeout time.Duration) RPCClientOption { - return func(c *clientConfig) { - c.timeout = timeout - } -} - // multiClientCloser is a wrapper struct to close clients across multiple namespaces. type multiClientCloser struct { closers []jsonrpc.ClientCloser @@ -78,32 +65,22 @@ func (c *Client) Close() { // NewClient creates a new Client with one connection per namespace with the // given token as the authorization token. -func NewClient(ctx context.Context, addr, token string, opts ...RPCClientOption) (*Client, error) { - config := &clientConfig{} - for _, opt := range opts { - opt(config) - } - +func NewClient(ctx context.Context, addr, token string) (*Client, error) { authHeader := http.Header{perms.AuthKey: []string{fmt.Sprintf("Bearer %s", token)}} - return newClient(ctx, addr, authHeader, config) + return newClient(ctx, addr, authHeader) } -func newClient(ctx context.Context, addr string, authHeader http.Header, config *clientConfig) (*Client, error) { +func newClient(ctx context.Context, addr string, authHeader http.Header) (*Client, error) { var client Client var multiCloser multiClientCloser - httpClient := &http.Client{ - Timeout: config.timeout, - } - for name, module := range moduleMap(&client) { - closer, err := jsonrpc.NewMergeClient( + closer, err := jsonrpc.NewClient( ctx, addr, name, - []interface{}{module}, + module, authHeader, - jsonrpc.WithHTTPClient(httpClient), ) if err != nil { return nil, err @@ -111,7 +88,6 @@ func newClient(ctx context.Context, addr string, authHeader http.Header, config multiCloser.register(closer) } - client.closer = multiCloser return &client, nil } diff --git a/cmd/rpc.go b/cmd/rpc.go index 8991b41aed..08aa7486b1 100644 --- a/cmd/rpc.go +++ b/cmd/rpc.go @@ -82,12 +82,12 @@ func InitClient(cmd *cobra.Command, _ []string) error { } } - var opts []rpc.RPCClientOption if timeoutFlag > 0 { - opts = append(opts, rpc.WithTimeout(timeoutFlag)) + ctx, _ := context.WithTimeout(cmd.Context(), timeoutFlag) + cmd.SetContext(ctx) } - client, err := rpc.NewClient(cmd.Context(), requestURL, authTokenFlag, opts...) + client, err := rpc.NewClient(cmd.Context(), requestURL, authTokenFlag) if err != nil { return err } From 164769eaff8582ec2014922ceaa879a683a3f2e2 Mon Sep 17 00:00:00 2001 From: sebasti810 Date: Fri, 6 Sep 2024 17:37:46 +0200 Subject: [PATCH 4/8] chore(api): clean up --- api/rpc/client/client.go | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/api/rpc/client/client.go b/api/rpc/client/client.go index 7f1fb1a4c1..7f90af4b9f 100644 --- a/api/rpc/client/client.go +++ b/api/rpc/client/client.go @@ -71,17 +71,11 @@ func NewClient(ctx context.Context, addr, token string) (*Client, error) { } func newClient(ctx context.Context, addr string, authHeader http.Header) (*Client, error) { - var client Client var multiCloser multiClientCloser + var client Client for name, module := range moduleMap(&client) { - closer, err := jsonrpc.NewClient( - ctx, - addr, - name, - module, - authHeader, - ) + closer, err := jsonrpc.NewClient(ctx, addr, name, module, authHeader) if err != nil { return nil, err } From 8a37e79e27f646796732a03f3b440f2e64cacc9c Mon Sep 17 00:00:00 2001 From: sebasti810 Date: Fri, 6 Sep 2024 17:38:43 +0200 Subject: [PATCH 5/8] chore(api): clean up --- api/rpc/client/client.go | 1 - 1 file changed, 1 deletion(-) diff --git a/api/rpc/client/client.go b/api/rpc/client/client.go index 7f90af4b9f..56b4a54d19 100644 --- a/api/rpc/client/client.go +++ b/api/rpc/client/client.go @@ -73,7 +73,6 @@ func NewClient(ctx context.Context, addr, token string) (*Client, error) { func newClient(ctx context.Context, addr string, authHeader http.Header) (*Client, error) { var multiCloser multiClientCloser var client Client - for name, module := range moduleMap(&client) { closer, err := jsonrpc.NewClient(ctx, addr, name, module, authHeader) if err != nil { From cf9d52783cd256c1e0f1e8e23967a17bd2b9b88c Mon Sep 17 00:00:00 2001 From: sebasti810 Date: Fri, 6 Sep 2024 17:48:20 +0200 Subject: [PATCH 6/8] chore(api): adding a comment --- cmd/rpc.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cmd/rpc.go b/cmd/rpc.go index 08aa7486b1..0a9ebfcf01 100644 --- a/cmd/rpc.go +++ b/cmd/rpc.go @@ -83,6 +83,7 @@ func InitClient(cmd *cobra.Command, _ []string) error { } if timeoutFlag > 0 { + // we don't cancel, because we want to keep this context alive outside the InitClient Function ctx, _ := context.WithTimeout(cmd.Context(), timeoutFlag) cmd.SetContext(ctx) } From f7bbb685a5d162dc635a7a60dda82d5bdf594276 Mon Sep 17 00:00:00 2001 From: rene <41963722+renaynay@users.noreply.github.com> Date: Wed, 2 Oct 2024 17:25:17 +0200 Subject: [PATCH 7/8] lint --- cmd/rpc.go | 2 +- share/shwap/p2p/bitswap/block_fetch_test.go | 2 +- share/shwap/p2p/shrex/error.go | 4 +++- store/file/file.go | 4 +++- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/cmd/rpc.go b/cmd/rpc.go index 0a9ebfcf01..ed3d144c3d 100644 --- a/cmd/rpc.go +++ b/cmd/rpc.go @@ -84,7 +84,7 @@ func InitClient(cmd *cobra.Command, _ []string) error { if timeoutFlag > 0 { // we don't cancel, because we want to keep this context alive outside the InitClient Function - ctx, _ := context.WithTimeout(cmd.Context(), timeoutFlag) + ctx, _ := context.WithTimeout(cmd.Context(), timeoutFlag) //nolint:govet cmd.SetContext(ctx) } diff --git a/share/shwap/p2p/bitswap/block_fetch_test.go b/share/shwap/p2p/bitswap/block_fetch_test.go index 6642801efe..5eeb4a56d2 100644 --- a/share/shwap/p2p/bitswap/block_fetch_test.go +++ b/share/shwap/p2p/bitswap/block_fetch_test.go @@ -2,7 +2,6 @@ package bitswap import ( "context" - "math/rand/v2" "sync" "testing" "time" @@ -17,6 +16,7 @@ import ( mocknet "github.com/libp2p/go-libp2p/p2p/net/mock" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "math/rand/v2" "github.com/celestiaorg/rsmt2d" diff --git a/share/shwap/p2p/shrex/error.go b/share/shwap/p2p/shrex/error.go index d32c3c85b2..b5e6dce677 100644 --- a/share/shwap/p2p/shrex/error.go +++ b/share/shwap/p2p/shrex/error.go @@ -1,6 +1,8 @@ package shrex -import "errors" +import ( + "errors" +) // ErrorContains reports whether any error in err's tree matches any error in targets tree. func ErrorContains(err, target error) bool { diff --git a/store/file/file.go b/store/file/file.go index 50bd7f86cd..3cf89015f6 100644 --- a/store/file/file.go +++ b/store/file/file.go @@ -1,6 +1,8 @@ package file -import logging "github.com/ipfs/go-log/v2" +import ( + logging "github.com/ipfs/go-log/v2" +) var log = logging.Logger("store/file") From 517e8648c77202ee1f486c30eec0000d78adca26 Mon Sep 17 00:00:00 2001 From: rene <41963722+renaynay@users.noreply.github.com> Date: Thu, 3 Oct 2024 10:28:20 +0200 Subject: [PATCH 8/8] chore: lint --- share/shwap/p2p/bitswap/block_fetch_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/share/shwap/p2p/bitswap/block_fetch_test.go b/share/shwap/p2p/bitswap/block_fetch_test.go index 5eeb4a56d2..6642801efe 100644 --- a/share/shwap/p2p/bitswap/block_fetch_test.go +++ b/share/shwap/p2p/bitswap/block_fetch_test.go @@ -2,6 +2,7 @@ package bitswap import ( "context" + "math/rand/v2" "sync" "testing" "time" @@ -16,7 +17,6 @@ import ( mocknet "github.com/libp2p/go-libp2p/p2p/net/mock" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "math/rand/v2" "github.com/celestiaorg/rsmt2d"