-
Notifications
You must be signed in to change notification settings - Fork 55
/
unleash.go
74 lines (61 loc) · 2.17 KB
/
unleash.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
package unleash
import "github.com/Unleash/unleash-client-go/v4/api"
var defaultClient *Client
// ErrorListener defines an interface that be implemented in order to receive
// errors and warnings from the client.
type ErrorListener interface {
// OnError is called whenever the client experiences an error.
OnError(error)
// OnWarning is called whenever the client experiences a warning.
OnWarning(error)
}
// MetricListener defines an interface that can be implemented in order to receive
// events that are relevant to sending metrics.
type MetricListener interface {
// OnCount is called whenever the specified feature is queried.
OnCount(string, bool)
// OnSent is called whenever the server has successfully sent metrics to the server.
OnSent(MetricsData)
// OnRegistered is called whenever the client has successfully registered with the metrics server.
OnRegistered(ClientData)
}
// RepositoryListener defines an interface that can be implemented in order to receive events that are relevant to
// the feature toggle repository.
type RepositoryListener interface {
// OnReady is called when the client has loaded the feature toggles from
// the Unleash server.
OnReady()
}
// IsEnabled queries the default client whether or not the specified feature is enabled or not.
func IsEnabled(feature string, options ...FeatureOption) bool {
if defaultClient == nil {
var opts featureOption
for _, o := range options {
o(&opts)
}
return handleFallback(opts, feature, opts.ctx).Enabled
}
return defaultClient.IsEnabled(feature, options...)
}
// Initialize will specify the options to be used by the default client.
func Initialize(options ...ConfigOption) (err error) {
defaultClient, err = NewClient(options...)
return
}
func GetVariant(feature string, options ...VariantOption) *api.Variant {
if defaultClient == nil {
return api.GetDefaultVariant()
}
return defaultClient.GetVariant(feature, options...)
}
// Close will close the default client.
func Close() error {
if defaultClient == nil {
return nil
}
return defaultClient.Close()
}
// WaitForReady will block until the default client is ready or return immediately.
func WaitForReady() {
defaultClient.WaitForReady()
}