-
-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathroot_scope.go
More file actions
344 lines (286 loc) · 12 KB
/
Copy pathroot_scope.go
File metadata and controls
344 lines (286 loc) · 12 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
package do
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
)
// DefaultRootScopeName is the default name for the root scope.
const DefaultRootScopeName = "[root]"
// DefaultRootScope is a global instance of the root scope that can be used
// for simple dependency injection scenarios without creating a custom scope.
var DefaultRootScope = New()
// noOpLogf is a no-operation logging function used as a default when no logger is provided.
var noOpLogf = func(format string, args ...any) {}
// New creates a new dependency injection container with default options.
// This is the primary entry point for creating a new DI container.
//
// Parameters:
// - packages: Optional package functions to execute during initialization
//
// Returns a new RootScope instance ready for service registration.
//
// Play: https://go.dev/play/p/-Fvet5zLoVY
//
// Example:
//
// injector := do.New()
// do.Provide(injector, func(i do.Injector) (*MyService, error) {
// return &MyService{}, nil
// })
func New(packages ...func(Injector)) *RootScope {
return NewWithOpts(&InjectorOpts{}, packages...)
}
// NewWithOpts creates a new dependency injection container with custom options.
// This allows you to configure logging, hooks, health check settings, and other options.
//
// Parameters:
// - opts: Configuration options for the injector
// - packages: Optional package functions to execute during initialization
//
// Returns a new RootScope instance with the specified configuration.
//
// Play: https://go.dev/play/p/SmhFpWZGKUw
//
// Example:
//
// opts := &do.InjectorOpts{
// Logf: func(format string, args ...any) {
// log.Printf(format, args...)
// },
// HealthCheckParallelism: 10,
// }
// injector := do.NewWithOpts(opts)
func NewWithOpts(opts *InjectorOpts, packages ...func(Injector)) *RootScope {
if opts == nil {
opts = &InjectorOpts{}
}
if opts.Logf == nil {
opts.Logf = noOpLogf
}
if opts.HookBeforeRegistration == nil {
opts.HookBeforeRegistration = []func(*Scope, string){}
}
if opts.HookAfterRegistration == nil {
opts.HookAfterRegistration = []func(*Scope, string){}
}
if opts.HookBeforeInvocation == nil {
opts.HookBeforeInvocation = []func(*Scope, string){}
}
if opts.HookAfterInvocation == nil {
opts.HookAfterInvocation = []func(*Scope, string, error){}
}
if opts.HookBeforeShutdown == nil {
opts.HookBeforeShutdown = []func(*Scope, string){}
}
if opts.HookAfterShutdown == nil {
opts.HookAfterShutdown = []func(*Scope, string, error){}
}
root := &RootScope{
self: newScope(DefaultRootScopeName, nil, nil),
opts: opts,
dag: newDAG(),
healthCheckPool: nil,
}
root.self.rootScope = root
if opts.HealthCheckParallelism > 0 {
root.healthCheckPool = newJobPool[error](opts.HealthCheckParallelism)
root.healthCheckPool.start()
}
root.opts.Logf("DI: injector created")
for _, pkg := range packages {
pkg(root)
}
return root
}
// Ensure RootScope implements the Injector interface at compile time.
var _ Injector = (*RootScope)(nil)
// RootScope is the top-level scope in the dependency injection container hierarchy.
// It serves as the entry point for all DI operations and manages the overall container lifecycle.
//
// Key responsibilities:
// - Service registration and resolution
// - Child scope management
// - Health check coordination
// - Shutdown orchestration
// - Dependency graph management
type RootScope struct {
self *Scope // The root scope instance
opts *InjectorOpts // Configuration options
dag *DAG // Dependency graph for service relationships
healthCheckPool *jobPool[error] // Pool for parallel health check operations
}
// Pass-through methods that delegate to the underlying scope
// These methods provide the same interface as Scope but operate on the root level
// ID returns the unique identifier of the root scope.
func (s *RootScope) ID() string { return s.self.ID() }
// Name returns the name of the root scope.
func (s *RootScope) Name() string { return s.self.Name() }
// Scope creates a new child scope under the root scope.
func (s *RootScope) Scope(name string, p ...func(Injector)) *Scope { return s.self.Scope(name, p...) }
// RootScope returns the root scope itself (this instance).
func (s *RootScope) RootScope() *RootScope { return s.self.RootScope() }
// Ancestors returns an empty slice since the root scope has no ancestors.
func (s *RootScope) Ancestors() []*Scope { return []*Scope{} }
// Children returns the list of immediate child scopes.
func (s *RootScope) Children() []*Scope { return s.self.Children() }
// ChildByID searches for a child scope by its unique ID across the entire scope hierarchy.
func (s *RootScope) ChildByID(id string) (*Scope, bool) { return s.self.ChildByID(id) }
// ChildByName searches for a child scope by its name across the entire scope hierarchy.
func (s *RootScope) ChildByName(name string) (*Scope, bool) { return s.self.ChildByName(name) }
// ListProvidedServices returns all services available in the root scope and all its descendant scopes.
func (s *RootScope) ListProvidedServices() []ServiceDescription { return s.self.ListProvidedServices() }
// ListInvokedServices returns all services that have been invoked in the root scope and all its descendant scopes.
func (s *RootScope) ListInvokedServices() []ServiceDescription { return s.self.ListInvokedServices() }
// HealthCheck performs health checks on all services in the root scope and all its descendant scopes.
func (s *RootScope) HealthCheck() map[string]error { return s.self.HealthCheck() }
// HealthCheckWithContext performs health checks with context support for cancellation and timeouts.
func (s *RootScope) HealthCheckWithContext(ctx context.Context) map[string]error {
return s.self.HealthCheckWithContext(ctx)
}
// Shutdown gracefully shuts down the root scope and all its descendant scopes.
func (s *RootScope) Shutdown() *ShutdownReport { return s.ShutdownWithContext(context.Background()) }
// ShutdownWithContext gracefully shuts down the root scope and all its descendant scopes with context support.
// This method ensures proper cleanup of the health check pool and all registered services.
func (s *RootScope) ShutdownWithContext(ctx context.Context) *ShutdownReport {
defer func() {
if s.healthCheckPool != nil {
s.healthCheckPool.stop()
s.healthCheckPool = nil
}
}()
return s.self.ShutdownWithContext(ctx)
}
func (s *RootScope) clone(root *RootScope, parent *Scope) *Scope { return s.self.clone(root, parent) }
func (s *RootScope) serviceExist(name string) bool { return s.self.serviceExist(name) }
func (s *RootScope) serviceExistRec(name string) bool { return s.self.serviceExistRec(name) }
func (s *RootScope) serviceGet(name string) (any, bool) { return s.self.serviceGet(name) }
func (s *RootScope) serviceGetRec(name string) (any, *Scope, bool) { return s.self.serviceGetRec(name) }
func (s *RootScope) serviceSet(name string, service any) { s.self.serviceSet(name, service) } // serviceSet is not protected against double registration
func (s *RootScope) serviceForEach(cb func(string, *Scope, any) bool) { s.self.serviceForEach(cb) }
func (s *RootScope) serviceForEachRec(cb func(string, *Scope, any) bool) {
s.self.serviceForEachRec(cb)
}
func (s *RootScope) serviceHealthCheck(ctx context.Context, name string) error {
return s.self.serviceHealthCheck(ctx, name)
}
func (s *RootScope) serviceShutdown(ctx context.Context, name string) error {
return s.self.serviceShutdown(ctx, name)
}
func (s *RootScope) onServiceInvoke(name string) { s.self.onServiceInvoke(name) }
func (s *RootScope) queueServiceHealthcheck(ctx context.Context, scope *Scope, serviceName string) <-chan error {
cancel := func() {}
if s.opts.HealthCheckTimeout > 0 {
// `ctx` might already contain a timeout, but we add another one
ctx, cancel = context.WithTimeout(ctx, s.opts.HealthCheckTimeout)
}
// when no pooling policy has been defined
if s.opts.HealthCheckParallelism == 0 || s.healthCheckPool == nil {
err := make(chan error, 1) // a single message will be sent (nil or error)
go func() {
defer cancel()
select {
case e := <-func() chan error {
c := make(chan error, 1)
go func() { c <- scope.serviceHealthCheck(ctx, serviceName) }()
return c
}():
err <- e
close(err)
case <-ctx.Done():
err <- fmt.Errorf("%w: %s", ErrHealthCheckTimeout, ctx.Err()) //nolint:errorlint
close(err)
}
}()
return err
}
// delegate execution to the healthcheck pool
return s.healthCheckPool.rpc(func() error {
defer cancel()
return scope.serviceHealthCheck(ctx, serviceName)
})
}
/**
* RootScope stuff
*/
// AddBeforeRegistrationHook adds a hook that will be called before a service is registered.
//
// Play: https://go.dev/play/p/IstT_4oovQD
func (s *RootScope) AddBeforeRegistrationHook(hook func(*Scope, string)) {
s.opts.HookBeforeRegistration = append(s.opts.HookBeforeRegistration, hook)
}
// AddAfterRegistrationHook adds a hook that will be called after a service is registered.
//
// Play: https://go.dev/play/p/IstT_4oovQD
func (s *RootScope) AddAfterRegistrationHook(hook func(*Scope, string)) {
s.opts.HookAfterRegistration = append(s.opts.HookAfterRegistration, hook)
}
// AddBeforeInvocationHook adds a hook that will be called before a service is invoked.
//
// Play: https://go.dev/play/p/IstT_4oovQD
func (s *RootScope) AddBeforeInvocationHook(hook func(*Scope, string)) {
s.opts.HookBeforeInvocation = append(s.opts.HookBeforeInvocation, hook)
}
// AddAfterInvocationHook adds a hook that will be called after a service is invoked.
//
// Play: https://go.dev/play/p/IstT_4oovQD
func (s *RootScope) AddAfterInvocationHook(hook func(*Scope, string, error)) {
s.opts.HookAfterInvocation = append(s.opts.HookAfterInvocation, hook)
}
// AddBeforeShutdownHook adds a hook that will be called before a service is shutdown.
//
// Play: https://go.dev/play/p/FFKDcV0hMJx
func (s *RootScope) AddBeforeShutdownHook(hook func(*Scope, string)) {
s.opts.HookBeforeShutdown = append(s.opts.HookBeforeShutdown, hook)
}
// AddAfterShutdownHook adds a hook that will be called after a service is shutdown.
//
// Play: https://go.dev/play/p/FFKDcV0hMJx
func (s *RootScope) AddAfterShutdownHook(hook func(*Scope, string, error)) {
s.opts.HookAfterShutdown = append(s.opts.HookAfterShutdown, hook)
}
// Clone clones injector with provided services but not with invoked instances.
//
// Play: https://go.dev/play/p/DqIlXhZ8c4t
func (s *RootScope) Clone() *RootScope {
return s.CloneWithOpts(s.opts.copy())
}
// CloneWithOpts clones injector with provided services but not with invoked instances, with options.
//
// Play: https://go.dev/play/p/fT-v63fbFk5
func (s *RootScope) CloneWithOpts(opts *InjectorOpts) *RootScope {
clone := NewWithOpts(opts)
clone.self = s.clone(clone, nil)
s.opts.Logf("DI: injector cloned")
return clone
}
// ShutdownOnSignals listens for the provided signals in order to gracefully stop services.
// It will block until receiving any of these signals.
// If no signal is provided, syscall.SIGTERM and os.Interrupt will be handled by default.
func (s *RootScope) ShutdownOnSignals(signals ...os.Signal) (os.Signal, *ShutdownReport) {
return s.ShutdownOnSignalsWithContext(context.Background(), signals...)
}
// ShutdownOnSignalsWithContext listens for the provided signals in order to gracefully stop services.
// It will block until receiving any of these signals.
// If no signal is provided, syscall.SIGTERM and os.Interrupt will be handled by default.
func (s *RootScope) ShutdownOnSignalsWithContext(ctx context.Context, signals ...os.Signal) (os.Signal, *ShutdownReport) {
// Make sure there is at least syscall.SIGTERM and os.Interrupt as a signal
if len(signals) < 1 {
signals = append(signals, syscall.SIGTERM, os.Interrupt)
}
ch := make(chan os.Signal, 5)
signal.Notify(ch, signals...)
var sig os.Signal
select {
case sig = <-ch:
// got a signal
case <-ctx.Done():
signal.Stop(ch)
close(ch)
return nil, s.ShutdownWithContext(ctx)
}
signal.Stop(ch)
close(ch)
return sig, s.ShutdownWithContext(ctx)
}