-
Notifications
You must be signed in to change notification settings - Fork 54
/
config.go
269 lines (234 loc) · 8.21 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
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
package unleash
import (
"net/http"
"net/url"
"time"
"github.com/Unleash/unleash-client-go/v4/api"
"github.com/Unleash/unleash-client-go/v4/context"
"github.com/Unleash/unleash-client-go/v4/strategy"
)
type configOption struct {
appName string
environment string
instanceId string
url string
projectName string
refreshInterval time.Duration
metricsInterval time.Duration
disableMetrics bool
backupPath string
strategies []strategy.Strategy
listener interface{}
storage Storage
httpClient *http.Client
customHeaders http.Header
}
// ConfigOption represents a option for configuring the client.
type ConfigOption func(*configOption)
// WithListener allows users to register a type that implements one or more of
// the listener interfaces. If no listener is registered then the user is responsible
// for draining the various channels on the client. Failure to do so will stop the client
// from working as the worker routines will be blocked.
func WithListener(listener interface{}) ConfigOption {
return func(o *configOption) {
o.listener = listener
}
}
// WithAppName specifies the name of the application.
func WithAppName(appName string) ConfigOption {
return func(o *configOption) {
o.appName = appName
}
}
// WithEnvironment specifies the environment
func WithEnvironment(env string) ConfigOption {
return func(o *configOption) {
o.environment = env
}
}
// WithInstanceId specifies the instance identifier of the current instance. If not provided,
// one will be generated based on various parameters such as current user and hostname.
func WithInstanceId(instanceId string) ConfigOption {
return func(o *configOption) {
o.instanceId = instanceId
}
}
// WithUrl specifies the url of the unleash server the user is connecting to.
func WithUrl(url string) ConfigOption {
return func(o *configOption) {
o.url = url
}
}
// WithRefreshInterval specifies the time interval with which the client should sync the
// feature toggles from the unleash server (default 15s).
func WithRefreshInterval(refreshInterval time.Duration) ConfigOption {
return func(o *configOption) {
o.refreshInterval = refreshInterval
}
}
// WithMetricsInterval specifies the time interval with which the client should upload
// the metrics data to the unleash server (default 60s).
func WithMetricsInterval(metricsInterval time.Duration) ConfigOption {
return func(o *configOption) {
o.metricsInterval = metricsInterval
}
}
// WithDisableMetrics specifies that the client should not log metrics to the unleash server.
func WithDisableMetrics(disableMetrics bool) ConfigOption {
return func(o *configOption) {
o.disableMetrics = disableMetrics
}
}
// WithBackupPath specifies the path that is passed to the storage implementation for storing
// the feature toggles locally.
func WithBackupPath(backupPath string) ConfigOption {
return func(o *configOption) {
o.backupPath = backupPath
}
}
// WithStrategies specifies which strategies (in addition to the defaults) should be used by the
// client.
func WithStrategies(strategies ...strategy.Strategy) ConfigOption {
return func(o *configOption) {
o.strategies = strategies
}
}
// WithStorage specifies which storage implementation the repository should use for storing feature
// toggles.
func WithStorage(storage Storage) ConfigOption {
return func(o *configOption) {
o.storage = storage
}
}
// WithHttpClient specifies which HttpClient the client should use for making requests to the server.
func WithHttpClient(client *http.Client) ConfigOption {
return func(o *configOption) {
o.httpClient = client
}
}
// WithCustomHeaders specifies any custom headers that should be sent along with requests to the
// server.
func WithCustomHeaders(headers http.Header) ConfigOption {
return func(o *configOption) {
o.customHeaders = headers
}
}
// WithProjectName defines a projectName on the config object and is used to
// filter toggles by project name.
func WithProjectName(projectName string) ConfigOption {
return func(o *configOption) {
o.projectName = projectName
}
}
// FeatureResolver represents a function to be called to resolve the feature instead of using the repository
type FeatureResolver func(feature string) *api.Feature
// WithResolver allows you to bypass the repository when resolving a feature name to its actual instance.
func WithResolver(resolver FeatureResolver) FeatureOption {
return func(opts *featureOption) {
opts.resolver = resolver
}
}
// FallbackFunc represents a function to be called if the feature is not found.
type FallbackFunc func(feature string, ctx *context.Context) bool
type featureOption struct {
fallback *bool
fallbackFunc FallbackFunc
ctx *context.Context
resolver FeatureResolver
}
// FeatureOption provides options for querying if a feature is enabled or not.
type FeatureOption func(*featureOption)
// WithFallback specifies what the value should be if the feature toggle is not found on the
// unleash service.
func WithFallback(fallback bool) FeatureOption {
return func(opts *featureOption) {
opts.fallback = &fallback
}
}
// WithFallbackFunc specifies a fallback function to evaluate a feature
// toggle in the event that it is not found on the service.
func WithFallbackFunc(fallback FallbackFunc) FeatureOption {
return func(opts *featureOption) {
opts.fallbackFunc = fallback
}
}
// WithContext allows the user to provide a context that will be passed into the active strategy
// for determining if a specified feature should be enabled or not.
func WithContext(ctx context.Context) FeatureOption {
return func(opts *featureOption) {
opts.ctx = &ctx
}
}
// WithVariantContext specifies a context for the GetVariant
// call
func WithVariantContext(ctx context.Context) VariantOption {
return func(opts *variantOption) {
opts.ctx = &ctx
}
}
// WithVariantResolver allows you to bypass the repository when resolving a feature name to its actual instance.
func WithVariantResolver(resolver FeatureResolver) VariantOption {
return func(opts *variantOption) {
opts.resolver = resolver
}
}
// VariantFallbackFunc represents a function to be called if the variant is not found.
type VariantFallbackFunc func(feature string, ctx *context.Context) *api.Variant
type variantOption struct {
variantFallback *api.Variant
variantFallbackFunc VariantFallbackFunc
ctx *context.Context
resolver FeatureResolver
}
// VariantOption provides options for querying if a variant is found or not.
type VariantOption func(*variantOption)
// WithVariantFallback specifies what the value should be if the
// variant is not found on the unleash service. This could be because
// the feature doesn't exist, because it is disabled, or because it
// has no variants.
//
// If you specify a fallback variant, note that its `FeatureEnabled`
// field will be set to whatever you pass in or `false` by default. In
// other words, it will not reflect the feature's actual enabled
// state.
func WithVariantFallback(variantFallback *api.Variant) VariantOption {
return func(opts *variantOption) {
opts.variantFallback = variantFallback
}
}
// WithVariantFallbackFunc specifies a fallback function to evaluate
// to a variant when a variant is not found for a feature. This could
// be because the feature doesn't exist, because it is disabled, or
// because it has no variants.
//
// If you specify a fallback variant, note that its `FeatureEnabled`
// field will be set to whatever you pass in or `false` by default. In
// other words, it will not reflect the feature's actual enabled
// state.
func WithVariantFallbackFunc(variantFallbackFunc VariantFallbackFunc) VariantOption {
return func(opts *variantOption) {
opts.variantFallbackFunc = variantFallbackFunc
}
}
type repositoryOptions struct {
appName string
instanceId string
projectName string
url url.URL
backupPath string
refreshInterval time.Duration
storage Storage
httpClient *http.Client
customHeaders http.Header
}
type metricsOptions struct {
appName string
instanceId string
url url.URL
strategies []string
metricsInterval time.Duration
disableMetrics bool
httpClient *http.Client
customHeaders http.Header
started *time.Time
}