-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
324 lines (263 loc) · 7.66 KB
/
Copy pathclient.go
File metadata and controls
324 lines (263 loc) · 7.66 KB
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// Package client provides the Switcher Client SDK for Go.
//
// For usage examples see README.md:
// https://github.com/switcherapi/switcher-client-go#quick-start
package client
import (
"net/http"
"sync"
"sync/atomic"
)
var globalClient atomic.Pointer[Client]
var getSwitcherAfterReadMissHook func()
var defaultClientBeforeCompareAndSwapHook func()
// Client is the primary SDK instance that holds configuration, cached snapshot, switchers
// and background workers. Use NewClient to construct or BuildContext to set the package-wide client.
type Client struct {
mu sync.RWMutex
context Context
switchers map[string]*Switcher
snapshot *Snapshot
executionLogger *executionLogger
throttleTokens chan struct{}
snapshotWatcher *snapshotWatcher
snapshotAutoUpdater *snapshotAutoUpdater
authMu sync.Mutex
authToken string
authTokenExp int64
httpClientMu sync.Mutex
httpClient_ *http.Client
notifyErrorMu sync.RWMutex
notifyErrorCallback func(error)
}
// NewClient creates a new Client with defaults applied from the provided Context.
//
// Use BuildContext to install a global package-level client.
func NewClient(ctx Context) *Client {
defaulted := ctx.withDefaults()
return &Client{
context: defaulted,
switchers: make(map[string]*Switcher),
executionLogger: newExecutionLogger(),
throttleTokens: newThrottleTokens(defaulted.Options.ThrottleMaxWorkers),
snapshotWatcher: newSnapshotWatcher(),
snapshotAutoUpdater: newSnapshotAutoUpdater(),
}
}
// BuildContext installs a package-level default client built from ctx.
func BuildContext(ctx Context) {
client := NewClient(ctx)
if current := globalClient.Load(); current != nil {
current.stopBackgroundTasks()
}
globalClient.Store(client)
client.ScheduleSnapshotAutoUpdate(0, nil)
}
// Context returns the client's effective Context (configuration).
func (c *Client) Context() Context {
c.mu.RLock()
defer c.mu.RUnlock()
return c.context
}
// GetSwitcher returns a Switcher for key.
func GetSwitcher(key string) *Switcher {
return defaultClient().GetSwitcher(key)
}
// GetSwitcher returns a Switcher instance bound to this Client.
//
// Created Switchers are cached
// on the Client so subsequent calls for the same key return the same instance.
func (c *Client) GetSwitcher(key string) *Switcher {
if key == "" {
return &Switcher{
client: c,
key: key,
}
}
c.mu.RLock()
switcher, ok := c.switchers[key]
c.mu.RUnlock()
if ok {
return switcher
}
if getSwitcherAfterReadMissHook != nil {
getSwitcherAfterReadMissHook()
}
c.mu.Lock()
defer c.mu.Unlock()
if switcher, ok = c.switchers[key]; ok {
return switcher
}
switcher = &Switcher{
client: c,
key: key,
}
c.switchers[key] = switcher
return switcher
}
// LoadSnapshot delegates to the default client to load snapshot data from disk and optionally remote.
//
// Returns the loaded snapshot version or an error.
func LoadSnapshot(options *LoadSnapshotOptions) (int, error) {
return defaultClient().LoadSnapshot(options)
}
// LoadSnapshot loads the snapshot according to options.
//
// It can read local files, check remote version
// and start watching for changes when requested.
func (c *Client) LoadSnapshot(options *LoadSnapshotOptions) (int, error) {
settings := LoadSnapshotOptions{}
if options != nil {
settings = *options
}
if _, err := c.loadSnapshotFromCurrentFile(); err != nil {
return 0, err
}
if c.shouldCheckSnapshot(settings.FetchRemote) {
if _, err := c.CheckSnapshot(); err != nil {
return 0, err
}
}
if settings.WatchSnapshot {
if err := c.WatchSnapshot(WatchSnapshotCallback{}); err != nil {
return 0, err
}
}
return c.SnapshotVersion(), nil
}
// SnapshotVersion returns current snapshot version from the package default client.
func SnapshotVersion() int {
return defaultClient().SnapshotVersion()
}
// SnapshotVersion returns the client's current snapshot version (0 when not loaded).
func (c *Client) SnapshotVersion() int {
c.mu.RLock()
defer c.mu.RUnlock()
if c.snapshot == nil {
return 0
}
return c.snapshot.Domain.Version
}
// CheckSnapshot checks the remote API for an updated snapshot and applies it when newer.
//
// Returns true when a new snapshot was applied.
func CheckSnapshot() (bool, error) {
return defaultClient().CheckSnapshot()
}
// CheckSnapshot checks for and applies a newer snapshot from the remote API.
//
// Returns true when updated.
func (c *Client) CheckSnapshot() (bool, error) {
token, err := c.ensureToken()
if err != nil {
return false, err
}
if err := missingTokenError(token); err != nil {
return false, err
}
upToDate, err := c.checkSnapshotVersion(token, c.SnapshotVersion())
if err != nil {
return false, err
}
if upToDate {
return false, nil
}
snapshot, err := c.resolveSnapshot(token)
if err != nil {
return false, err
}
if err := saveSnapshotToFile(c.Context(), snapshot); err != nil {
return false, err
}
c.setSnapshot(snapshot)
return true, nil
}
// CheckSwitchers validates that the provided switcher keys exist in the current snapshot
// or on the remote API, depending on the client's mode.
func CheckSwitchers(switcherKeys []string) error {
return defaultClient().CheckSwitchers(switcherKeys)
}
// CheckSwitchers validates switcher keys against local snapshot data or the remote API.
func (c *Client) CheckSwitchers(switcherKeys []string) error {
if c.Context().Options.Local {
return checkLocalSwitchers(c.snapshotState(), switcherKeys)
}
token, err := c.ensureToken()
if err != nil {
return err
}
if err := missingTokenError(token); err != nil {
return err
}
return c.checkSwitchers(token, switcherKeys)
}
// GetExecution retrieves the last execution log entry for the provided Switcher using the default client.
func GetExecution(switcher *Switcher) ExecutionEntry {
return defaultClient().GetExecution(switcher)
}
// GetExecution returns the recorded execution entry for a Switcher, or an empty entry when nil.
func (c *Client) GetExecution(switcher *Switcher) ExecutionEntry {
if switcher == nil {
return ExecutionEntry{}
}
execution := switcher.snapshotForExecution()
return c.executionLogger.get(execution.key, execution.entries)
}
// ClearLogger clears the execution log cache on the default client.
func ClearLogger() {
defaultClient().ClearLogger()
}
// ClearLogger clears the client's execution logger cache.
func (c *Client) ClearLogger() {
c.executionLogger.clear()
}
// SubscribeNotifyError registers a callback to receive asynchronous SDK errors on the default client.
func SubscribeNotifyError(callback func(error)) {
defaultClient().SubscribeNotifyError(callback)
}
// SubscribeNotifyError registers a per-client callback invoked when the SDK encounters errors.
func (c *Client) SubscribeNotifyError(callback func(error)) {
c.notifyErrorMu.Lock()
defer c.notifyErrorMu.Unlock()
c.notifyErrorCallback = callback
}
func (c *Client) notifyError(err error) {
c.notifyErrorMu.RLock()
callback := c.notifyErrorCallback
c.notifyErrorMu.RUnlock()
if callback != nil {
callback(err)
}
}
func (c *Client) runBackgroundTask(task func()) {
if c.throttleTokens == nil {
go task()
return
}
go func() {
c.throttleTokens <- struct{}{}
defer func() {
<-c.throttleTokens
}()
task()
}()
}
func defaultClient() *Client {
if client := globalClient.Load(); client != nil {
return client
}
client := NewClient(Context{})
if defaultClientBeforeCompareAndSwapHook != nil {
defaultClientBeforeCompareAndSwapHook()
}
if globalClient.CompareAndSwap(nil, client) {
return client
}
return globalClient.Load()
}
func newThrottleTokens(maxWorkers int) chan struct{} {
if maxWorkers <= 0 {
return nil
}
return make(chan struct{}, maxWorkers)
}