From 1c62d99902feff319b33413902f809f076ba4e4b Mon Sep 17 00:00:00 2001 From: Sebastian <48989775+sebasti810@users.noreply.github.com> Date: Thu, 3 Oct 2024 10:38:10 +0200 Subject: [PATCH] feat(cmd): adding timeout flag (#3580) Co-authored-by: Hlib Kanunnikov Co-authored-by: rene <41963722+renaynay@users.noreply.github.com> Co-authored-by: Oleg Kovalov --- cmd/rpc.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/cmd/rpc.go b/cmd/rpc.go index 230b51508b..ed3d144c3d 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,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 @@ -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