-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathconfiguration.go
More file actions
138 lines (120 loc) · 5.04 KB
/
Copy pathconfiguration.go
File metadata and controls
138 lines (120 loc) · 5.04 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
package devcycle
import (
"net/http"
"time"
"github.com/devcyclehq/go-server-sdk/v2/api"
"github.com/devcyclehq/go-server-sdk/v2/util"
)
// contextKeys are used to identify the type of value in the context.
// Since these are string, it is possible to get a short description of the
// context key for logging and debugging using key.String().
type EventQueueOptions = api.EventQueueOptions
type AdvancedOptions struct {
OverridePlatformData *api.PlatformData
OverrideConfigWithV1 bool
OverrideMaxSSEPolling time.Duration
}
type ConfigMetadata struct {
Project api.ProjectMetadata `json:"project,omitempty"`
Environment api.EnvironmentMetadata `json:"environment,omitempty"`
}
type Options struct {
EnableEdgeDB bool `json:"enableEdgeDb,omitempty"`
EnableCloudBucketing bool `json:"enableCloudBucketing,omitempty"`
EventFlushIntervalMS time.Duration `json:"eventFlushIntervalMS,omitempty"`
ConfigPollingIntervalMS time.Duration `json:"configPollingIntervalMS,omitempty"`
RequestTimeout time.Duration `json:"requestTimeout,omitempty"`
DisableAutomaticEventLogging bool `json:"disableAutomaticEventLogging,omitempty"`
DisableCustomEventLogging bool `json:"disableCustomEventLogging,omitempty"`
DisableETagMatching bool `json:"disableETagMatching,omitempty"`
DisableRealtimeUpdates bool `json:"disableRealtimeUpdates,omitempty"`
// Deprecated: EnableBetaRealtimeUpdates is no longer supported. SSE connections are enabled by default.
EnableBetaRealtimeUpdates bool `json:"enableRealtimeUpdates,omitempty"`
MaxEventQueueSize int `json:"maxEventsPerFlush,omitempty"`
FlushEventQueueSize int `json:"minEventsPerFlush,omitempty"`
ConfigCDNURI string
EventsAPIURI string
ClientEventHandler chan api.ClientEvent
BucketingAPIURI string
Logger util.Logger
EvalHooks []*EvalHook
AdvancedOptions
configMetadata ConfigMetadata
}
func (o *Options) eventQueueOptions() *EventQueueOptions {
return &EventQueueOptions{
FlushEventsInterval: o.EventFlushIntervalMS,
DisableAutomaticEventLogging: o.DisableAutomaticEventLogging,
DisableCustomEventLogging: o.DisableCustomEventLogging,
MaxEventQueueSize: o.MaxEventQueueSize,
FlushEventQueueSize: o.FlushEventQueueSize,
EventRequestChunkSize: 100, // TODO: make this configurable
EventsAPIBasePath: o.EventsAPIURI,
}
}
func (o *Options) CheckDefaults() {
if o.ConfigCDNURI == "" {
o.ConfigCDNURI = "https://config-cdn.devcycle.com"
}
if o.EventsAPIURI == "" {
o.EventsAPIURI = "https://events.devcycle.com"
}
if o.BucketingAPIURI == "" {
o.BucketingAPIURI = "https://bucketing-api.devcycle.com"
}
if o.EventFlushIntervalMS == 0 {
o.EventFlushIntervalMS = time.Second * 30
} else if o.EventFlushIntervalMS < time.Millisecond*500 || o.EventFlushIntervalMS > time.Minute {
util.Warnf("EventFlushIntervalMS cannot be less than 500ms or longer than 1 minute. Defaulting to 30 seconds.")
o.EventFlushIntervalMS = time.Second * 30
}
if o.ConfigPollingIntervalMS == 0 {
o.ConfigPollingIntervalMS = time.Second * 10
} else if o.ConfigPollingIntervalMS < time.Second {
util.Warnf("ConfigPollingIntervalMS cannot be less than 1 second. Defaulting to 10 seconds.")
o.ConfigPollingIntervalMS = time.Second * 10
}
if o.AdvancedOptions.OverrideMaxSSEPolling != 0 && o.AdvancedOptions.OverrideMaxSSEPolling < time.Second {
o.AdvancedOptions.OverrideMaxSSEPolling = time.Second
}
if o.RequestTimeout <= time.Second*5 {
o.RequestTimeout = time.Second * 5
}
if o.MaxEventQueueSize <= 0 {
o.MaxEventQueueSize = 10000
} else if o.MaxEventQueueSize > 50000 {
o.MaxEventQueueSize = 50000
}
if o.FlushEventQueueSize <= 0 {
o.FlushEventQueueSize = 1000
} else if o.FlushEventQueueSize > 50000 {
o.FlushEventQueueSize = 50000
}
}
type HTTPConfiguration struct {
BasePath string `json:"basePath,omitempty"`
ConfigCDNBasePath string `json:"configCDNBasePath,omitempty"`
EventsAPIBasePath string `json:"eventsAPIBasePath,omitempty"`
Host string `json:"host,omitempty"`
Scheme string `json:"scheme,omitempty"`
DefaultHeader map[string]string `json:"defaultHeader,omitempty"`
UserAgent string `json:"userAgent,omitempty"`
HTTPClient *http.Client
}
func NewConfiguration(options *Options) *HTTPConfiguration {
cfg := &HTTPConfiguration{
BasePath: options.BucketingAPIURI,
ConfigCDNBasePath: options.ConfigCDNURI,
EventsAPIBasePath: options.EventsAPIURI,
DefaultHeader: make(map[string]string),
UserAgent: "DevCycle-Server-SDK/" + VERSION + "/go",
HTTPClient: &http.Client{
// Set an explicit timeout so that we don't wait forever on a request
Timeout: options.RequestTimeout,
},
}
return cfg
}
func (c *HTTPConfiguration) AddDefaultHeader(key string, value string) {
c.DefaultHeader[key] = value
}