-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanager.go
More file actions
358 lines (314 loc) · 9.4 KB
/
Copy pathmanager.go
File metadata and controls
358 lines (314 loc) · 9.4 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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package fastconf
import (
"context"
"fmt"
"github.com/fastabc/fastconf/internal/fcerr"
imanager "github.com/fastabc/fastconf/internal/manager"
istate "github.com/fastabc/fastconf/internal/state"
itenant "github.com/fastabc/fastconf/internal/tenant"
"github.com/fastabc/fastconf/policy"
)
// closedErrCh is returned by Errors() when the manager is nil/uninitialized,
// avoiding a per-call allocation.
var closedErrCh = func() chan ReloadError {
ch := make(chan ReloadError)
close(ch)
return ch
}()
// Manager is the strongly-typed, lock-free configuration manager.
type Manager[T any] struct {
inner *imanager.M[T]
}
// New constructs a Manager and runs the first reload synchronously.
//
// For one-line initialisation in main / init see [MustNew].
func New[T any](ctx context.Context, opts ...Option) (*Manager[T], error) {
m, err := imanager.New[T](ctx, opts...)
if err != nil {
return nil, err
}
return &Manager[T]{inner: m}, nil
}
// MustNew is the panic variant of [New]. It is intended for top-level
// program initialisation (main / init), where the only sensible
// response to a configuration-load failure is to abort startup with a
// loud, deterministic panic:
//
// var Config = fastconf.MustNew[AppConfig](context.Background(),
// fastconf.WithDir("conf.d"),
// fastconf.WithProvider(provider.NewEnv("APP_")),
// )
//
// Long-running servers / daemons should continue to use [New] so they
// can decide whether to fall back to built-in defaults or keep serving
// the previous snapshot. MustNew deliberately omits MustGet /
// MustReload variants:
//
// - [Manager.Get] on a successfully constructed manager never
// returns nil — New runs the initial reload before returning, so
// the snapshot is always populated.
// - [Manager.Reload] failures are runtime events; panicking on a
// network blip would violate the framework's failure-safe contract.
// - [Extract] is nil-safe by design and cannot fail.
//
// The panic message wraps the underlying error so `recover` / panic
// reporters surface the original cause.
func MustNew[T any](ctx context.Context, opts ...Option) *Manager[T] {
m, err := New[T](ctx, opts...)
if err != nil {
panic(fmt.Errorf("fastconf.MustNew: %w", err))
}
return m
}
func (m *Manager[T]) Get() *T {
if m == nil {
return nil
}
return m.inner.Get()
}
func (m *Manager[T]) Snapshot() *State[T] {
if m == nil {
return nil
}
return wrapState(m.inner.Snapshot())
}
func (m *Manager[T]) Close() error {
if m == nil {
return nil
}
return m.inner.Close()
}
func (m *Manager[T]) Errors() <-chan ReloadError {
if m == nil {
return closedErrCh
}
return m.inner.Errors()
}
func (m *Manager[T]) Reload(ctx context.Context, opts ...ReloadOption) error {
if m == nil {
return fcerr.ErrClosed
}
return m.inner.Reload(ctx, opts...)
}
func (m *Manager[T]) Plan() *PlanBuilder[T] {
if m == nil {
return &PlanBuilder[T]{}
}
return &PlanBuilder[T]{inner: m.inner.Plan()}
}
func (m *Manager[T]) Replay() *Replay[T] {
if m == nil {
return &Replay[T]{}
}
return &Replay[T]{inner: m.inner.Replay()}
}
func (m *Manager[T]) Watcher() *Watcher[T] {
if m == nil {
return &Watcher[T]{}
}
return &Watcher[T]{inner: m.inner.Watcher()}
}
type ReloadOption = imanager.ReloadOption
// WithSourceOverride injects a one-shot in-memory layer for a single
// Reload. Override values must be JSON-serializable; Reload copies the map
// into an independent JSON-shaped tree when applying the option, then the
// next plain Reload reverts to the natural source state.
func WithSourceOverride(override map[string]any) ReloadOption {
return imanager.WithSourceOverride(override)
}
func WithReloadReason(reason string) ReloadOption {
return imanager.WithReloadReason(reason)
}
type ValidatorReport = imanager.ValidatorReport
type PlanResult[T any] struct {
Proposed *State[T]
Diff []DiffEntry
Validators []ValidatorReport
Policies []policy.Violation
}
type PlanBuilder[T any] struct {
inner *imanager.PlanBuilder[T]
}
func (b *PlanBuilder[T]) WithHostname(host string) *PlanBuilder[T] {
if b != nil && b.inner != nil {
b.inner = b.inner.WithHostname(host)
}
return b
}
func (b *PlanBuilder[T]) Run(ctx context.Context) (*PlanResult[T], error) {
if b == nil || b.inner == nil {
return nil, fmt.Errorf("fastconf: nil manager")
}
res, err := b.inner.Run(ctx)
if err != nil {
return nil, err
}
return &PlanResult[T]{
Proposed: wrapState(res.Proposed),
Diff: res.Diff,
Validators: res.Validators,
Policies: res.Policies,
}, nil
}
type Replay[T any] struct {
inner *imanager.Replay[T]
}
func (r *Replay[T]) List() []*State[T] {
if r == nil {
return nil
}
return wrapStates(r.inner.List())
}
func (r *Replay[T]) Rollback(target *State[T]) error {
if r == nil {
return imanager.ErrHistoryDisabled
}
return r.inner.Rollback(unwrapState(target))
}
type Watcher[T any] struct {
inner *imanager.Watcher[T]
}
func (w *Watcher[T]) Pause() {
if w != nil && w.inner != nil {
w.inner.Pause()
}
}
func (w *Watcher[T]) Resume() {
if w != nil && w.inner != nil {
w.inner.Resume()
}
}
func (w *Watcher[T]) Paused() bool {
return w != nil && w.inner != nil && w.inner.Paused()
}
var ErrUnknownGeneration = imanager.ErrUnknownGeneration
var ErrHistoryDisabled = imanager.ErrHistoryDisabled
// SubscribeOption customises a [Subscribe] registration. The only
// constructor today is [WithEqual]; the type is exported so callers can
// write helper functions that return options.
type SubscribeOption[M any] func(*subscribeOpts[M])
// subscribeOpts carries the resolved per-subscriber knobs.
type subscribeOpts[M any] struct {
equal func(old, new *M) bool
}
// WithEqual replaces the default [reflect.DeepEqual] comparator used by
// [Subscribe] to decide whether the extracted value actually changed.
//
// The framework invokes equal only with two non-nil pointers; nil ↔
// non-nil transitions are unambiguous changes and never consult equal.
// Return true to mark old and new as unchanged (the callback is skipped).
//
// Common uses:
//
// - Ignore a noisy field: return a.URL == b.URL && a.Pool == b.Pool
// - Hash-compare large structs: return a.Hash == b.Hash
// - Force fire-on-every-reload (e.g. audit, mirror, heartbeat):
// WithEqual(func(_, _ *T) bool { return false })
func WithEqual[M any](equal func(old, new *M) bool) SubscribeOption[M] {
return func(o *subscribeOpts[M]) { o.equal = equal }
}
// Subscribe registers a callback that fires after a successful reload
// when the value extracted by extract has actually changed.
//
// Change detection uses [reflect.DeepEqual] on the dereferenced values by
// default. Pass [WithEqual] to substitute a custom comparator (skip
// noisy fields, hash-compare large structs, force fire-on-every-reload).
//
// cancel := fastconf.Subscribe(mgr,
// func(c *AppConfig) *DBConfig { return &c.Database },
// func(old, new *DBConfig) {
// reconnect(new) // guaranteed: database config actually changed
// },
// )
// defer cancel()
//
// nil ↔ non-nil transitions always fire (unambiguous changes; equality
// function is not consulted). Two nil values do not fire.
//
// Callbacks run synchronously on the reload goroutine. They must return
// quickly; blocking I/O postpones the next reload. Spawn a goroutine
// inside the callback if needed.
//
// A panic in fn (or in a [WithEqual] comparator) is recovered and
// surfaced on [Manager.Errors]; it does not poison the writer or affect
// other subscribers. The returned cancel removes the subscription;
// calling it after Close() is a no-op.
//
// To run a side effect on every committed reload regardless of equality,
// pass WithEqual(func(_, _ *T) bool { return false }).
func Subscribe[T any, M any](
m *Manager[T],
extract func(*T) *M,
fn func(old, new *M),
opts ...SubscribeOption[M],
) (cancel func()) {
if m == nil {
return func() {}
}
cfg := subscribeOpts[M]{}
for _, opt := range opts {
if opt != nil {
opt(&cfg)
}
}
return imanager.Subscribe[T, M](m.inner, extract, fn, cfg.equal)
}
type TenantManager[T any] struct {
inner *itenant.Manager[T]
}
func NewTenantManager[T any]() *TenantManager[T] {
return &TenantManager[T]{inner: itenant.New[T]()}
}
func (tm *TenantManager[T]) Add(ctx context.Context, id string, opts ...Option) (*Manager[T], error) {
if tm == nil {
return nil, fmt.Errorf("fastconf: nil TenantManager")
}
m, err := tm.inner.Add(ctx, id, opts...)
if err != nil {
return nil, err
}
return &Manager[T]{inner: m}, nil
}
func (tm *TenantManager[T]) Get(id string) (*Manager[T], error) {
if tm == nil {
return nil, fmt.Errorf("%w: %q", itenant.ErrUnknownTenant, id)
}
m, err := tm.inner.Get(id)
if err != nil {
return nil, err
}
return &Manager[T]{inner: m}, nil
}
func (tm *TenantManager[T]) Has(id string) bool {
return tm != nil && tm.inner.Has(id)
}
func (tm *TenantManager[T]) Remove(id string) error {
if tm == nil {
return fmt.Errorf("%w: %q", itenant.ErrUnknownTenant, id)
}
return tm.inner.Remove(id)
}
func (tm *TenantManager[T]) Tenants() []string {
if tm == nil {
return nil
}
return tm.inner.Tenants()
}
func (tm *TenantManager[T]) Close() error {
if tm == nil {
return nil
}
return tm.inner.Close()
}
func wrapStates[T any](states []*istate.State[T]) []*State[T] {
if states == nil {
return nil
}
out := make([]*State[T], len(states))
for i, s := range states {
out[i] = wrapState(s)
}
return out
}
var ErrTenantExists = itenant.ErrTenantExists
var ErrUnknownTenant = itenant.ErrUnknownTenant