github.com/ydb-platform/ydb-go-sdk/v3@v3.89.2/internal/table/config/config.go (about)

     1  package config
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/jonboulle/clockwork"
     7  
     8  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/config"
     9  	"github.com/ydb-platform/ydb-go-sdk/v3/trace"
    10  )
    11  
    12  const (
    13  	DefaultSessionPoolDeleteTimeout        = 500 * time.Millisecond
    14  	DefaultSessionPoolCreateSessionTimeout = 5 * time.Second
    15  	DefaultSessionPoolSizeLimit            = 50
    16  	DefaultSessionPoolIdleThreshold        = 5 * time.Minute
    17  
    18  	// Deprecated: table client do not supports background session keep-aliving now.
    19  	// Will be removed after Oct 2024.
    20  	// Read about versioning policy: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#deprecated
    21  	DefaultKeepAliveMinSize = 10
    22  
    23  	// Deprecated: table client do not supports background session keep-aliving now.
    24  	// Will be removed after Oct 2024.
    25  	// Read about versioning policy: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#deprecated
    26  	DefaultIdleKeepAliveThreshold = 2
    27  
    28  	// Deprecated: table client do not supports background session keep-aliving now.
    29  	// Will be removed after Oct 2024.
    30  	// Read about versioning policy: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#deprecated
    31  	DefaultSessionPoolKeepAliveTimeout = 500 * time.Millisecond
    32  )
    33  
    34  func New(opts ...Option) *Config {
    35  	c := defaults()
    36  	for _, opt := range opts {
    37  		if opt != nil {
    38  			opt(c)
    39  		}
    40  	}
    41  
    42  	return c
    43  }
    44  
    45  type Option func(*Config)
    46  
    47  // With applies common configuration params
    48  func With(config config.Common) Option {
    49  	return func(c *Config) {
    50  		c.Common = config
    51  	}
    52  }
    53  
    54  // WithSizeLimit defines upper bound of pooled sessions.
    55  // If sizeLimit is less than or equal to zero then the
    56  // DefaultSessionPoolSizeLimit variable is used as a limit.
    57  func WithSizeLimit(sizeLimit int) Option {
    58  	return func(c *Config) {
    59  		if sizeLimit > 0 {
    60  			c.sizeLimit = sizeLimit
    61  		}
    62  	}
    63  }
    64  
    65  func WithPoolSessionUsageLimit(sessionUsageLimit uint64) Option {
    66  	return func(c *Config) {
    67  		c.sessionUsageLimit = sessionUsageLimit
    68  	}
    69  }
    70  
    71  // WithKeepAliveMinSize defines lower bound for sessions in the pool. If there are more sessions open, then
    72  // the excess idle ones will be closed and removed after IdleKeepAliveThreshold is reached for each of them.
    73  // If keepAliveMinSize is less than zero, then no sessions will be preserved
    74  // If keepAliveMinSize is zero, the DefaultKeepAliveMinSize is used
    75  //
    76  // Deprecated: table client do not supports background session keep-aliving now.
    77  // Will be removed after Oct 2024.
    78  // Read about versioning policy: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#deprecated
    79  func WithKeepAliveMinSize(keepAliveMinSize int) Option {
    80  	return func(c *Config) {}
    81  }
    82  
    83  // WithIdleKeepAliveThreshold defines number of keepAlive messages to call before the
    84  // session is removed if it is an excess session (see KeepAliveMinSize)
    85  // This means that session will be deleted after the expiration of lifetime = IdleThreshold * IdleKeepAliveThreshold
    86  // If IdleKeepAliveThreshold is less than zero then it will be treated as infinite and no sessions will
    87  // be removed ever.
    88  // If IdleKeepAliveThreshold is equal to zero, it will be set to DefaultIdleKeepAliveThreshold
    89  //
    90  // Deprecated: table client do not supports background session keep-aliving now.
    91  // Will be removed after Oct 2024.
    92  // Read about versioning policy: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#deprecated
    93  func WithIdleKeepAliveThreshold(idleKeepAliveThreshold int) Option {
    94  	return func(c *Config) {}
    95  }
    96  
    97  // WithIdleThreshold sets maximum duration between any activity within session.
    98  // If this threshold reached, session will be closed.
    99  //
   100  // If idleThreshold is less than zero then there is no idle limit.
   101  // If idleThreshold is zero, then the DefaultSessionPoolIdleThreshold value is used.
   102  func WithIdleThreshold(idleThreshold time.Duration) Option {
   103  	return func(c *Config) {
   104  		if idleThreshold < 0 {
   105  			idleThreshold = 0
   106  		}
   107  		c.idleThreshold = idleThreshold
   108  	}
   109  }
   110  
   111  // WithKeepAliveTimeout limits maximum time spent on KeepAlive request
   112  // If keepAliveTimeout is less than or equal to zero then the DefaultSessionPoolKeepAliveTimeout is used.
   113  //
   114  // Deprecated: table client do not supports background session keep-aliving now.
   115  // Will be removed after Oct 2024.
   116  // Read about versioning policy: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#deprecated
   117  func WithKeepAliveTimeout(keepAliveTimeout time.Duration) Option {
   118  	return func(c *Config) {}
   119  }
   120  
   121  // WithCreateSessionTimeout limits maximum time spent on Create session request
   122  // If createSessionTimeout is less than or equal to zero then no used timeout on create session request
   123  func WithCreateSessionTimeout(createSessionTimeout time.Duration) Option {
   124  	return func(c *Config) {
   125  		if createSessionTimeout > 0 {
   126  			c.createSessionTimeout = createSessionTimeout
   127  		} else {
   128  			c.createSessionTimeout = 0
   129  		}
   130  	}
   131  }
   132  
   133  // WithDeleteTimeout limits maximum time spent on Delete request
   134  // If deleteTimeout is less than or equal to zero then the DefaultSessionPoolDeleteTimeout is used.
   135  func WithDeleteTimeout(deleteTimeout time.Duration) Option {
   136  	return func(c *Config) {
   137  		if deleteTimeout > 0 {
   138  			c.deleteTimeout = deleteTimeout
   139  		}
   140  	}
   141  }
   142  
   143  // WithTrace appends table trace to early defined traces
   144  func WithTrace(trace *trace.Table, opts ...trace.TableComposeOption) Option {
   145  	return func(c *Config) {
   146  		c.trace = c.trace.Compose(trace, opts...)
   147  	}
   148  }
   149  
   150  // WithIgnoreTruncated disables errors on truncated flag
   151  func WithIgnoreTruncated() Option {
   152  	return func(c *Config) {
   153  		c.ignoreTruncated = true
   154  	}
   155  }
   156  
   157  // WithClock replaces default clock
   158  func WithClock(clock clockwork.Clock) Option {
   159  	return func(c *Config) {
   160  		c.clock = clock
   161  	}
   162  }
   163  
   164  // Config is a configuration of table client
   165  type Config struct {
   166  	config.Common
   167  
   168  	sizeLimit         int
   169  	sessionUsageLimit uint64
   170  
   171  	createSessionTimeout time.Duration
   172  	deleteTimeout        time.Duration
   173  	idleThreshold        time.Duration
   174  
   175  	ignoreTruncated bool
   176  
   177  	trace *trace.Table
   178  
   179  	clock clockwork.Clock
   180  }
   181  
   182  // Trace defines trace over table client calls
   183  func (c *Config) Trace() *trace.Table {
   184  	return c.trace
   185  }
   186  
   187  // Clock defines clock
   188  func (c *Config) Clock() clockwork.Clock {
   189  	return c.clock
   190  }
   191  
   192  // SizeLimit is an upper bound of pooled sessions.
   193  // If SizeLimit is less than or equal to zero then the
   194  // DefaultSessionPoolSizeLimit variable is used as a limit.
   195  func (c *Config) SizeLimit() int {
   196  	return c.sizeLimit
   197  }
   198  
   199  func (c *Config) SessionUsageLimit() uint64 {
   200  	return c.sessionUsageLimit
   201  }
   202  
   203  // KeepAliveMinSize is a lower bound for sessions in the pool. If there are more sessions open, then
   204  // the excess idle ones will be closed and removed after IdleKeepAliveThreshold is reached for each of them.
   205  // If KeepAliveMinSize is less than zero, then no sessions will be preserved
   206  // If KeepAliveMinSize is zero, the DefaultKeepAliveMinSize is used
   207  //
   208  // Deprecated: table client do not supports background session keep-aliving now.
   209  // Will be removed after Oct 2024.
   210  // Read about versioning policy: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#deprecated
   211  func (c *Config) KeepAliveMinSize() int {
   212  	return DefaultKeepAliveMinSize
   213  }
   214  
   215  // IgnoreTruncated specifies behavior on truncated flag
   216  func (c *Config) IgnoreTruncated() bool {
   217  	return c.ignoreTruncated
   218  }
   219  
   220  // IdleKeepAliveThreshold is a number of keepAlive messages to call before the
   221  // session is removed if it is an excess session (see KeepAliveMinSize)
   222  // This means that session will be deleted after the expiration of lifetime = IdleThreshold * IdleKeepAliveThreshold
   223  // If IdleKeepAliveThreshold is less than zero then it will be treated as infinite and no sessions will
   224  // be removed ever.
   225  // If IdleKeepAliveThreshold is equal to zero, it will be set to DefaultIdleKeepAliveThreshold
   226  //
   227  // Deprecated: table client do not supports background session keep-aliving now.
   228  // Will be removed after Oct 2024.
   229  // Read about versioning policy: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#deprecated
   230  func (c *Config) IdleKeepAliveThreshold() int {
   231  	return DefaultIdleKeepAliveThreshold
   232  }
   233  
   234  // IdleThreshold is a maximum duration between any activity within session.
   235  // If this threshold reached, idle session will be closed
   236  //
   237  // If IdleThreshold is less than zero then there is no idle limit.
   238  // If IdleThreshold is zero, then the DefaultSessionPoolIdleThreshold value is used.
   239  func (c *Config) IdleThreshold() time.Duration {
   240  	return c.idleThreshold
   241  }
   242  
   243  // KeepAliveTimeout limits maximum time spent on KeepAlive request
   244  // If KeepAliveTimeout is less than or equal to zero then the DefaultSessionPoolKeepAliveTimeout is used.
   245  //
   246  // Deprecated: table client do not supports background session keep-aliving now.
   247  // Will be removed after Oct 2024.
   248  // Read about versioning policy: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#deprecated
   249  func (c *Config) KeepAliveTimeout() time.Duration {
   250  	return DefaultSessionPoolKeepAliveTimeout
   251  }
   252  
   253  // CreateSessionTimeout limits maximum time spent on Create session request
   254  func (c *Config) CreateSessionTimeout() time.Duration {
   255  	return c.createSessionTimeout
   256  }
   257  
   258  // DeleteTimeout limits maximum time spent on Delete request
   259  //
   260  // If DeleteTimeout is less than or equal to zero then the DefaultSessionPoolDeleteTimeout is used.
   261  func (c *Config) DeleteTimeout() time.Duration {
   262  	return c.deleteTimeout
   263  }
   264  
   265  func defaults() *Config {
   266  	return &Config{
   267  		sizeLimit:            DefaultSessionPoolSizeLimit,
   268  		createSessionTimeout: DefaultSessionPoolCreateSessionTimeout,
   269  		deleteTimeout:        DefaultSessionPoolDeleteTimeout,
   270  		idleThreshold:        DefaultSessionPoolIdleThreshold,
   271  		clock:                clockwork.NewRealClock(),
   272  		trace:                &trace.Table{},
   273  	}
   274  }