/
options.go
194 lines (167 loc) · 5.29 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
package postgres
import (
"fmt"
"time"
"github.com/alecthomas/units"
"github.com/authzed/spicedb/internal/datastore/common"
)
type postgresOptions struct {
connMaxIdleTime *time.Duration
connMaxLifetime *time.Duration
healthCheckPeriod *time.Duration
maxOpenConns *int
minOpenConns *int
watchBufferLength uint16
revisionFuzzingTimedelta time.Duration
gcWindow time.Duration
gcInterval time.Duration
gcMaxOperationTime time.Duration
splitAtEstimatedQuerySize units.Base2Bytes
enablePrometheusStats bool
logger *tracingLogger
}
const (
errFuzzingTooLarge = "revision fuzzing timedelta (%s) must be less than GC window (%s)"
defaultWatchBufferLength = 128
defaultGarbageCollectionWindow = 24 * time.Hour
defaultGarbageCollectionInterval = time.Minute * 3
defaultGarbageCollectionMaxOperationTime = time.Minute
)
// Option provides the facility to configure how clients within the
// Postgres datastore interact with the running Postgres database.
type Option func(*postgresOptions)
func generateConfig(options []Option) (postgresOptions, error) {
computed := postgresOptions{
gcWindow: defaultGarbageCollectionWindow,
gcInterval: defaultGarbageCollectionInterval,
gcMaxOperationTime: defaultGarbageCollectionMaxOperationTime,
watchBufferLength: defaultWatchBufferLength,
splitAtEstimatedQuerySize: common.DefaultSplitAtEstimatedQuerySize,
}
for _, option := range options {
option(&computed)
}
// Run any checks on the config that need to be done
if computed.revisionFuzzingTimedelta >= computed.gcWindow {
return computed, fmt.Errorf(
errFuzzingTooLarge,
computed.revisionFuzzingTimedelta,
computed.gcWindow,
)
}
return computed, nil
}
// SplitAtEstimatedQuerySize is the query size at which it is split into two
// (or more) queries.
//
// This value defaults to `common.DefaultSplitAtEstimatedQuerySize`.
func SplitAtEstimatedQuerySize(splitAtEstimatedQuerySize units.Base2Bytes) Option {
return func(po *postgresOptions) {
po.splitAtEstimatedQuerySize = splitAtEstimatedQuerySize
}
}
// ConnMaxIdleTime is the duration after which an idle connection will be
// automatically closed by the health check.
//
// This value defaults to having no maximum.
func ConnMaxIdleTime(idle time.Duration) Option {
return func(po *postgresOptions) {
po.connMaxIdleTime = &idle
}
}
// ConnMaxLifetime is the duration since creation after which a connection will
// be automatically closed.
//
// This value defaults to having no maximum.
func ConnMaxLifetime(lifetime time.Duration) Option {
return func(po *postgresOptions) {
po.connMaxLifetime = &lifetime
}
}
// HealthCheckPeriod is the interval by which idle Postgres client connections
// are health checked in order to keep them alive in a connection pool.
func HealthCheckPeriod(period time.Duration) Option {
return func(po *postgresOptions) {
po.healthCheckPeriod = &period
}
}
// MaxOpenConns is the maximum size of the connection pool.
//
// This value defaults to having no maximum.
func MaxOpenConns(conns int) Option {
return func(po *postgresOptions) {
po.maxOpenConns = &conns
}
}
// 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.
//
// This value defaults to zero.
func MinOpenConns(conns int) Option {
return func(po *postgresOptions) {
po.minOpenConns = &conns
}
}
// 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(po *postgresOptions) {
po.watchBufferLength = watchBufferLength
}
}
// RevisionFuzzingTimedelta is the time bucket size to which advertised
// revisions will be rounded.
//
// This value defaults to 5 seconds.
func RevisionFuzzingTimedelta(delta time.Duration) Option {
return func(po *postgresOptions) {
po.revisionFuzzingTimedelta = delta
}
}
// 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(po *postgresOptions) {
po.gcWindow = window
}
}
// GCInterval is the the interval at which garbage collection will occur.
//
// This value defaults to 3 minutes.
func GCInterval(interval time.Duration) Option {
return func(po *postgresOptions) {
po.gcInterval = interval
}
}
// 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(po *postgresOptions) {
po.gcMaxOperationTime = time
}
}
// EnablePrometheusStats enables Prometheus metrics provided by the Postgres
// clients being used by the datastore.
//
// Prometheus metrics are disable by default.
func EnablePrometheusStats() Option {
return func(po *postgresOptions) {
po.enablePrometheusStats = true
}
}
// EnableTracing enables trace-level logging for the Postgres clients being
// used by the datastore.
//
// Tracing is disabled by default.
func EnableTracing() Option {
return func(po *postgresOptions) {
po.logger = &tracingLogger{}
}
}