/
config.go
239 lines (195 loc) · 5.23 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
/*
* Licensed to Apache Software Foundation (ASF) under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Apache Software Foundation (ASF) licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
package config
import (
"context"
"errors"
"sync/atomic"
)
import (
"gopkg.in/yaml.v3"
)
var ErrorNotImplement = errors.New("not implement")
type (
CenterTest = center
)
var _ Center = (*center)(nil)
type center struct {
tenant string
initialize int32
storeOperate StoreOperator
Reader ConfigReader
Writer ConfigWriter
Watcher ConfigWatcher
}
func NewCenter(tenant string, storeOperate StoreOperator, opts ...option) (Center, error) {
c := ¢er{
storeOperate: storeOperate,
tenant: tenant,
}
opt := &configOption{
tenant: tenant,
}
for i := range opts {
opts[i](opt)
}
if err := c.newConfigWriter(*opt); err != nil {
return nil, err
}
if err := c.newConfigWatcher(*opt); err != nil {
return nil, err
}
if err := c.newConfigReader(*opt); err != nil {
return nil, err
}
return c, nil
}
func (c *center) newConfigReader(opt configOption) error {
if !opt.openReader {
return nil
}
p := NewPathInfo(opt.tenant)
holders := map[PathKey]*atomic.Value{}
for k := range p.ConfigKeyMapping {
holders[k] = &atomic.Value{}
holders[k].Store(NewEmptyTenant())
}
var ret ConfigReader
reader := &configReader{
tenant: opt.tenant,
storeOperate: c.storeOperate,
pathInfo: p,
holders: holders,
}
ret = reader
if opt.cacheable {
watcher, err := NewConfigWatcher(c.tenant, c.storeOperate, NewPathInfo(opt.tenant))
if err != nil {
return err
}
ret = &cacheConfigReader{
reader: reader,
watcher: watcher,
}
}
c.Reader = ret
return nil
}
func (c *center) newConfigWriter(opt configOption) error {
if !opt.openWriter {
return nil
}
writer := &configWriter{
tenant: opt.tenant,
storeOperate: c.storeOperate,
pathInfo: NewPathInfo(opt.tenant),
}
c.Writer = writer
return nil
}
func (c *center) newConfigWatcher(opt configOption) error {
if !opt.openWatcher {
return nil
}
watcher, err := NewConfigWatcher(c.tenant, c.storeOperate, NewPathInfo(opt.tenant))
if err != nil {
return err
}
c.Watcher = watcher
return nil
}
// LoadAll loads the full Tenant configuration, the first time it will be loaded remotely,
// and then it will be directly assembled from the cache layer
func (c *center) LoadAll(ctx context.Context) (*Tenant, error) {
if c.Reader == nil {
return nil, ErrorNotImplement
}
return c.Reader.LoadAll(ctx)
}
// Load loads the full Tenant configuration, the first time it will be loaded remotely,
// and then it will be directly assembled from the cache layer
func (c *center) Load(ctx context.Context, item ConfigItem) (*Tenant, error) {
if c.Reader == nil {
return nil, ErrorNotImplement
}
return c.Reader.Load(ctx, item)
}
// Import imports the configuration information of a tenant
func (c *center) Import(ctx context.Context, cfg *Tenant) error {
if c.Writer == nil {
return ErrorNotImplement
}
return c.Writer.Import(ctx, cfg)
}
// Write imports the configuration information of a tenant
func (c *center) Write(ctx context.Context, item ConfigItem, cfg *Tenant) error {
if c.Writer == nil {
return ErrorNotImplement
}
return c.Writer.Write(ctx, item, cfg)
}
// Subscribe subscribes to all changes of an event by EventType
func (c *center) Subscribe(ctx context.Context, et EventType, cb EventCallback) (context.CancelFunc, error) {
if c.Watcher == nil {
return nil, ErrorNotImplement
}
return c.Watcher.Subscribe(ctx, et, cb)
}
func (c *center) Close() error {
if c.Reader != nil {
if err := c.Reader.Close(); err != nil {
return err
}
}
if c.Writer != nil {
if err := c.Writer.Close(); err != nil {
return err
}
}
if c.Watcher != nil {
if err := c.Watcher.Close(); err != nil {
return err
}
}
if c.storeOperate != nil {
if err := c.storeOperate.Close(); err != nil {
return err
}
}
return nil
}
func (c *center) Tenant() string {
return c.tenant
}
func JSONToYAML(j string) ([]byte, error) {
// Convert the JSON to an object.
var jsonObj interface{}
// We are using yaml.Unmarshal here (instead of json.Unmarshal) because the
// Go JSON library doesn't try to pick the right number type (int, float,
// etc.) when unmarshalling to interface{}, it just picks float64
// universally. go-yaml does go through the effort of picking the right
// number type, so we can preserve number type throughout this process.
err := yaml.Unmarshal([]byte(j), &jsonObj)
if err != nil {
return nil, err
}
// Marshal this object into YAML.
return yaml.Marshal(jsonObj)
}