-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplumber.go
350 lines (309 loc) · 8.2 KB
/
plumber.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
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
// Copyright 2024 Outreach Corporation. All Rights Reserved.
// Description: This file contains dependency management helpers
// Package plumber package provides orchestration library to construct application dependency graph and manage service tasks
package plumber
import (
"context"
"errors"
"fmt"
"reflect"
"sync"
)
// Errors
var (
// ErrCircularDependency error indicating circular dependency
ErrCircularDependency = errors.New("circular dependency")
// ErrRunnerNotDefined error indicates that ResolutionR doesn't have runner set
ErrRunnerNotDefined = errors.New("runner not defined")
)
// Dependency represent a dependency that can be supplied into Require method
type Dependency interface {
Iterate(func(dep Dependency) bool)
Error() error
}
// Future represents a struct that will help with dependency evaluation
type Future[T any] struct {
d *D[T]
}
// Then evaluates a dependencies and trigger callback when all good
func (f *Future[T]) Then(callback func()) {
var errs []error
for _, d := range f.d.deps {
var (
circular bool
err error
)
d.Iterate(func(dep Dependency) bool {
if f.d == dep {
circular = true
}
return !circular
})
if circular {
err = ErrCircularDependency
}
if err == nil {
err = d.Error()
}
if err != nil {
errs = append(errs, fmt.Errorf("dependency not resolved, %s requires %s (%w)", f.d, d, err))
}
}
if len(errs) != 0 {
f.d.err = errors.Join(errs...)
return
}
callback()
}
// D represent a dependency wrapper
type D[T any] struct {
resolving bool
defined bool
resolved bool
value T
err error
once sync.Once
mx sync.Mutex
resolve func()
deps []Dependency
listeners []func()
wrappers []func(T) T
}
// String return names of underlaying type
func (d *D[T]) String() string {
var v T
return reflect.TypeOf(&v).Elem().String()
}
// define sets resolution function but only once
func (d *D[T]) define(resolve func()) {
d.once.Do(func() {
d.defined = true
d.resolve = func() {
resolve()
d.resolved = true
d.resolving = false
for _, w := range d.wrappers {
d.value = w(d.value)
}
for _, l := range d.listeners {
l()
}
}
})
}
// Define allows to define value using callback that returns a value and error
func (d *D[T]) DefineError(resolve func() (T, error)) *D[T] {
d.define(func() {
d.value, d.err = resolve()
})
return d
}
// Define allows to define value using callback that returns a value
func (d *D[T]) Define(resolve func() T) *D[T] {
d.define(func() {
d.value = resolve()
})
return d
}
// Const assigns a static value
func (d *D[T]) Const(v T) *D[T] {
d.define(func() {
d.value = v
})
return d
}
// Use overwrites defined value with specific instance. Should be used only for testings
func (d *D[T]) Use(v T) *D[T] {
d.resolved = true
d.value = v
return d
}
// Must returns a value or panics in case of the error
func (d *D[T]) Must() T {
v, err := d.InstanceError()
if err != nil {
panic(err)
}
return v
}
// Instance returns a value
func (d *D[T]) Instance() T {
d.mx.Lock()
defer d.mx.Unlock()
var zero T
if !d.defined {
return zero
}
if d.resolve != nil && !d.resolved {
d.resolving = true
d.resolve()
}
return d.value
}
// InstanceError returns and a value and the error
func (d *D[T]) InstanceError() (T, error) {
v := d.Instance()
err := d.err
if !d.defined {
err = fmt.Errorf("instance %s not resolved", d)
}
return v, err
}
// Error returns and error
func (d *D[T]) Error() error {
_, err := d.InstanceError()
return err
}
// Iterate iterates dependency graph, when callback returns true iterator will continue down stream
func (d *D[T]) Iterate(callback func(dep Dependency) bool) {
for _, dep := range d.deps {
if !callback(dep) {
break
}
dep.Iterate(callback)
}
}
// Resolve returns a callback providing a resolution orchestrator
// Using the orchestrator we can define dependencies between values
func (d *D[T]) Resolve(callback func(*Resolution[T])) *D[T] {
d.define(func() {
r := Resolution[T]{d: d}
callback(&r)
})
return d
}
// WhenResolved registers a callback that will be triggered when dependency is resolved
func (d *D[T]) WhenResolved(callback func()) *D[T] {
d.listeners = append(d.listeners, callback)
return d
}
// Wrap registers a wrapping callback that will be triggered when dependency is resolved
// The callback allows to augment the original value. Wrapping should be used mostly to
// redefine the dependency for a different test environments
func (d *D[T]) Wrap(wrappers ...func(T) T) *D[T] {
d.wrappers = append(d.wrappers, wrappers...)
return d
}
// R represents a runnable dependency wrapper
// It is meant to be supplied into the Pipeline()
type R[T any] struct {
D[T]
runnable Runner
}
// Resolve returns a callback providing a resolution orchestrator
// Using the orchestrator we can define dependencies between values
func (r *R[T]) Resolve(callback func(*ResolutionR[T])) *R[T] {
r.D.Resolve(func(dr *Resolution[T]) {
rr := &ResolutionR[T]{resolution: dr, r: r}
callback(rr)
})
return r
}
// Define allows to define value using callback that returns a value
func (r *R[T]) Define(resolve func() T) *R[T] {
r.D.Define(func() T {
rv := resolve()
var v any = rv
if runner, ok := v.(Runner); ok {
r.runnable = runner
}
return rv
})
return r
}
// Run executes Run method on value and satisfies Runner,Closer and Readier interfaces
func (r *R[T]) Run(ctx context.Context) error {
if err := r.D.Error(); err != nil {
return err
}
if r.runnable == nil {
return fmt.Errorf("Runnable %s not resolved", &r.D)
}
return r.runnable.Run(ctx)
}
// Close executes Close method on value and satisfies Closer interface
func (r *R[T]) Close(ctx context.Context) error {
if err := r.D.Error(); err != nil {
return err
}
if r.runnable == nil {
return fmt.Errorf("Runnable %s not resolved", &r.D)
}
return RunnerClose(ctx, r.runnable)
}
func (r *R[T]) Ready() (<-chan struct{}, error) {
if err := r.D.Error(); err != nil {
return nil, err
}
if r.runnable == nil {
return nil, fmt.Errorf("Runnable %s not resolved", &r.D)
}
return RunnerReady(r.runnable)
}
// Resolution is value resolution orchestrator
type Resolution[T any] struct {
d *D[T]
}
// Resolved ends the resolution with given value
func (r *Resolution[T]) Resolve(v T) {
r.d.value = v
}
// Error ends resolution with and error
func (r *Resolution[T]) Error(err error) {
r.d.err = err
}
// ResolveError ends the resolution with given value and error
func (r *Resolution[T]) ResolveError(v T, err error) {
r.Resolve(v)
r.Error(err)
}
// Require allows to define a dependant for the current value
// It is a necessary to call Then to trigger a dependency evaluation
func (r *Resolution[T]) Require(deps ...Dependency) *Future[T] {
r.d.deps = deps
return &Future[T]{
d: r.d,
}
}
// ResolutionR represents a resolution orchestrator for a runnable values
type ResolutionR[T any] struct {
r *R[T]
resolution *Resolution[T]
}
// Error ends resolution with and error
func (rr *ResolutionR[T]) Error(err error) {
rr.resolution.Error(err)
}
// Resolved ends the resolution with given runnable value
// This instance will be executed once a R included int the started pipeline
func (rr *ResolutionR[T]) Resolve(v Runner) {
rr.resolution.Resolve(v.(T))
rr.r.runnable = v
}
// ResolveError ends the resolution with given value and error
func (rr *ResolutionR[T]) ResolveError(v Runner, err error) {
rr.Resolve(v)
rr.Error(err)
}
// ResolveAdapter ends the resolution with given value and runnable adapter
// that will be executed once a R is included int the started pipeline
func (rr *ResolutionR[T]) ResolveAdapter(v T, runnable Runner) {
rr.resolution.Resolve(v)
rr.r.runnable = runnable
}
// Require allows to define a dependant for the current value
// It is a necessary to call Then to trigger a dependency evaluation
func (rr *ResolutionR[T]) Require(deps ...Dependency) *Future[T] {
return rr.resolution.Require(deps...)
}
// Resolved checks given dependencies and checks whether they are resolved or not.
// Multi error is returned.
func Resolved(deps ...interface{ Error() error }) error {
var errs []error
for _, d := range deps {
if err := d.Error(); err != nil {
errs = append(errs, err)
}
}
return errors.Join(errs...)
}