Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cmd): adding timeout flag #3580

Merged
merged 12 commits into from
Oct 3, 2024
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
Loading