From 90bfe0bad9bce9728b73d8b5d9ea5f046107738e Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Wed, 5 Oct 2022 10:39:47 +0300 Subject: [PATCH] [#1826] neofs-cli: Add `--timeout` flag Allow to specify it everywhere `--rpc-endpoint` flag is present. Signed-off-by: Evgenii Stratonikov --- CHANGELOG.md | 1 + cmd/neofs-cli/internal/client/sdk.go | 8 ++++++++ cmd/neofs-cli/internal/commonflags/flags.go | 10 ++++++++++ 3 files changed, 19 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f885cca75..450de65b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ Changelog for NeoFS Node - `flush-cache` control service command to flush write-cache (#1806) - `wallet-address` flag in `neofs-adm morph refill-gas` command (#1820) - Validate policy before container creation (#1704) +- `--timeout` flag in `neofs-cli` subcommands (#1837) ### Changed - Allow to evacuate shard data with `EvacuateShard` control RPC (#1800) diff --git a/cmd/neofs-cli/internal/client/sdk.go b/cmd/neofs-cli/internal/client/sdk.go index 576b570bf..4ef2044ea 100644 --- a/cmd/neofs-cli/internal/client/sdk.go +++ b/cmd/neofs-cli/internal/client/sdk.go @@ -9,6 +9,7 @@ import ( "fmt" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common" + "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags" "github.com/nspcc-dev/neofs-node/pkg/network" "github.com/nspcc-dev/neofs-sdk-go/client" "github.com/spf13/cobra" @@ -48,6 +49,13 @@ func GetSDKClient(key *ecdsa.PrivateKey, addr network.Address) (*client.Client, prmInit.SetDefaultPrivateKey(*key) prmInit.ResolveNeoFSFailures() prmDial.SetServerURI(addr.URIAddr()) + if timeout := viper.GetDuration(commonflags.Timeout); timeout > 0 { + // In CLI we can only set a timeout for the whole operation. + // By also setting stream timeout we ensure that no operation hands + // for too long. + prmDial.SetTimeout(timeout) + prmDial.SetStreamTimeout(timeout) + } c.Init(prmInit) diff --git a/cmd/neofs-cli/internal/commonflags/flags.go b/cmd/neofs-cli/internal/commonflags/flags.go index 7c3c50f89..53c4812bf 100644 --- a/cmd/neofs-cli/internal/commonflags/flags.go +++ b/cmd/neofs-cli/internal/commonflags/flags.go @@ -1,6 +1,8 @@ package commonflags import ( + "time" + "github.com/spf13/cobra" "github.com/spf13/viper" ) @@ -28,6 +30,11 @@ const ( RPCDefault = "" RPCUsage = "remote node address (as 'multiaddr' or ':')" + Timeout = "timeout" + TimeoutShorthand = "t" + TimeoutDefault = 15 * time.Second + TimeoutUsage = "timeout for an operation" + Verbose = "verbose" VerboseShorthand = "v" VerboseUsage = "verbose output" @@ -41,11 +48,13 @@ const ( // - WalletPath // - Account // - RPC +// - Timeout func Init(cmd *cobra.Command) { InitWithoutRPC(cmd) ff := cmd.Flags() ff.StringP(RPC, RPCShorthand, RPCDefault, RPCUsage) + ff.DurationP(Timeout, TimeoutShorthand, TimeoutDefault, TimeoutUsage) } // InitWithoutRPC is similar to Init but doesn't create the RPC flag. @@ -65,4 +74,5 @@ func Bind(cmd *cobra.Command) { _ = viper.BindPFlag(WalletPath, ff.Lookup(WalletPath)) _ = viper.BindPFlag(Account, ff.Lookup(Account)) _ = viper.BindPFlag(RPC, ff.Lookup(RPC)) + _ = viper.BindPFlag(Timeout, ff.Lookup(Timeout)) }