-
Notifications
You must be signed in to change notification settings - Fork 245
/
options.go
242 lines (217 loc) · 7.35 KB
/
options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package mysql
import (
"fmt"
"time"
)
const (
errQuantizationTooLarge = "revision quantization interval (%s) must be less than GC window (%s)"
defaultGarbageCollectionWindow = 24 * time.Hour
defaultGarbageCollectionInterval = time.Minute * 3
defaultGarbageCollectionMaxOperationTime = time.Minute
defaultMaxOpenConns = 20
defaultConnMaxIdleTime = 30 * time.Minute
defaultConnMaxLifetime = 30 * time.Minute
defaultWatchBufferLength = 128
defaultUsersetBatchSize = 1024
defaultQuantization = 5 * time.Second
defaultMaxRevisionStalenessPercent = 0.1
defaultEnablePrometheusStats = false
defaultMaxRetries = 8
defaultGCEnabled = true
)
type mysqlOptions struct {
revisionQuantization time.Duration
gcWindow time.Duration
gcInterval time.Duration
gcMaxOperationTime time.Duration
maxRevisionStalenessPercent float64
watchBufferLength uint16
tablePrefix string
enablePrometheusStats bool
maxOpenConns int
connMaxIdleTime time.Duration
connMaxLifetime time.Duration
splitAtUsersetCount uint16
analyzeBeforeStats bool
maxRetries uint8
lockWaitTimeoutSeconds *uint8
gcEnabled bool
}
// Option provides the facility to configure how clients within the
// MySQL datastore interact with the running MySQL database.
type Option func(*mysqlOptions)
func generateConfig(options []Option) (mysqlOptions, error) {
computed := mysqlOptions{
gcWindow: defaultGarbageCollectionWindow,
gcInterval: defaultGarbageCollectionInterval,
gcMaxOperationTime: defaultGarbageCollectionMaxOperationTime,
watchBufferLength: defaultWatchBufferLength,
maxOpenConns: defaultMaxOpenConns,
connMaxIdleTime: defaultConnMaxIdleTime,
connMaxLifetime: defaultConnMaxLifetime,
splitAtUsersetCount: defaultUsersetBatchSize,
revisionQuantization: defaultQuantization,
maxRevisionStalenessPercent: defaultMaxRevisionStalenessPercent,
enablePrometheusStats: defaultEnablePrometheusStats,
maxRetries: defaultMaxRetries,
gcEnabled: defaultGCEnabled,
}
for _, option := range options {
option(&computed)
}
// Run any checks on the config that need to be done
if computed.revisionQuantization >= computed.gcWindow {
return computed, fmt.Errorf(
errQuantizationTooLarge,
computed.revisionQuantization,
computed.gcWindow,
)
}
return computed, nil
}
// WatchBufferLength is the number of entries that can be stored in the watch
// buffer while awaiting read by the client.
//
// This value defaults to 128.
func WatchBufferLength(watchBufferLength uint16) Option {
return func(mo *mysqlOptions) {
mo.watchBufferLength = watchBufferLength
}
}
// RevisionQuantization is the time bucket size to which advertised
// revisions will be rounded.
//
// This value defaults to 5 seconds.
func RevisionQuantization(quantization time.Duration) Option {
return func(mo *mysqlOptions) {
mo.revisionQuantization = quantization
}
}
// MaxRevisionStalenessPercent is the amount of time, expressed as a percentage of
// the revision quantization window, that a previously computed rounded revision
// can still be advertised after the next rounded revision would otherwise be ready.
//
// This value defaults to 0.1 (10%).
func MaxRevisionStalenessPercent(stalenessPercent float64) Option {
return func(mo *mysqlOptions) {
mo.maxRevisionStalenessPercent = stalenessPercent
}
}
// GCWindow is the maximum age of a passed revision that will be considered
// valid.
//
// This value defaults to 24 hours.
func GCWindow(window time.Duration) Option {
return func(mo *mysqlOptions) {
mo.gcWindow = window
}
}
// GCInterval is the interval at which garbage collection will occur.
//
// This value defaults to 3 minutes.
func GCInterval(interval time.Duration) Option {
return func(mo *mysqlOptions) {
mo.gcInterval = interval
}
}
// MaxRetries is the maximum number of times a retriable transaction will be
// client-side retried.
//
// Default: 10
func MaxRetries(maxRetries uint8) Option {
return func(mo *mysqlOptions) {
mo.maxRetries = maxRetries
}
}
// TablePrefix allows defining a MySQL table name prefix.
//
// No prefix is set by default
func TablePrefix(prefix string) Option {
return func(mo *mysqlOptions) {
mo.tablePrefix = prefix
}
}
// SplitAtUsersetCount is the batch size for which userset queries will be
// split into smaller queries.
//
// This defaults to 1024.
func SplitAtUsersetCount(splitAtUsersetCount uint16) Option {
return func(mo *mysqlOptions) {
mo.splitAtUsersetCount = splitAtUsersetCount
}
}
// WithEnablePrometheusStats marks whether Prometheus metrics provided by Go's database/sql package
// are enabled.
//
// Prometheus metrics are disabled by default.
func WithEnablePrometheusStats(enablePrometheusStats bool) Option {
return func(mo *mysqlOptions) {
mo.enablePrometheusStats = enablePrometheusStats
}
}
// ConnMaxIdleTime is the duration after which an idle connection will be
// automatically closed.
// See https://pkg.go.dev/database/sql#DB.SetConnMaxIdleTime/
//
// This value defaults to having no maximum.
func ConnMaxIdleTime(idle time.Duration) Option {
return func(mo *mysqlOptions) {
mo.connMaxIdleTime = idle
}
}
// ConnMaxLifetime is the duration since creation after which a connection will
// be automatically closed.
// See https://pkg.go.dev/database/sql#DB.SetConnMaxLifetime
//
// This value defaults to having no maximum.
func ConnMaxLifetime(lifetime time.Duration) Option {
return func(mo *mysqlOptions) {
mo.connMaxLifetime = lifetime
}
}
// MaxOpenConns is the maximum size of the connection pool.
// See https://pkg.go.dev/database/sql#DB.SetMaxOpenConns
//
// This value defaults to having no maximum.
func MaxOpenConns(conns int) Option {
return func(mo *mysqlOptions) {
mo.maxOpenConns = conns
}
}
// DebugAnalyzeBeforeStatistics signals to the Statistics method that it should
// run Analyze Table on the relationships table before returning statistics.
// This should only be used for debug and testing.
//
// Disabled by default.
func DebugAnalyzeBeforeStatistics() Option {
return func(mo *mysqlOptions) {
mo.analyzeBeforeStats = true
}
}
// OverrideLockWaitTimeout sets the lock wait timeout on each new connection established
// with the databases. As an OLTP service, the default of 50s is unbearably long to block
// a write for our service, so we suggest setting this value to the minimum of 1 second.
//
// Uses server default by default.
func OverrideLockWaitTimeout(seconds uint8) Option {
return func(mo *mysqlOptions) {
mo.lockWaitTimeoutSeconds = &seconds
}
}
// GCEnabled indicates whether garbage collection is enabled.
//
// GC is enabled by default.
func GCEnabled(isGCEnabled bool) Option {
return func(mo *mysqlOptions) {
mo.gcEnabled = isGCEnabled
}
}
// GCMaxOperationTime is the maximum operation time of a garbage collection
// pass before it times out.
//
// This value defaults to 1 minute.
func GCMaxOperationTime(time time.Duration) Option {
return func(mo *mysqlOptions) {
mo.gcMaxOperationTime = time
}
}