github.com/ydb-platform/ydb-go-sdk/v3@v3.89.2/config/config.go (about) 1 package config 2 3 import ( 4 "crypto/tls" 5 "crypto/x509" 6 "time" 7 8 "google.golang.org/grpc" 9 grpcCodes "google.golang.org/grpc/codes" 10 11 "github.com/ydb-platform/ydb-go-sdk/v3/credentials" 12 balancerConfig "github.com/ydb-platform/ydb-go-sdk/v3/internal/balancer/config" 13 "github.com/ydb-platform/ydb-go-sdk/v3/internal/config" 14 "github.com/ydb-platform/ydb-go-sdk/v3/internal/meta" 15 "github.com/ydb-platform/ydb-go-sdk/v3/retry/budget" 16 "github.com/ydb-platform/ydb-go-sdk/v3/trace" 17 ) 18 19 // Config contains driver configuration. 20 type Config struct { 21 config.Common 22 23 trace *trace.Driver 24 dialTimeout time.Duration 25 connectionTTL time.Duration 26 balancerConfig *balancerConfig.Config 27 secure bool 28 endpoint string 29 database string 30 metaOptions []meta.Option 31 grpcOptions []grpc.DialOption 32 credentials credentials.Credentials 33 tlsConfig *tls.Config 34 meta *meta.Meta 35 36 excludeGRPCCodesForPessimization []grpcCodes.Code 37 } 38 39 func (c *Config) Credentials() credentials.Credentials { 40 return c.credentials 41 } 42 43 // ExcludeGRPCCodesForPessimization defines grpc codes for exclude its from pessimization trigger 44 func (c *Config) ExcludeGRPCCodesForPessimization() []grpcCodes.Code { 45 return c.excludeGRPCCodesForPessimization 46 } 47 48 // GrpcDialOptions reports about used grpc dialing options 49 func (c *Config) GrpcDialOptions() []grpc.DialOption { 50 return append( 51 defaultGrpcOptions(c.trace, c.secure, c.tlsConfig), 52 c.grpcOptions..., 53 ) 54 } 55 56 // Meta reports meta information about database connection 57 func (c *Config) Meta() *meta.Meta { 58 return c.meta 59 } 60 61 // ConnectionTTL defines interval for parking grpc connections. 62 // 63 // If ConnectionTTL is zero - connections are not park. 64 func (c *Config) ConnectionTTL() time.Duration { 65 return c.connectionTTL 66 } 67 68 // Secure is a flag for secure connection 69 func (c *Config) Secure() bool { 70 return c.secure 71 } 72 73 // Endpoint is a required starting endpoint for connect 74 func (c *Config) Endpoint() string { 75 return c.endpoint 76 } 77 78 // TLSConfig reports about TLS configuration 79 func (c *Config) TLSConfig() *tls.Config { 80 return c.tlsConfig 81 } 82 83 // DialTimeout is the maximum amount of time a dial will wait for a connect to 84 // complete. 85 // 86 // If DialTimeout is zero then no timeout is used. 87 func (c *Config) DialTimeout() time.Duration { 88 return c.dialTimeout 89 } 90 91 // Database is a required database name. 92 func (c *Config) Database() string { 93 return c.database 94 } 95 96 // Trace contains driver tracing options. 97 func (c *Config) Trace() *trace.Driver { 98 return c.trace 99 } 100 101 // Balancer is an optional configuration related to selected balancer. 102 // That is, some balancing methods allow to be configured. 103 func (c *Config) Balancer() *balancerConfig.Config { 104 return c.balancerConfig 105 } 106 107 type Option func(c *Config) 108 109 // WithInternalDNSResolver 110 // 111 // Deprecated: always used internal dns-resolver. 112 // Will be removed after Oct 2024. 113 // Read about versioning policy: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#deprecated 114 func WithInternalDNSResolver() Option { 115 return func(c *Config) {} 116 } 117 118 func WithEndpoint(endpoint string) Option { 119 return func(c *Config) { 120 c.endpoint = endpoint 121 } 122 } 123 124 // WithSecure changes secure connection flag. 125 // 126 // Warning: if secure is false - TLS config options has no effect. 127 func WithSecure(secure bool) Option { 128 return func(c *Config) { 129 c.secure = secure 130 } 131 } 132 133 func WithDatabase(database string) Option { 134 return func(c *Config) { 135 c.database = database 136 } 137 } 138 139 // WithCertificate appends certificate to TLS config root certificates 140 func WithCertificate(certificate *x509.Certificate) Option { 141 return func(c *Config) { 142 c.tlsConfig.RootCAs.AddCert(certificate) 143 } 144 } 145 146 // WithTLSConfig replaces older TLS config 147 // 148 // Warning: all early changes of TLS config will be lost 149 func WithTLSConfig(tlsConfig *tls.Config) Option { 150 return func(c *Config) { 151 c.tlsConfig = tlsConfig 152 } 153 } 154 155 func WithTrace(t trace.Driver, opts ...trace.DriverComposeOption) Option { //nolint:gocritic 156 return func(c *Config) { 157 c.trace = c.trace.Compose(&t, opts...) 158 } 159 } 160 161 // Experimental: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#experimental 162 func WithRetryBudget(b budget.Budget) Option { 163 return func(c *Config) { 164 config.SetRetryBudget(&c.Common, b) 165 } 166 } 167 168 func WithTraceRetry(t *trace.Retry, opts ...trace.RetryComposeOption) Option { 169 return func(c *Config) { 170 config.SetTraceRetry(&c.Common, t, opts...) 171 } 172 } 173 174 // WithApplicationName add provided application name to all api requests 175 func WithApplicationName(applicationName string) Option { 176 return func(c *Config) { 177 c.metaOptions = append(c.metaOptions, meta.WithApplicationNameOption(applicationName)) 178 } 179 } 180 181 // WithUserAgent add provided user agent to all api requests 182 // 183 // Deprecated: use WithApplicationName instead. 184 // Will be removed after Oct 2024. 185 // Read about versioning policy: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#deprecated 186 func WithUserAgent(userAgent string) Option { 187 return func(c *Config) { 188 c.metaOptions = append(c.metaOptions, meta.WithApplicationNameOption(userAgent)) 189 } 190 } 191 192 func WithConnectionTTL(ttl time.Duration) Option { 193 return func(c *Config) { 194 c.connectionTTL = ttl 195 } 196 } 197 198 func WithCredentials(credentials credentials.Credentials) Option { 199 return func(c *Config) { 200 c.credentials = credentials 201 } 202 } 203 204 // WithOperationTimeout defines the maximum amount of time a YDB server will process 205 // an operation. After timeout exceeds YDB will try to cancel operation and 206 // regardless of the cancellation appropriate error will be returned to 207 // the client. 208 // 209 // If OperationTimeout is zero then no timeout is used. 210 func WithOperationTimeout(operationTimeout time.Duration) Option { 211 return func(c *Config) { 212 config.SetOperationTimeout(&c.Common, operationTimeout) 213 } 214 } 215 216 // WithOperationCancelAfter sets the maximum amount of time a YDB server will process an 217 // operation. After timeout exceeds YDB will try to cancel operation and if 218 // it succeeds appropriate error will be returned to the client; otherwise 219 // processing will be continued. 220 // 221 // If OperationCancelAfter is zero then no timeout is used. 222 func WithOperationCancelAfter(operationCancelAfter time.Duration) Option { 223 return func(c *Config) { 224 config.SetOperationCancelAfter(&c.Common, operationCancelAfter) 225 } 226 } 227 228 // WithNoAutoRetry disable auto-retry calls from YDB sub-clients 229 func WithNoAutoRetry() Option { 230 return func(c *Config) { 231 config.SetAutoRetry(&c.Common, false) 232 } 233 } 234 235 // WithPanicCallback applies panic callback to config 236 func WithPanicCallback(panicCallback func(e interface{})) Option { 237 return func(c *Config) { 238 config.SetPanicCallback(&c.Common, panicCallback) 239 } 240 } 241 242 func WithDialTimeout(timeout time.Duration) Option { 243 return func(c *Config) { 244 c.dialTimeout = timeout 245 } 246 } 247 248 func WithBalancer(balancer *balancerConfig.Config) Option { 249 return func(c *Config) { 250 c.balancerConfig = balancer 251 } 252 } 253 254 func WithRequestsType(requestsType string) Option { 255 return func(c *Config) { 256 c.metaOptions = append(c.metaOptions, meta.WithRequestTypeOption(requestsType)) 257 } 258 } 259 260 // WithMinTLSVersion applies minimum TLS version that is acceptable. 261 func WithMinTLSVersion(minVersion uint16) Option { 262 return func(c *Config) { 263 c.tlsConfig.MinVersion = minVersion 264 } 265 } 266 267 // WithTLSSInsecureSkipVerify applies InsecureSkipVerify flag to TLS config 268 func WithTLSSInsecureSkipVerify() Option { 269 return func(c *Config) { 270 c.tlsConfig.InsecureSkipVerify = true 271 } 272 } 273 274 // WithGrpcOptions appends custom grpc dial options to defaults 275 func WithGrpcOptions(option ...grpc.DialOption) Option { 276 return func(c *Config) { 277 c.grpcOptions = append(c.grpcOptions, option...) 278 } 279 } 280 281 func ExcludeGRPCCodesForPessimization(codes ...grpcCodes.Code) Option { 282 return func(c *Config) { 283 c.excludeGRPCCodesForPessimization = append( 284 c.excludeGRPCCodesForPessimization, 285 codes..., 286 ) 287 } 288 } 289 290 func New(opts ...Option) *Config { 291 c := defaultConfig() 292 293 for _, opt := range opts { 294 if opt != nil { 295 opt(c) 296 } 297 } 298 299 c.meta = meta.New(c.database, c.credentials, c.trace, c.metaOptions...) 300 301 return c 302 } 303 304 // With makes copy of current Config with specified options 305 func (c *Config) With(opts ...Option) *Config { 306 for _, opt := range opts { 307 if opt != nil { 308 opt(c) 309 } 310 } 311 c.meta = meta.New( 312 c.database, 313 c.credentials, 314 c.trace, 315 c.metaOptions..., 316 ) 317 318 return c 319 }