forked from TrueCloudLab/frostfs-node
Aleksey Savchuk
6f798b9c4b
Before, when the target RPC server was unavailable, requests made by CLI didn't wait for a timeout specified by the `--timeout` option if the timeout was more than 20 seconds. It's because of the gRPC default backoff strategy. Adding this option fixes that behavior. Signed-off-by: Aleksey Savchuk <a.savchuk@yadro.com>
54 lines
1.8 KiB
Go
54 lines
1.8 KiB
Go
package tree
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"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"
|
|
)
|
|
|
|
// _client returns grpc Tree service client. Should be removed
|
|
// after making Tree API public.
|
|
func _client() (tree.TreeServiceClient, error) {
|
|
var netAddr network.Address
|
|
err := netAddr.FromString(viper.GetString(commonflags.RPC))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
opts := []grpc.DialOption{
|
|
grpc.WithChainUnaryInterceptor(
|
|
metrics.NewUnaryClientInterceptor(),
|
|
tracing.NewUnaryClientInteceptor(),
|
|
),
|
|
grpc.WithChainStreamInterceptor(
|
|
metrics.NewStreamClientInterceptor(),
|
|
tracing.NewStreamClientInterceptor(),
|
|
),
|
|
grpc.WithDefaultCallOptions(grpc.WaitForReady(true)),
|
|
}
|
|
|
|
if !strings.HasPrefix(netAddr.URIAddr(), "grpcs:") {
|
|
opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
|
}
|
|
|
|
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)
|
|
}
|