Skip to content

Commit

Permalink
fix: 3575
Browse files Browse the repository at this point in the history
  • Loading branch information
sebasti810 committed Jul 19, 2024
1 parent 79295fc commit 6bebf4b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
34 changes: 29 additions & 5 deletions api/rpc/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/http"
"time"

"github.com/filecoin-project/go-jsonrpc"

Expand Down Expand Up @@ -39,6 +40,18 @@ type Client struct {
closer multiClientCloser
}

type ClientOption func(*clientConfig)

Check failure on line 43 in api/rpc/client/client.go

View workflow job for this annotation

GitHub Actions / go-ci / Lint

exported: type name will be used as client.ClientOption by other packages, and that stutters; consider calling this Option (revive)

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
Expand All @@ -63,22 +76,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
}

Expand Down
18 changes: 16 additions & 2 deletions cmd/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"path/filepath"
"time"

"github.com/spf13/cobra"
flag "github.com/spf13/pflag"
Expand All @@ -18,6 +19,7 @@ import (
var (
requestURL string
authTokenFlag string
timeoutFlag time.Duration
)

func RPCFlags() *flag.FlagSet {
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 6bebf4b

Please sign in to comment.