-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfig.go
214 lines (181 loc) · 6.26 KB
/
config.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
package fauna
import (
"context"
"fmt"
"net/http"
"net/url"
"strings"
"time"
)
// ClientConfigFn configuration options for the [fauna.Client]
type ClientConfigFn func(*Client)
// Context specify the context to be used for the [fauna.Client]
func Context(ctx context.Context) ClientConfigFn {
return func(c *Client) { c.ctx = ctx }
}
// HTTPClient set the http.Client for the [fauna.Client]
func HTTPClient(client *http.Client) ClientConfigFn {
return func(c *Client) { c.http = client }
}
// AdditionalHeaders specify headers for the [fauna.Client]
func AdditionalHeaders(headers map[string]string) ClientConfigFn {
return func(c *Client) {
for k, v := range headers {
c.headers[k] = v
}
}
}
// MaxAttempts sets the maximum number of times the [fauna.Client]
// will attempt to run a query, retrying if appropriate.
func MaxAttempts(attempts int) ClientConfigFn {
return func(c *Client) { c.maxAttempts = attempts }
}
// MaxBackoff sets the maximum duration the [fauna.Client] will wait
// before retrying.
func MaxBackoff(backoff time.Duration) ClientConfigFn {
return func(c *Client) { c.maxBackoff = backoff }
}
// DefaultTypecheck set header on the [fauna.Client]
// Enable or disable typechecking of the query before evaluation. If
// not set, Fauna will use the value of the "typechecked" flag on
// the database configuration.
func DefaultTypecheck(enabled bool) ClientConfigFn {
return func(c *Client) {
c.setHeader(HeaderTypecheck, fmt.Sprintf("%v", enabled))
}
}
// Linearized set header on the [fauna.Client]
// If true, unconditionally run the query as strictly serialized.
// This affects read-only transactions. Transactions which write will always be strictly serialized.
func Linearized(enabled bool) ClientConfigFn {
return func(c *Client) {
c.setHeader(HeaderLinearized, fmt.Sprintf("%v", enabled))
}
}
// MaxContentionRetries set header on the [fauna.Client]
// The max number of times to retry the query if contention is encountered.
func MaxContentionRetries(i int) ClientConfigFn {
return func(c *Client) {
c.setHeader(HeaderMaxContentionRetries, fmt.Sprintf("%v", i))
}
}
// QueryTimeout set header on the [fauna.Client]
func QueryTimeout(d time.Duration) ClientConfigFn {
return func(c *Client) {
c.setHeader(HeaderQueryTimeoutMs, fmt.Sprintf("%v", d.Milliseconds()))
}
}
// QueryTags sets header on the [fauna.Client]
// Set tags to associate with the query. See [logging]
//
// [logging]: https://docs.fauna.com/fauna/current/build/logs/query_log/
func QueryTags(tags map[string]string) ClientConfigFn {
return func(c *Client) {
c.setHeader(HeaderTags, argsStringFromMap(tags))
}
}
// URL set the [fauna.Client] URL
func URL(url string) ClientConfigFn {
return func(c *Client) { c.url = url }
}
// WithLogger set the [fauna.Client] Logger
func WithLogger(logger Logger) ClientConfigFn {
return func(c *Client) { c.logger = logger }
}
// QueryOptFn function to set options on the [Client.Query]
type QueryOptFn func(req *queryRequest)
// QueryContext set the [context.Context] for a single [Client.Query]
func QueryContext(ctx context.Context) QueryOptFn {
return func(req *queryRequest) {
req.Context = ctx
}
}
// Tags set the tags header on a single [Client.Query]
func Tags(tags map[string]string) QueryOptFn {
return func(req *queryRequest) {
if val, exists := req.Headers[HeaderTags]; exists {
req.Headers[HeaderTags] = argsStringFromMap(tags, strings.Split(val, ",")...)
} else {
req.Headers[HeaderTags] = argsStringFromMap(tags)
}
}
}
// Traceparent sets the header on a single [Client.Query]
func Traceparent(id string) QueryOptFn {
return func(req *queryRequest) { req.Headers[HeaderTraceparent] = id }
}
// Timeout set the query timeout on a single [Client.Query]
func Timeout(dur time.Duration) QueryOptFn {
return func(req *queryRequest) {
req.Headers[HeaderQueryTimeoutMs] = fmt.Sprintf("%d", dur.Milliseconds())
}
}
// Typecheck sets the header on a single [Client.Query]
func Typecheck(enabled bool) QueryOptFn {
return func(req *queryRequest) { req.Headers[HeaderTypecheck] = fmt.Sprintf("%v", enabled) }
}
// StreamOptFn function to set options on the [Client.Stream]
type StreamOptFn func(req *streamRequest)
// StreamStartTime set the streams starting timestamp.
//
// Useful when resuming a stream at a given point in time.
func StreamStartTime(ts time.Time) StreamOptFn {
return func(req *streamRequest) {
req.StartTS = ts.UnixMicro()
}
}
// StreamStartTimeUnixMicros set the stream starting timestamp.
//
// Useful when resuming a stream at a given point in time.
func StreamStartTimeUnixMicros(ts int64) StreamOptFn {
return func(req *streamRequest) {
req.StartTS = ts
}
}
// EventCursor set the stream starting point based on a previously received
// event cursor.
//
// Useful when resuming a stream after a failure.
func EventCursor(cursor string) StreamOptFn {
return func(req *streamRequest) { req.Cursor = cursor }
}
func argsStringFromMap(input map[string]string, currentArgs ...string) string {
params := url.Values{}
for _, c := range currentArgs {
s := strings.Split(c, "=")
params.Set(s[0], s[1])
}
for k, v := range input {
params.Set(k, v)
}
return strings.ReplaceAll(params.Encode(), "&", ",")
}
// FeedOptFn function to set options on the [fauna.EventFeed]
type FeedOptFn func(req *feedOptions)
// EventFeedCursor set the cursor for the [fauna.EventFeed]
// cannot be used with [EventFeedStartTime] or in [fauna.Client.FeedFromQuery]
func EventFeedCursor(cursor string) FeedOptFn {
return func(req *feedOptions) { req.Cursor = &cursor }
}
// EventFeedStartTime set the start time for the [fauna.EventFeed]
// cannot be used with [EventFeedCursor]
func EventFeedStartTime(ts time.Time) FeedOptFn {
return func(req *feedOptions) {
asMicro := ts.UnixMicro()
req.StartTS = &asMicro
}
}
// EventFeedStartTimeUnixMicros set the start time for the [fauna.EventFeed]
// cannot be used with [EventFeedCursor]
func EventFeedStartTimeUnixMicros(ts int64) FeedOptFn {
return func(req *feedOptions) {
req.StartTS = &ts
}
}
// EventFeedPageSize set the page size for the [fauna.EventFeed]
// The page size is the maximum number of events returned per page.
// Must be in the range 1 to 16000 (inclusive).
// Defaults to 16.
func EventFeedPageSize(pageSize int) FeedOptFn {
return func(req *feedOptions) { req.PageSize = &pageSize }
}