-
Notifications
You must be signed in to change notification settings - Fork 260
/
options.go
123 lines (103 loc) · 3.28 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
package crdb
import (
"fmt"
"time"
"github.com/alecthomas/units"
"github.com/authzed/spicedb/internal/datastore/common"
)
type crdbOptions struct {
connMaxIdleTime *time.Duration
connMaxLifetime *time.Duration
minOpenConns *int
maxOpenConns *int
watchBufferLength uint16
revisionQuantization time.Duration
gcWindow time.Duration
splitAtEstimatedQuerySize units.Base2Bytes
}
const (
errQuantizationTooLarge = "revision quantization (%s) must be less than GC window (%s)"
defaultRevisionQuantization = 5 * time.Second
defaultWatchBufferLength = 128
)
type CRDBOption func(*crdbOptions)
func generateConfig(options []CRDBOption) (crdbOptions, error) {
computed := crdbOptions{
gcWindow: 24 * time.Hour,
watchBufferLength: defaultWatchBufferLength,
revisionQuantization: defaultRevisionQuantization,
splitAtEstimatedQuerySize: common.DefaultSplitAtEstimatedQuerySize,
}
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
}
// SplitAtEstimatedQuerySize is the query size at which it is split into two (or more) queries.
// Default: common.DefaultSplitAtEstimatedQuerySize
func SplitAtEstimatedQuerySize(splitAtEstimatedQuerySize units.Base2Bytes) CRDBOption {
return func(po *crdbOptions) {
po.splitAtEstimatedQuerySize = splitAtEstimatedQuerySize
}
}
// ConnMaxIdleTime is the duration after which an idle connection will be automatically closed by
// the health check.
// Default: no maximum
func ConnMaxIdleTime(idle time.Duration) CRDBOption {
return func(po *crdbOptions) {
po.connMaxIdleTime = &idle
}
}
// ConnMaxLifetime is the duration since creation after which a connection will be automatically
// closed.
// Default: no maximum
func ConnMaxLifetime(lifetime time.Duration) CRDBOption {
return func(po *crdbOptions) {
po.connMaxLifetime = &lifetime
}
}
// MinOpenConns is the minimum size of the connection pool. The health check will increase the
// number of connections to this amount if it had dropped below.
// Default: 0
func MinOpenConns(conns int) CRDBOption {
return func(po *crdbOptions) {
po.minOpenConns = &conns
}
}
// MaxOpenConns is the maximum size of the connection pool.
// Default: no maximum
func MaxOpenConns(conns int) CRDBOption {
return func(po *crdbOptions) {
po.maxOpenConns = &conns
}
}
// WatchBufferLength is the number of entries that can be stored in the watch buffer while awaiting
// read by the client.
// Default: 128
func WatchBufferLength(watchBufferLength uint16) CRDBOption {
return func(po *crdbOptions) {
po.watchBufferLength = watchBufferLength
}
}
// RevisionQuantization is the time bucket size to which advertised revisions will be rounded.
// Default: 5s
func RevisionQuantization(bucketSize time.Duration) CRDBOption {
return func(po *crdbOptions) {
po.revisionQuantization = bucketSize
}
}
// GCWindow is the maximum age of a passed revision that will be considered valid.
// Default: 24h
func GCWindow(window time.Duration) CRDBOption {
return func(po *crdbOptions) {
po.gcWindow = window
}
}