-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsettings_client.go
More file actions
231 lines (195 loc) · 6.56 KB
/
settings_client.go
File metadata and controls
231 lines (195 loc) · 6.56 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
package azureappconfiguration
import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
"github.com/Azure/AppConfiguration-GoProvider/azureappconfiguration/internal/tracing"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/data/azappconfig/v2"
)
type settingsResponse struct {
settings []azappconfig.Setting
watchedETags map[WatchedSetting]*azcore.ETag
pageETags map[comparableSelector][]*azcore.ETag
}
type selectorSettingsClient struct {
selectors []Selector
client *azappconfig.Client
tracingOptions tracing.Options
}
type watchedSettingClient struct {
watchedSettings []WatchedSetting
eTags map[WatchedSetting]*azcore.ETag
client *azappconfig.Client
tracingOptions tracing.Options
}
type pageETagsClient struct {
pageETags map[comparableSelector][]*azcore.ETag
client *azappconfig.Client
tracingOptions tracing.Options
}
type settingsClient interface {
getSettings(ctx context.Context) (*settingsResponse, error)
}
type eTagsClient interface {
checkIfETagChanged(ctx context.Context) (bool, error)
}
// snapshotSettingsLoader is a function type that loads settings from a snapshot by name.
type snapshotSettingsLoader func(ctx context.Context, snapshotName string) ([]azappconfig.Setting, error)
type refreshClient struct {
loader settingsClient
monitor eTagsClient
sentinels settingsClient
}
func (s *selectorSettingsClient) getSettings(ctx context.Context) (*settingsResponse, error) {
if s.tracingOptions.Enabled {
ctx = policy.WithHTTPHeader(ctx, tracing.CreateCorrelationContextHeader(ctx, s.tracingOptions))
}
settings := make([]azappconfig.Setting, 0)
pageETags := make(map[comparableSelector][]*azcore.ETag)
for _, filter := range s.selectors {
if filter.SnapshotName == "" {
selector := azappconfig.SettingSelector{
KeyFilter: to.Ptr(filter.KeyFilter),
LabelFilter: to.Ptr(filter.LabelFilter),
TagsFilter: filter.TagFilters,
Fields: azappconfig.AllSettingFields(),
}
pager := s.client.NewListSettingsPager(selector, nil)
eTags := make([]*azcore.ETag, 0)
for pager.More() {
page, err := pager.NextPage(ctx)
if err != nil {
return nil, err
} else if page.Settings != nil {
settings = append(settings, page.Settings...)
eTags = append(eTags, page.ETag)
}
}
pageETags[filter.comparableKey()] = eTags
} else {
snapshotSettings, err := loadSnapshotSettings(ctx, s.client, filter.SnapshotName)
if err != nil {
return nil, err
}
settings = append(settings, snapshotSettings...)
}
}
return &settingsResponse{
settings: settings,
pageETags: pageETags,
}, nil
}
func (c *watchedSettingClient) getSettings(ctx context.Context) (*settingsResponse, error) {
if c.tracingOptions.Enabled {
ctx = policy.WithHTTPHeader(ctx, tracing.CreateCorrelationContextHeader(ctx, c.tracingOptions))
}
settings := make([]azappconfig.Setting, 0, len(c.watchedSettings))
watchedETags := make(map[WatchedSetting]*azcore.ETag)
for _, watchedSetting := range c.watchedSettings {
response, err := c.client.GetSetting(ctx, watchedSetting.Key, &azappconfig.GetSettingOptions{Label: to.Ptr(watchedSetting.Label)})
if err != nil {
var respErr *azcore.ResponseError
if errors.As(err, &respErr) && respErr.StatusCode == 404 {
label := watchedSetting.Label
if label == "" || label == "\x00" { // NUL is escaped to \x00 in golang
label = "no"
}
// If the watched setting is not found, log and continue
log.Printf("Watched key '%s' with %s label does not exist", watchedSetting.Key, label)
continue
}
return nil, err
}
settings = append(settings, response.Setting)
watchedETags[watchedSetting] = response.Setting.ETag
}
return &settingsResponse{
settings: settings,
watchedETags: watchedETags,
}, nil
}
func (c *watchedSettingClient) checkIfETagChanged(ctx context.Context) (bool, error) {
if c.tracingOptions.Enabled {
ctx = policy.WithHTTPHeader(ctx, tracing.CreateCorrelationContextHeader(ctx, c.tracingOptions))
}
for watchedSetting, ETag := range c.eTags {
_, err := c.client.GetSetting(ctx, watchedSetting.Key, &azappconfig.GetSettingOptions{Label: to.Ptr(watchedSetting.Label), OnlyIfChanged: ETag})
if err != nil {
var respErr *azcore.ResponseError
if errors.As(err, &respErr) && (respErr.StatusCode == 404 || respErr.StatusCode == 304) {
continue
}
return false, err
}
return true, nil
}
return false, nil
}
func (c *pageETagsClient) checkIfETagChanged(ctx context.Context) (bool, error) {
for selector, pageETags := range c.pageETags {
s := azappconfig.SettingSelector{
KeyFilter: to.Ptr(selector.KeyFilter),
LabelFilter: to.Ptr(selector.LabelFilter),
Fields: azappconfig.AllSettingFields(),
}
tagFilters := make([]string, 0)
if selector.TagFilters != "" {
json.Unmarshal([]byte(selector.TagFilters), &tagFilters)
s.TagsFilter = tagFilters
}
conditions := make([]azcore.MatchConditions, 0)
for _, eTag := range pageETags {
conditions = append(conditions, azcore.MatchConditions{IfNoneMatch: eTag})
}
pager := c.client.NewListSettingsPager(s, &azappconfig.ListSettingsOptions{
MatchConditions: conditions,
})
pageCount := 0
for pager.More() {
pageCount++
page, err := pager.NextPage(context.Background())
if err != nil {
return false, err
}
// ETag changed
if page.ETag != nil {
return true, nil
}
}
if pageCount != len(pageETags) {
return true, nil
}
}
return false, nil
}
func loadSnapshotSettings(ctx context.Context, client *azappconfig.Client, snapshotName string) ([]azappconfig.Setting, error) {
settings := make([]azappconfig.Setting, 0)
snapshot, err := client.GetSnapshot(ctx, snapshotName, nil)
if err != nil {
var respErr *azcore.ResponseError
if errors.As(err, &respErr) && respErr.StatusCode == 404 {
return settings, nil // treat non-existing snapshot as empty
}
return nil, err
}
if snapshot.CompositionType == nil || *snapshot.CompositionType != azappconfig.CompositionTypeKey {
return nil, fmt.Errorf("composition type for the selected snapshot '%s' must be 'key'", snapshotName)
}
pager := client.NewListSettingsForSnapshotPager(snapshotName, nil)
for pager.More() {
page, err := pager.NextPage(ctx)
if err != nil {
return nil, err
} else if page.Settings != nil {
settings = append(settings, page.Settings...)
}
}
return settings, nil
}