github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/config/defaults.go (about)

     1  package config
     2  
     3  import (
     4  	"crypto/tls"
     5  	"crypto/x509"
     6  	"time"
     7  
     8  	"google.golang.org/grpc"
     9  	grpcCredentials "google.golang.org/grpc/credentials"
    10  	"google.golang.org/grpc/credentials/insecure"
    11  	"google.golang.org/grpc/keepalive"
    12  
    13  	"github.com/ydb-platform/ydb-go-sdk/v3/balancers"
    14  	"github.com/ydb-platform/ydb-go-sdk/v3/credentials"
    15  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/stack"
    16  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/xresolver"
    17  	"github.com/ydb-platform/ydb-go-sdk/v3/trace"
    18  )
    19  
    20  var (
    21  	// DefaultKeepaliveInterval contains default duration between grpc keepalive
    22  	DefaultKeepaliveInterval    = 10 * time.Second
    23  	MinKeepaliveInterval        = 10 * time.Second
    24  	DefaultDialTimeout          = 5 * time.Second
    25  	DefaultGRPCMsgSize          = 64 * 1024 * 1024 // 64MB
    26  	DefaultGrpcConnectionPolicy = keepalive.ClientParameters{
    27  		Time:                DefaultKeepaliveInterval,
    28  		Timeout:             MinKeepaliveInterval,
    29  		PermitWithoutStream: true,
    30  	}
    31  )
    32  
    33  func defaultGrpcOptions(t *trace.Driver, secure bool, tlsConfig *tls.Config) (opts []grpc.DialOption) {
    34  	opts = append(opts,
    35  		// keep-aliving all connections
    36  		grpc.WithKeepaliveParams(
    37  			DefaultGrpcConnectionPolicy,
    38  		),
    39  		// use round robin balancing policy for fastest dialing
    40  		grpc.WithDefaultServiceConfig(`{
    41  			"loadBalancingPolicy": "round_robin"
    42  		}`),
    43  		// limit size of outgoing and incoming packages
    44  		grpc.WithDefaultCallOptions(
    45  			grpc.MaxCallRecvMsgSize(DefaultGRPCMsgSize),
    46  			grpc.MaxCallSendMsgSize(DefaultGRPCMsgSize),
    47  		),
    48  		// use proxy-resolvers
    49  		// 1) for interpret schemas `ydb`, `grpc` and `grpcs` in node URLs as for dns resolver
    50  		// 2) for observe resolving events
    51  		grpc.WithResolvers(
    52  			xresolver.New("", t),
    53  			xresolver.New("ydb", t),
    54  			xresolver.New("grpc", t),
    55  			xresolver.New("grpcs", t),
    56  		),
    57  	)
    58  	if secure {
    59  		opts = append(opts, grpc.WithTransportCredentials(
    60  			grpcCredentials.NewTLS(tlsConfig),
    61  		))
    62  	} else {
    63  		opts = append(opts, grpc.WithTransportCredentials(
    64  			insecure.NewCredentials(),
    65  		))
    66  	}
    67  
    68  	return opts
    69  }
    70  
    71  func certPool() *x509.CertPool {
    72  	certPool, err := x509.SystemCertPool()
    73  	if err == nil {
    74  		return certPool
    75  	}
    76  
    77  	return x509.NewCertPool()
    78  }
    79  
    80  func defaultTLSConfig() *tls.Config {
    81  	return &tls.Config{
    82  		MinVersion: tls.VersionTLS12,
    83  		RootCAs:    certPool(),
    84  	}
    85  }
    86  
    87  func defaultConfig() (c *Config) {
    88  	return &Config{
    89  		credentials: credentials.NewAnonymousCredentials(
    90  			credentials.WithSourceInfo(stack.Record(0)),
    91  		),
    92  		balancerConfig: balancers.Default(),
    93  		tlsConfig:      defaultTLSConfig(),
    94  		dialTimeout:    DefaultDialTimeout,
    95  		trace:          &trace.Driver{},
    96  	}
    97  }