diff --git a/cmd/frostfs-cli/modules/tree/add.go b/cmd/frostfs-cli/modules/tree/add.go index 0b8dc292..019feb0e 100644 --- a/cmd/frostfs-cli/modules/tree/add.go +++ b/cmd/frostfs-cli/modules/tree/add.go @@ -47,9 +47,10 @@ func add(cmd *cobra.Command, _ []string) { meta, err := parseMeta(cmd) commonCmd.ExitOnErr(cmd, "meta data parsing: %w", err) - ctx := cmd.Context() + ctx, cancel := contextWithTimeout(cmd) + defer cancel() - cli, err := _client(ctx) + cli, err := _client() commonCmd.ExitOnErr(cmd, "failed to create client: %w", err) rawCID := make([]byte, sha256.Size) diff --git a/cmd/frostfs-cli/modules/tree/add_by_path.go b/cmd/frostfs-cli/modules/tree/add_by_path.go index ea815dbf..5d5b00b7 100644 --- a/cmd/frostfs-cli/modules/tree/add_by_path.go +++ b/cmd/frostfs-cli/modules/tree/add_by_path.go @@ -50,9 +50,10 @@ func addByPath(cmd *cobra.Command, _ []string) { commonCmd.ExitOnErr(cmd, "decode container ID string: %w", err) tid, _ := cmd.Flags().GetString(treeIDFlagKey) - ctx := cmd.Context() + ctx, cancel := contextWithTimeout(cmd) + defer cancel() - cli, err := _client(ctx) + cli, err := _client() commonCmd.ExitOnErr(cmd, "failed to create client: %w", err) rawCID := make([]byte, sha256.Size) diff --git a/cmd/frostfs-cli/modules/tree/client.go b/cmd/frostfs-cli/modules/tree/client.go index 4f4f5465..4e0099f0 100644 --- a/cmd/frostfs-cli/modules/tree/client.go +++ b/cmd/frostfs-cli/modules/tree/client.go @@ -3,13 +3,14 @@ package tree import ( "context" "strings" - "time" + "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/common" "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/commonflags" "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/network" "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/tree" metrics "git.frostfs.info/TrueCloudLab/frostfs-observability/metrics/grpc" tracing "git.frostfs.info/TrueCloudLab/frostfs-observability/tracing/grpc" + "github.com/spf13/cobra" "github.com/spf13/viper" "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" @@ -17,7 +18,7 @@ import ( // _client returns grpc Tree service client. Should be removed // after making Tree API public. -func _client(ctx context.Context) (tree.TreeServiceClient, error) { +func _client() (tree.TreeServiceClient, error) { var netAddr network.Address err := netAddr.FromString(viper.GetString(commonflags.RPC)) if err != nil { @@ -25,7 +26,6 @@ func _client(ctx context.Context) (tree.TreeServiceClient, error) { } opts := []grpc.DialOption{ - grpc.WithBlock(), grpc.WithChainUnaryInterceptor( metrics.NewUnaryClientInterceptor(), tracing.NewUnaryClientInteceptor(), @@ -40,12 +40,14 @@ func _client(ctx context.Context) (tree.TreeServiceClient, error) { opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials())) } - // a default connection establishing timeout - const defaultClientConnectTimeout = time.Second * 2 - - ctx, cancel := context.WithTimeout(ctx, defaultClientConnectTimeout) - cc, err := grpc.DialContext(ctx, netAddr.URIAddr(), opts...) - cancel() - + cc, err := grpc.NewClient(netAddr.URIAddr(), opts...) return tree.NewTreeServiceClient(cc), err } + +func contextWithTimeout(cmd *cobra.Command) (context.Context, context.CancelFunc) { + if timeout := viper.GetDuration(commonflags.Timeout); timeout > 0 { + common.PrintVerbose(cmd, "Set request timeout to %s.", timeout) + return context.WithTimeout(cmd.Context(), timeout) + } + return context.WithTimeout(cmd.Context(), commonflags.TimeoutDefault) +} diff --git a/cmd/frostfs-cli/modules/tree/get_by_path.go b/cmd/frostfs-cli/modules/tree/get_by_path.go index f239066c..7061723f 100644 --- a/cmd/frostfs-cli/modules/tree/get_by_path.go +++ b/cmd/frostfs-cli/modules/tree/get_by_path.go @@ -50,9 +50,10 @@ func getByPath(cmd *cobra.Command, _ []string) { commonCmd.ExitOnErr(cmd, "decode container ID string: %w", err) tid, _ := cmd.Flags().GetString(treeIDFlagKey) - ctx := cmd.Context() + ctx, cancel := contextWithTimeout(cmd) + defer cancel() - cli, err := _client(ctx) + cli, err := _client() commonCmd.ExitOnErr(cmd, "failed to create client: %w", err) rawCID := make([]byte, sha256.Size) diff --git a/cmd/frostfs-cli/modules/tree/get_op_log.go b/cmd/frostfs-cli/modules/tree/get_op_log.go index b1b307f6..376aa8e8 100644 --- a/cmd/frostfs-cli/modules/tree/get_op_log.go +++ b/cmd/frostfs-cli/modules/tree/get_op_log.go @@ -44,9 +44,10 @@ func getOpLog(cmd *cobra.Command, _ []string) { commonCmd.ExitOnErr(cmd, "decode container ID string: %w", err) tid, _ := cmd.Flags().GetString(treeIDFlagKey) - ctx := cmd.Context() + ctx, cancel := contextWithTimeout(cmd) + defer cancel() - cli, err := _client(ctx) + cli, err := _client() commonCmd.ExitOnErr(cmd, "failed to create client: %w", err) rawCID := make([]byte, sha256.Size) diff --git a/cmd/frostfs-cli/modules/tree/healthcheck.go b/cmd/frostfs-cli/modules/tree/healthcheck.go index f0506467..b01bb2e7 100644 --- a/cmd/frostfs-cli/modules/tree/healthcheck.go +++ b/cmd/frostfs-cli/modules/tree/healthcheck.go @@ -26,9 +26,10 @@ func initHealthcheckCmd() { func healthcheck(cmd *cobra.Command, _ []string) { pk := key.GetOrGenerate(cmd) - ctx := cmd.Context() + ctx, cancel := contextWithTimeout(cmd) + defer cancel() - cli, err := _client(ctx) + cli, err := _client() commonCmd.ExitOnErr(cmd, "failed to create client: %w", err) req := &tree.HealthcheckRequest{ diff --git a/cmd/frostfs-cli/modules/tree/list.go b/cmd/frostfs-cli/modules/tree/list.go index a25d066d..f8c0e490 100644 --- a/cmd/frostfs-cli/modules/tree/list.go +++ b/cmd/frostfs-cli/modules/tree/list.go @@ -38,9 +38,10 @@ func list(cmd *cobra.Command, _ []string) { err := cnr.DecodeString(cidString) commonCmd.ExitOnErr(cmd, "decode container ID string: %w", err) - ctx := cmd.Context() + ctx, cancel := contextWithTimeout(cmd) + defer cancel() - cli, err := _client(ctx) + cli, err := _client() commonCmd.ExitOnErr(cmd, "failed to create client: %w", err) rawCID := make([]byte, sha256.Size) diff --git a/cmd/frostfs-cli/modules/tree/move.go b/cmd/frostfs-cli/modules/tree/move.go index 24abbd65..dc807d75 100644 --- a/cmd/frostfs-cli/modules/tree/move.go +++ b/cmd/frostfs-cli/modules/tree/move.go @@ -45,9 +45,10 @@ func move(cmd *cobra.Command, _ []string) { err := cnr.DecodeString(cidString) commonCmd.ExitOnErr(cmd, "decode container ID string: %w", err) - ctx := cmd.Context() + ctx, cancel := contextWithTimeout(cmd) + defer cancel() - cli, err := _client(ctx) + cli, err := _client() commonCmd.ExitOnErr(cmd, "failed to create client: %w", err) rawCID := make([]byte, sha256.Size) diff --git a/cmd/frostfs-cli/modules/tree/remove.go b/cmd/frostfs-cli/modules/tree/remove.go index 74e9d974..d0b6fab2 100644 --- a/cmd/frostfs-cli/modules/tree/remove.go +++ b/cmd/frostfs-cli/modules/tree/remove.go @@ -41,9 +41,10 @@ func remove(cmd *cobra.Command, _ []string) { err := cnr.DecodeString(cidString) commonCmd.ExitOnErr(cmd, "decode container ID string: %w", err) - ctx := cmd.Context() + ctx, cancel := contextWithTimeout(cmd) + defer cancel() - cli, err := _client(ctx) + cli, err := _client() commonCmd.ExitOnErr(cmd, "failed to create client: %w", err) rawCID := make([]byte, sha256.Size) diff --git a/cmd/frostfs-cli/modules/tree/subtree.go b/cmd/frostfs-cli/modules/tree/subtree.go index e88ef79c..83a8909b 100644 --- a/cmd/frostfs-cli/modules/tree/subtree.go +++ b/cmd/frostfs-cli/modules/tree/subtree.go @@ -46,9 +46,10 @@ func getSubTree(cmd *cobra.Command, _ []string) { err := cnr.DecodeString(cidString) commonCmd.ExitOnErr(cmd, "decode container ID string: %w", err) - ctx := cmd.Context() + ctx, cancel := contextWithTimeout(cmd) + defer cancel() - cli, err := _client(ctx) + cli, err := _client() commonCmd.ExitOnErr(cmd, "failed to create client: %w", err) rawCID := make([]byte, sha256.Size)