github.com/TrueCloudLab/frostfs-api-go/v2@v2.0.0-20230228134343-196241c4e79a/rpc/client/options.go (about) 1 package client 2 3 import ( 4 "crypto/tls" 5 "time" 6 7 "google.golang.org/grpc" 8 "google.golang.org/grpc/credentials/insecure" 9 "google.golang.org/grpc/keepalive" 10 ) 11 12 const ( 13 grpcScheme = "grpc" 14 grpcTLSScheme = "grpcs" 15 ) 16 17 // Option is a Client's option. 18 type Option func(*cfg) 19 20 type cfg struct { 21 addr string 22 23 dialTimeout time.Duration 24 rwTimeout time.Duration 25 26 tlsCfg *tls.Config 27 grpcDialOpts []grpc.DialOption 28 29 conn *grpc.ClientConn 30 } 31 32 const ( 33 defaultDialTimeout = 5 * time.Second 34 defaultKeepAliveTimeout = 5 * time.Second 35 defaultRWTimeout = 1 * time.Minute 36 ) 37 38 func (c *cfg) initDefault() { 39 c.dialTimeout = defaultDialTimeout 40 c.rwTimeout = defaultRWTimeout 41 c.grpcDialOpts = []grpc.DialOption{ 42 grpc.WithBlock(), 43 grpc.WithTransportCredentials(insecure.NewCredentials()), 44 grpc.WithKeepaliveParams(keepalive.ClientParameters{ 45 Timeout: defaultKeepAliveTimeout, 46 }), 47 } 48 } 49 50 // WithNetworkAddress returns option to specify 51 // network address of the remote server. 52 // 53 // Ignored if WithGRPCConn is provided. 54 func WithNetworkAddress(v string) Option { 55 return func(c *cfg) { 56 if v != "" { 57 c.addr = v 58 } 59 } 60 } 61 62 // WithNetworkURIAddress combines WithNetworkAddress and WithTLSCfg options 63 // based on arguments. 64 // 65 // Do not use along with WithNetworkAddress and WithTLSCfg. 66 // 67 // Ignored if WithGRPCConn is provided. 68 func WithNetworkURIAddress(addr string, tlsCfg *tls.Config) []Option { 69 host, isTLS, err := ParseURI(addr) 70 if err != nil { 71 return nil 72 } 73 74 opts := make([]Option, 2) 75 opts[0] = WithNetworkAddress(host) 76 if isTLS { 77 if tlsCfg == nil { 78 tlsCfg = &tls.Config{} 79 } 80 opts[1] = WithTLSCfg(tlsCfg) 81 } else { 82 opts[1] = WithTLSCfg(nil) 83 } 84 85 return opts 86 } 87 88 // WithDialTimeout returns option to specify 89 // dial timeout of the remote server connection. 90 // 91 // Ignored if WithGRPCConn is provided. 92 func WithDialTimeout(v time.Duration) Option { 93 return func(c *cfg) { 94 if v > 0 { 95 c.dialTimeout = v 96 } 97 } 98 } 99 100 // WithRWTimeout returns option to specify timeout 101 // for reading and writing single gRPC message. 102 func WithRWTimeout(v time.Duration) Option { 103 return func(c *cfg) { 104 if v > 0 { 105 c.rwTimeout = v 106 } 107 } 108 } 109 110 // WithTLSCfg returns option to specify 111 // TLS configuration. 112 // 113 // Ignored if WithGRPCConn is provided. 114 func WithTLSCfg(v *tls.Config) Option { 115 return func(c *cfg) { 116 c.tlsCfg = v 117 } 118 } 119 120 // WithGRPCConn returns option to specify 121 // gRPC virtual connection. 122 func WithGRPCConn(v *grpc.ClientConn) Option { 123 return func(c *cfg) { 124 if v != nil { 125 c.conn = v 126 } 127 } 128 } 129 130 // WithGRPCDialOptions returns an option to specify grpc.DialOption. 131 func WithGRPCDialOptions(opts []grpc.DialOption) Option { 132 return func(c *cfg) { 133 c.grpcDialOpts = append(c.grpcDialOpts, opts...) 134 } 135 }