github.com/ydb-platform/ydb-go-sdk/v3@v3.89.2/options.go (about)

     1  package ydb
     2  
     3  import (
     4  	"context"
     5  	"crypto/tls"
     6  	"crypto/x509"
     7  	"fmt"
     8  	"os"
     9  	"path/filepath"
    10  	"time"
    11  
    12  	"github.com/ydb-platform/ydb-go-sdk/v3/config"
    13  	"github.com/ydb-platform/ydb-go-sdk/v3/credentials"
    14  	balancerConfig "github.com/ydb-platform/ydb-go-sdk/v3/internal/balancer/config"
    15  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/certificates"
    16  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/conn"
    17  	coordinationConfig "github.com/ydb-platform/ydb-go-sdk/v3/internal/coordination/config"
    18  	discoveryConfig "github.com/ydb-platform/ydb-go-sdk/v3/internal/discovery/config"
    19  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/dsn"
    20  	queryConfig "github.com/ydb-platform/ydb-go-sdk/v3/internal/query/config"
    21  	ratelimiterConfig "github.com/ydb-platform/ydb-go-sdk/v3/internal/ratelimiter/config"
    22  	schemeConfig "github.com/ydb-platform/ydb-go-sdk/v3/internal/scheme/config"
    23  	scriptingConfig "github.com/ydb-platform/ydb-go-sdk/v3/internal/scripting/config"
    24  	tableConfig "github.com/ydb-platform/ydb-go-sdk/v3/internal/table/config"
    25  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
    26  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/xsql"
    27  	"github.com/ydb-platform/ydb-go-sdk/v3/log"
    28  	"github.com/ydb-platform/ydb-go-sdk/v3/retry/budget"
    29  	"github.com/ydb-platform/ydb-go-sdk/v3/topic/topicoptions"
    30  	"github.com/ydb-platform/ydb-go-sdk/v3/trace"
    31  )
    32  
    33  // Option contains configuration values for Driver
    34  type Option func(ctx context.Context, d *Driver) error
    35  
    36  func WithStaticCredentials(user, password string) Option {
    37  	return func(ctx context.Context, c *Driver) error {
    38  		c.userInfo = &dsn.UserInfo{
    39  			User:     user,
    40  			Password: password,
    41  		}
    42  
    43  		return nil
    44  	}
    45  }
    46  
    47  // WithNodeAddressMutator applies mutator for node addresses from discovery.ListEndpoints response
    48  //
    49  // Experimental: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#experimental
    50  func WithNodeAddressMutator(mutator func(address string) string) Option {
    51  	return func(ctx context.Context, c *Driver) error {
    52  		c.discoveryOptions = append(c.discoveryOptions, discoveryConfig.WithAddressMutator(mutator))
    53  
    54  		return nil
    55  	}
    56  }
    57  
    58  func WithAccessTokenCredentials(accessToken string) Option {
    59  	return WithCredentials(
    60  		credentials.NewAccessTokenCredentials(
    61  			accessToken,
    62  			credentials.WithSourceInfo(
    63  				"ydb.WithAccessTokenCredentials(accessToken)", // hide access token for logs
    64  			),
    65  		),
    66  	)
    67  }
    68  
    69  // WithOauth2TokenExchangeCredentials adds credentials that exchange token using
    70  // OAuth 2.0 token exchange protocol:
    71  // https://www.rfc-editor.org/rfc/rfc8693
    72  func WithOauth2TokenExchangeCredentials(
    73  	opts ...credentials.Oauth2TokenExchangeCredentialsOption,
    74  ) Option {
    75  	opts = append(opts, credentials.WithSourceInfo("ydb.WithOauth2TokenExchangeCredentials(opts)"))
    76  
    77  	return WithCreateCredentialsFunc(func(context.Context) (credentials.Credentials, error) {
    78  		return credentials.NewOauth2TokenExchangeCredentials(opts...)
    79  	})
    80  }
    81  
    82  /*
    83  WithOauth2TokenExchangeCredentialsFile adds credentials that exchange token using
    84  OAuth 2.0 token exchange protocol:
    85  https://www.rfc-editor.org/rfc/rfc8693
    86  Config file must be a valid json file
    87  
    88  Fields of json file
    89  
    90  	grant-type:           [string] Grant type option (default: "urn:ietf:params:oauth:grant-type:token-exchange")
    91  	res:                  [string | list of strings] Resource option (optional)
    92  	aud:                  [string | list of strings] Audience option for token exchange request (optional)
    93  	scope:                [string | list of strings] Scope option (optional)
    94  	requested-token-type: [string] Requested token type option (default: "urn:ietf:params:oauth:token-type:access_token")
    95  	subject-credentials:  [creds_json] Subject credentials options (optional)
    96  	actor-credentials:    [creds_json] Actor credentials options (optional)
    97  	token-endpoint:       [string] Token endpoint
    98  
    99  Fields of creds_json (JWT):
   100  
   101  	type:                 [string] Token source type. Set JWT
   102  	alg:                  [string] Algorithm for JWT signature.
   103  								   Supported algorithms can be listed
   104  								   with GetSupportedOauth2TokenExchangeJwtAlgorithms()
   105  	private-key:          [string] (Private) key in PEM format (RSA, EC) or Base64 format (HMAC) for JWT signature
   106  	kid:                  [string] Key id JWT standard claim (optional)
   107  	iss:                  [string] Issuer JWT standard claim (optional)
   108  	sub:                  [string] Subject JWT standard claim (optional)
   109  	aud:                  [string | list of strings] Audience JWT standard claim (optional)
   110  	jti:                  [string] JWT ID JWT standard claim (optional)
   111  	ttl:                  [string] Token TTL (default: 1h)
   112  
   113  Fields of creds_json (FIXED):
   114  
   115  	type:                 [string] Token source type. Set FIXED
   116  	token:                [string] Token value
   117  	token-type:           [string] Token type value. It will become
   118  								   subject_token_type/actor_token_type parameter
   119  								   in token exchange request (https://www.rfc-editor.org/rfc/rfc8693)
   120  */
   121  func WithOauth2TokenExchangeCredentialsFile(
   122  	configFilePath string,
   123  	opts ...credentials.Oauth2TokenExchangeCredentialsOption,
   124  ) Option {
   125  	srcInfo := credentials.WithSourceInfo(fmt.Sprintf("ydb.WithOauth2TokenExchangeCredentialsFile(%s)", configFilePath))
   126  	opts = append(opts, srcInfo)
   127  
   128  	return WithCreateCredentialsFunc(func(context.Context) (credentials.Credentials, error) {
   129  		return credentials.NewOauth2TokenExchangeCredentialsFile(configFilePath, opts...)
   130  	})
   131  }
   132  
   133  // WithApplicationName add provided application name to all api requests
   134  func WithApplicationName(applicationName string) Option {
   135  	return func(ctx context.Context, c *Driver) error {
   136  		c.options = append(c.options, config.WithApplicationName(applicationName))
   137  
   138  		return nil
   139  	}
   140  }
   141  
   142  // WithUserAgent add provided user agent value to all api requests
   143  //
   144  // Deprecated: use WithApplicationName instead.
   145  // Will be removed after Oct 2024.
   146  // Read about versioning policy: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#deprecated
   147  func WithUserAgent(userAgent string) Option {
   148  	return func(ctx context.Context, c *Driver) error {
   149  		c.options = append(c.options, config.WithApplicationName(userAgent))
   150  
   151  		return nil
   152  	}
   153  }
   154  
   155  func WithRequestsType(requestsType string) Option {
   156  	return func(ctx context.Context, c *Driver) error {
   157  		c.options = append(c.options, config.WithRequestsType(requestsType))
   158  
   159  		return nil
   160  	}
   161  }
   162  
   163  // WithConnectionString accept Driver string like
   164  //
   165  //	grpc[s]://{endpoint}/{database}[?param=value]
   166  //
   167  // Warning: WithConnectionString will be removed at next major release
   168  //
   169  // (Driver string will be required string param of ydb.Open)
   170  func WithConnectionString(connectionString string) Option {
   171  	return func(ctx context.Context, c *Driver) error {
   172  		if connectionString == "" {
   173  			return nil
   174  		}
   175  		info, err := dsn.Parse(connectionString)
   176  		if err != nil {
   177  			return xerrors.WithStackTrace(
   178  				fmt.Errorf("parse connection string '%s' failed: %w", connectionString, err),
   179  			)
   180  		}
   181  		c.options = append(c.options, info.Options...)
   182  		c.userInfo = info.UserInfo
   183  
   184  		return nil
   185  	}
   186  }
   187  
   188  // WithConnectionTTL defines duration for parking idle connections
   189  func WithConnectionTTL(ttl time.Duration) Option {
   190  	return func(ctx context.Context, c *Driver) error {
   191  		c.options = append(c.options, config.WithConnectionTTL(ttl))
   192  
   193  		return nil
   194  	}
   195  }
   196  
   197  // WithEndpoint defines endpoint option
   198  //
   199  // Warning: use ydb.Open with required Driver string parameter instead
   200  //
   201  // For making Driver string from endpoint+database+secure - use sugar.DSN()
   202  //
   203  // Deprecated: use dsn parameter in Open method
   204  func WithEndpoint(endpoint string) Option {
   205  	return func(ctx context.Context, c *Driver) error {
   206  		c.options = append(c.options, config.WithEndpoint(endpoint))
   207  
   208  		return nil
   209  	}
   210  }
   211  
   212  // WithDatabase defines database option
   213  //
   214  // Warning: use ydb.Open with required Driver string parameter instead
   215  //
   216  // For making Driver string from endpoint+database+secure - use sugar.DSN()
   217  //
   218  // Deprecated: use dsn parameter in Open method
   219  func WithDatabase(database string) Option {
   220  	return func(ctx context.Context, c *Driver) error {
   221  		c.options = append(c.options, config.WithDatabase(database))
   222  
   223  		return nil
   224  	}
   225  }
   226  
   227  // WithSecure defines secure option
   228  //
   229  // Warning: use ydb.Open with required Driver string parameter instead
   230  //
   231  // For making Driver string from endpoint+database+secure - use sugar.DSN()
   232  func WithSecure(secure bool) Option {
   233  	return func(ctx context.Context, c *Driver) error {
   234  		c.options = append(c.options, config.WithSecure(secure))
   235  
   236  		return nil
   237  	}
   238  }
   239  
   240  // WithInsecure defines secure option.
   241  //
   242  // Warning: WithInsecure lost current TLS config.
   243  func WithInsecure() Option {
   244  	return func(ctx context.Context, c *Driver) error {
   245  		c.options = append(c.options, config.WithSecure(false))
   246  
   247  		return nil
   248  	}
   249  }
   250  
   251  // WithMinTLSVersion set minimum TLS version acceptable for connections
   252  func WithMinTLSVersion(minVersion uint16) Option {
   253  	return func(ctx context.Context, c *Driver) error {
   254  		c.options = append(c.options, config.WithMinTLSVersion(minVersion))
   255  
   256  		return nil
   257  	}
   258  }
   259  
   260  // WithTLSSInsecureSkipVerify applies InsecureSkipVerify flag to TLS config
   261  func WithTLSSInsecureSkipVerify() Option {
   262  	return func(ctx context.Context, c *Driver) error {
   263  		c.options = append(c.options, config.WithTLSSInsecureSkipVerify())
   264  
   265  		return nil
   266  	}
   267  }
   268  
   269  // WithLogger add enables logging for selected tracing events.
   270  //
   271  // See trace package documentation for details.
   272  func WithLogger(l log.Logger, details trace.Detailer, opts ...log.Option) Option {
   273  	return func(ctx context.Context, c *Driver) error {
   274  		c.logger = l
   275  		c.loggerOpts = opts
   276  		c.loggerDetails = details
   277  
   278  		return nil
   279  	}
   280  }
   281  
   282  // WithAnonymousCredentials force to make requests withou authentication.
   283  func WithAnonymousCredentials() Option {
   284  	return WithCredentials(
   285  		credentials.NewAnonymousCredentials(credentials.WithSourceInfo("ydb.WithAnonymousCredentials()")),
   286  	)
   287  }
   288  
   289  // WithCreateCredentialsFunc add callback funcion to provide requests credentials
   290  func WithCreateCredentialsFunc(createCredentials func(ctx context.Context) (credentials.Credentials, error)) Option {
   291  	return func(ctx context.Context, c *Driver) error {
   292  		creds, err := createCredentials(ctx)
   293  		if err != nil {
   294  			return xerrors.WithStackTrace(err)
   295  		}
   296  		c.options = append(c.options, config.WithCredentials(creds))
   297  
   298  		return nil
   299  	}
   300  }
   301  
   302  // WithCredentials in conjunction with Driver.With function prohibit reuse of conn pool.
   303  // Thus, Driver.With will effectively create totally separate Driver.
   304  func WithCredentials(c credentials.Credentials) Option {
   305  	return WithCreateCredentialsFunc(func(context.Context) (credentials.Credentials, error) {
   306  		return c, nil
   307  	})
   308  }
   309  
   310  func WithBalancer(balancer *balancerConfig.Config) Option {
   311  	return func(ctx context.Context, c *Driver) error {
   312  		c.options = append(c.options, config.WithBalancer(balancer))
   313  
   314  		return nil
   315  	}
   316  }
   317  
   318  // WithDialTimeout sets timeout for establishing new Driver to cluster
   319  //
   320  // Default dial timeout is config.DefaultDialTimeout
   321  func WithDialTimeout(timeout time.Duration) Option {
   322  	return func(ctx context.Context, c *Driver) error {
   323  		c.options = append(c.options, config.WithDialTimeout(timeout))
   324  
   325  		return nil
   326  	}
   327  }
   328  
   329  // With collects additional configuration options.
   330  //
   331  // This option does not replace collected option, instead it will append provided options.
   332  func With(options ...config.Option) Option {
   333  	return func(ctx context.Context, c *Driver) error {
   334  		c.options = append(c.options, options...)
   335  
   336  		return nil
   337  	}
   338  }
   339  
   340  // MergeOptions concatentaes provided options to one cumulative value.
   341  func MergeOptions(opts ...Option) Option {
   342  	return func(ctx context.Context, c *Driver) error {
   343  		for _, opt := range opts {
   344  			if opt != nil {
   345  				if err := opt(ctx, c); err != nil {
   346  					return xerrors.WithStackTrace(err)
   347  				}
   348  			}
   349  		}
   350  
   351  		return nil
   352  	}
   353  }
   354  
   355  // WithDiscoveryInterval sets interval between cluster discovery calls.
   356  func WithDiscoveryInterval(discoveryInterval time.Duration) Option {
   357  	return func(ctx context.Context, c *Driver) error {
   358  		c.discoveryOptions = append(c.discoveryOptions, discoveryConfig.WithInterval(discoveryInterval))
   359  
   360  		return nil
   361  	}
   362  }
   363  
   364  // WithRetryBudget sets retry budget for all calls of all retryers.
   365  //
   366  // Experimental: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#experimental
   367  func WithRetryBudget(b budget.Budget) Option {
   368  	return func(ctx context.Context, c *Driver) error {
   369  		c.options = append(c.options, config.WithRetryBudget(b))
   370  
   371  		return nil
   372  	}
   373  }
   374  
   375  // WithTraceDriver appends trace.Driver into driver traces
   376  func WithTraceDriver(t trace.Driver, opts ...trace.DriverComposeOption) Option { //nolint:gocritic
   377  	return func(ctx context.Context, c *Driver) error {
   378  		c.options = append(c.options, config.WithTrace(t, opts...))
   379  
   380  		return nil
   381  	}
   382  }
   383  
   384  // WithTraceRetry appends trace.Retry into retry traces
   385  func WithTraceRetry(t trace.Retry, opts ...trace.RetryComposeOption) Option {
   386  	return func(ctx context.Context, c *Driver) error {
   387  		c.options = append(c.options,
   388  			config.WithTraceRetry(&t, append(
   389  				[]trace.RetryComposeOption{
   390  					trace.WithRetryPanicCallback(c.panicCallback),
   391  				},
   392  				opts...,
   393  			)...),
   394  		)
   395  
   396  		return nil
   397  	}
   398  }
   399  
   400  // WithCertificate appends certificate to TLS config root certificates
   401  func WithCertificate(cert *x509.Certificate) Option {
   402  	return func(ctx context.Context, c *Driver) error {
   403  		c.options = append(c.options, config.WithCertificate(cert))
   404  
   405  		return nil
   406  	}
   407  }
   408  
   409  // WithCertificatesFromFile appends certificates by filepath to TLS config root certificates
   410  func WithCertificatesFromFile(caFile string, opts ...certificates.FromFileOption) Option {
   411  	if len(caFile) > 0 && caFile[0] == '~' {
   412  		if home, err := os.UserHomeDir(); err == nil {
   413  			caFile = filepath.Join(home, caFile[1:])
   414  		}
   415  	}
   416  	if file, err := filepath.Abs(caFile); err == nil {
   417  		caFile = file
   418  	}
   419  	if file, err := filepath.EvalSymlinks(caFile); err == nil {
   420  		caFile = file
   421  	}
   422  
   423  	return func(ctx context.Context, c *Driver) error {
   424  		certs, err := certificates.FromFile(caFile, opts...)
   425  		if err != nil {
   426  			return xerrors.WithStackTrace(err)
   427  		}
   428  		for _, cert := range certs {
   429  			if err := WithCertificate(cert)(ctx, c); err != nil {
   430  				return xerrors.WithStackTrace(err)
   431  			}
   432  		}
   433  
   434  		return nil
   435  	}
   436  }
   437  
   438  // WithTLSConfig replaces older TLS config
   439  //
   440  // Warning: all early TLS config changes (such as WithCertificate, WithCertificatesFromFile, WithCertificatesFromPem,
   441  // WithMinTLSVersion, WithTLSSInsecureSkipVerify) will be lost
   442  func WithTLSConfig(tlsConfig *tls.Config) Option {
   443  	return func(ctx context.Context, c *Driver) error {
   444  		c.options = append(c.options, config.WithTLSConfig(tlsConfig))
   445  
   446  		return nil
   447  	}
   448  }
   449  
   450  // WithCertificatesFromPem appends certificates from pem-encoded data to TLS config root certificates
   451  func WithCertificatesFromPem(bytes []byte, opts ...certificates.FromPemOption) Option {
   452  	return func(ctx context.Context, c *Driver) error {
   453  		certs, err := certificates.FromPem(bytes, opts...)
   454  		if err != nil {
   455  			return xerrors.WithStackTrace(err)
   456  		}
   457  		for _, cert := range certs {
   458  			_ = WithCertificate(cert)(ctx, c)
   459  		}
   460  
   461  		return nil
   462  	}
   463  }
   464  
   465  // WithTableConfigOption collects additional configuration options for table.Client.
   466  // This option does not replace collected option, instead it will appen provided options.
   467  func WithTableConfigOption(option tableConfig.Option) Option {
   468  	return func(ctx context.Context, c *Driver) error {
   469  		c.tableOptions = append(c.tableOptions, option)
   470  
   471  		return nil
   472  	}
   473  }
   474  
   475  // WithQueryConfigOption collects additional configuration options for query.Client.
   476  // This option does not replace collected option, instead it will appen provided options.
   477  func WithQueryConfigOption(option queryConfig.Option) Option {
   478  	return func(ctx context.Context, c *Driver) error {
   479  		c.queryOptions = append(c.queryOptions, option)
   480  
   481  		return nil
   482  	}
   483  }
   484  
   485  // WithSessionPoolSizeLimit set max size of internal sessions pool in table.Client
   486  func WithSessionPoolSizeLimit(sizeLimit int) Option {
   487  	return func(ctx context.Context, d *Driver) error {
   488  		d.tableOptions = append(d.tableOptions, tableConfig.WithSizeLimit(sizeLimit))
   489  		d.queryOptions = append(d.queryOptions, queryConfig.WithPoolLimit(sizeLimit))
   490  
   491  		return nil
   492  	}
   493  }
   494  
   495  // WithSessionPoolSessionUsageLimit set max count for use session
   496  func WithSessionPoolSessionUsageLimit(sessionUsageLimit uint64) Option {
   497  	return func(ctx context.Context, d *Driver) error {
   498  		d.tableOptions = append(d.tableOptions, tableConfig.WithPoolSessionUsageLimit(sessionUsageLimit))
   499  		d.queryOptions = append(d.queryOptions, queryConfig.WithPoolSessionUsageLimit(sessionUsageLimit))
   500  
   501  		return nil
   502  	}
   503  }
   504  
   505  // WithLazyTx enables lazy transactions in query service client
   506  //
   507  // Lazy transaction means that begin call will be noop and first execute creates interactive transaction with given
   508  // transaction settings
   509  //
   510  // Experimental: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#experimental
   511  func WithLazyTx(lazyTx bool) Option {
   512  	return func(ctx context.Context, d *Driver) error {
   513  		d.queryOptions = append(d.queryOptions, queryConfig.WithLazyTx(lazyTx))
   514  
   515  		return nil
   516  	}
   517  }
   518  
   519  // WithSessionPoolIdleThreshold defines interval for idle sessions
   520  func WithSessionPoolIdleThreshold(idleThreshold time.Duration) Option {
   521  	return func(ctx context.Context, c *Driver) error {
   522  		c.tableOptions = append(c.tableOptions, tableConfig.WithIdleThreshold(idleThreshold))
   523  		c.databaseSQLOptions = append(
   524  			c.databaseSQLOptions,
   525  			xsql.WithIdleThreshold(idleThreshold),
   526  		)
   527  
   528  		return nil
   529  	}
   530  }
   531  
   532  // WithSessionPoolSessionIdleTimeToLive limits maximum time to live of idle session
   533  // If idleTimeToLive is less than or equal to zero then sessions will not be closed by idle
   534  func WithSessionPoolSessionIdleTimeToLive(idleThreshold time.Duration) Option {
   535  	return func(ctx context.Context, c *Driver) error {
   536  		c.queryOptions = append(c.queryOptions, queryConfig.WithSessionIdleTimeToLive(idleThreshold))
   537  
   538  		return nil
   539  	}
   540  }
   541  
   542  // WithSessionPoolCreateSessionTimeout set timeout for new session creation process in table.Client
   543  func WithSessionPoolCreateSessionTimeout(createSessionTimeout time.Duration) Option {
   544  	return func(ctx context.Context, c *Driver) error {
   545  		c.tableOptions = append(c.tableOptions, tableConfig.WithCreateSessionTimeout(createSessionTimeout))
   546  		c.queryOptions = append(c.queryOptions, queryConfig.WithSessionCreateTimeout(createSessionTimeout))
   547  
   548  		return nil
   549  	}
   550  }
   551  
   552  // WithSessionPoolDeleteTimeout set timeout to gracefully close deleting session in table.Client
   553  func WithSessionPoolDeleteTimeout(deleteTimeout time.Duration) Option {
   554  	return func(ctx context.Context, c *Driver) error {
   555  		c.tableOptions = append(c.tableOptions, tableConfig.WithDeleteTimeout(deleteTimeout))
   556  		c.queryOptions = append(c.queryOptions, queryConfig.WithSessionDeleteTimeout(deleteTimeout))
   557  
   558  		return nil
   559  	}
   560  }
   561  
   562  // WithSessionPoolKeepAliveMinSize set minimum sessions should be keeped alive in table.Client
   563  //
   564  // Deprecated: use WithApplicationName instead.
   565  // Will be removed after Oct 2024.
   566  // Read about versioning policy: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#deprecated
   567  func WithSessionPoolKeepAliveMinSize(keepAliveMinSize int) Option {
   568  	return func(ctx context.Context, c *Driver) error { return nil }
   569  }
   570  
   571  // WithSessionPoolKeepAliveTimeout set timeout of keep alive requests for session in table.Client
   572  //
   573  // Deprecated: use WithApplicationName instead.
   574  // Will be removed after Oct 2024.
   575  // Read about versioning policy: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#deprecated
   576  func WithSessionPoolKeepAliveTimeout(keepAliveTimeout time.Duration) Option {
   577  	return func(ctx context.Context, c *Driver) error { return nil }
   578  }
   579  
   580  // WithIgnoreTruncated disables errors on truncated flag
   581  func WithIgnoreTruncated() Option {
   582  	return func(ctx context.Context, c *Driver) error {
   583  		c.tableOptions = append(c.tableOptions, tableConfig.WithIgnoreTruncated())
   584  
   585  		return nil
   586  	}
   587  }
   588  
   589  // WithPanicCallback specified behavior on panic
   590  // Warning: WithPanicCallback must be defined on start of all options
   591  // (before `WithTrace{Driver,Table,Scheme,Scripting,Coordination,Ratelimiter}` and other options)
   592  // If not defined - panic would not intercept with driver
   593  func WithPanicCallback(panicCallback func(e interface{})) Option {
   594  	return func(ctx context.Context, c *Driver) error {
   595  		c.panicCallback = panicCallback
   596  		c.options = append(c.options, config.WithPanicCallback(panicCallback))
   597  
   598  		return nil
   599  	}
   600  }
   601  
   602  // WithSharedBalancer sets balancer from parent driver to child driver
   603  func WithSharedBalancer(parent *Driver) Option {
   604  	return func(ctx context.Context, c *Driver) error {
   605  		c.balancer = parent.balancer
   606  
   607  		return nil
   608  	}
   609  }
   610  
   611  // WithTraceTable appends trace.Table into table traces
   612  func WithTraceTable(t trace.Table, opts ...trace.TableComposeOption) Option { //nolint:gocritic
   613  	return func(ctx context.Context, c *Driver) error {
   614  		c.tableOptions = append(
   615  			c.tableOptions,
   616  			tableConfig.WithTrace(
   617  				&t,
   618  				append(
   619  					[]trace.TableComposeOption{
   620  						trace.WithTablePanicCallback(c.panicCallback),
   621  					},
   622  					opts...,
   623  				)...,
   624  			),
   625  		)
   626  
   627  		return nil
   628  	}
   629  }
   630  
   631  // WithTraceQuery appends trace.Query into query traces
   632  func WithTraceQuery(t trace.Query, opts ...trace.QueryComposeOption) Option { //nolint:gocritic
   633  	return func(ctx context.Context, c *Driver) error {
   634  		c.queryOptions = append(
   635  			c.queryOptions,
   636  			queryConfig.WithTrace(&t,
   637  				append(
   638  					[]trace.QueryComposeOption{
   639  						trace.WithQueryPanicCallback(c.panicCallback),
   640  					},
   641  					opts...,
   642  				)...,
   643  			),
   644  		)
   645  
   646  		return nil
   647  	}
   648  }
   649  
   650  // WithTraceScripting scripting trace option
   651  func WithTraceScripting(t trace.Scripting, opts ...trace.ScriptingComposeOption) Option {
   652  	return func(ctx context.Context, c *Driver) error {
   653  		c.scriptingOptions = append(
   654  			c.scriptingOptions,
   655  			scriptingConfig.WithTrace(
   656  				t,
   657  				append(
   658  					[]trace.ScriptingComposeOption{
   659  						trace.WithScriptingPanicCallback(c.panicCallback),
   660  					},
   661  					opts...,
   662  				)...,
   663  			),
   664  		)
   665  
   666  		return nil
   667  	}
   668  }
   669  
   670  // WithTraceScheme returns scheme trace option
   671  func WithTraceScheme(t trace.Scheme, opts ...trace.SchemeComposeOption) Option {
   672  	return func(ctx context.Context, c *Driver) error {
   673  		c.schemeOptions = append(
   674  			c.schemeOptions,
   675  			schemeConfig.WithTrace(
   676  				t,
   677  				append(
   678  					[]trace.SchemeComposeOption{
   679  						trace.WithSchemePanicCallback(c.panicCallback),
   680  					},
   681  					opts...,
   682  				)...,
   683  			),
   684  		)
   685  
   686  		return nil
   687  	}
   688  }
   689  
   690  // WithTraceCoordination returns coordination trace option
   691  func WithTraceCoordination(t trace.Coordination, opts ...trace.CoordinationComposeOption) Option { //nolint:gocritic
   692  	return func(ctx context.Context, c *Driver) error {
   693  		c.coordinationOptions = append(
   694  			c.coordinationOptions,
   695  			coordinationConfig.WithTrace(
   696  				&t,
   697  				append(
   698  					[]trace.CoordinationComposeOption{
   699  						trace.WithCoordinationPanicCallback(c.panicCallback),
   700  					},
   701  					opts...,
   702  				)...,
   703  			),
   704  		)
   705  
   706  		return nil
   707  	}
   708  }
   709  
   710  // WithTraceRatelimiter returns ratelimiter trace option
   711  func WithTraceRatelimiter(t trace.Ratelimiter, opts ...trace.RatelimiterComposeOption) Option {
   712  	return func(ctx context.Context, c *Driver) error {
   713  		c.ratelimiterOptions = append(
   714  			c.ratelimiterOptions,
   715  			ratelimiterConfig.WithTrace(
   716  				t,
   717  				append(
   718  					[]trace.RatelimiterComposeOption{
   719  						trace.WithRatelimiterPanicCallback(c.panicCallback),
   720  					},
   721  					opts...,
   722  				)...,
   723  			),
   724  		)
   725  
   726  		return nil
   727  	}
   728  }
   729  
   730  // WithRatelimiterOptions returns reatelimiter option
   731  func WithRatelimiterOptions(opts ...ratelimiterConfig.Option) Option {
   732  	return func(ctx context.Context, c *Driver) error {
   733  		c.ratelimiterOptions = append(c.ratelimiterOptions, opts...)
   734  
   735  		return nil
   736  	}
   737  }
   738  
   739  // WithTraceDiscovery adds configured discovery tracer to Driver
   740  func WithTraceDiscovery(t trace.Discovery, opts ...trace.DiscoveryComposeOption) Option {
   741  	return func(ctx context.Context, c *Driver) error {
   742  		c.discoveryOptions = append(
   743  			c.discoveryOptions,
   744  			discoveryConfig.WithTrace(
   745  				t,
   746  				append(
   747  					[]trace.DiscoveryComposeOption{
   748  						trace.WithDiscoveryPanicCallback(c.panicCallback),
   749  					},
   750  					opts...,
   751  				)...,
   752  			),
   753  		)
   754  
   755  		return nil
   756  	}
   757  }
   758  
   759  // WithTraceTopic adds configured discovery tracer to Driver
   760  func WithTraceTopic(t trace.Topic, opts ...trace.TopicComposeOption) Option { //nolint:gocritic
   761  	return func(ctx context.Context, c *Driver) error {
   762  		c.topicOptions = append(
   763  			c.topicOptions,
   764  			topicoptions.WithTrace(
   765  				t,
   766  				append(
   767  					[]trace.TopicComposeOption{
   768  						trace.WithTopicPanicCallback(c.panicCallback),
   769  					},
   770  					opts...,
   771  				)...,
   772  			),
   773  		)
   774  
   775  		return nil
   776  	}
   777  }
   778  
   779  // WithTraceDatabaseSQL adds configured discovery tracer to Driver
   780  func WithTraceDatabaseSQL(t trace.DatabaseSQL, opts ...trace.DatabaseSQLComposeOption) Option { //nolint:gocritic
   781  	return func(ctx context.Context, c *Driver) error {
   782  		c.databaseSQLOptions = append(
   783  			c.databaseSQLOptions,
   784  			xsql.WithTrace(
   785  				&t,
   786  				append(
   787  					[]trace.DatabaseSQLComposeOption{
   788  						trace.WithDatabaseSQLPanicCallback(c.panicCallback),
   789  					},
   790  					opts...,
   791  				)...,
   792  			),
   793  		)
   794  
   795  		return nil
   796  	}
   797  }
   798  
   799  // Private technical options for correct copies processing
   800  
   801  func withOnClose(onClose func(c *Driver)) Option {
   802  	return func(ctx context.Context, c *Driver) error {
   803  		c.onClose = append(c.onClose, onClose)
   804  
   805  		return nil
   806  	}
   807  }
   808  
   809  func withConnPool(pool *conn.Pool) Option {
   810  	return func(ctx context.Context, c *Driver) error {
   811  		c.pool = pool
   812  
   813  		return pool.Take(ctx)
   814  	}
   815  }