Skip to content

Commit

Permalink
feat(cmd): adding timeout flag (#3580)
Browse files Browse the repository at this point in the history
Co-authored-by: Hlib Kanunnikov <hlibwondertan@gmail.com>
Co-authored-by: rene <41963722+renaynay@users.noreply.github.com>
Co-authored-by: Oleg Kovalov <oleg@hey.com>
  • Loading branch information
4 people authored Oct 3, 2024
1 parent f48223d commit 1c62d99
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 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,6 +39,13 @@ 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
Expand Down Expand Up @@ -73,6 +82,12 @@ 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) //nolint:govet
cmd.SetContext(ctx)
}

client, err := rpc.NewClient(cmd.Context(), requestURL, authTokenFlag)
if err != nil {
return err
Expand Down

0 comments on commit 1c62d99

Please sign in to comment.