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