-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice_eager.go
More file actions
166 lines (135 loc) · 4.01 KB
/
service_eager.go
File metadata and controls
166 lines (135 loc) · 4.01 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
package do
import (
"context"
"reflect"
"sync"
"sync/atomic"
"github.com/samber/do/v2/stacktrace"
)
var (
_ serviceWrapper[int] = (*serviceEager[int])(nil)
_ serviceWrapperHealthcheck = (*serviceEager[int])(nil)
_ serviceWrapperShutdown = (*serviceEager[int])(nil)
_ serviceWrapperClone = (*serviceEager[int])(nil)
)
type serviceEager[T any] struct {
name string
typeName string
instance T
providerFrame stacktrace.Frame
invokationFrames map[stacktrace.Frame]struct{} // map garanties uniqueness
invokationFramesMu sync.RWMutex
invokationFramesCounter uint32
}
func newServiceEager[T any](name string, instance T) *serviceEager[T] {
providerFrame, _ := stacktrace.NewFrameFromCaller()
return &serviceEager[T]{
name: name,
typeName: inferServiceName[T](),
instance: instance,
providerFrame: providerFrame,
invokationFrames: map[stacktrace.Frame]struct{}{},
invokationFramesMu: sync.RWMutex{},
invokationFramesCounter: 0,
}
}
func (s *serviceEager[T]) getName() string {
return s.name
}
func (s *serviceEager[T]) getTypeName() string {
return s.typeName
}
func (s *serviceEager[T]) getServiceType() ServiceType {
return ServiceTypeEager
}
func (s *serviceEager[T]) getReflectType() reflect.Type {
return reflect.TypeOf((*T)(nil)).Elem() // if T is a pointer or interface, it will return a typed nil
}
func (s *serviceEager[T]) getInstanceAny(i Injector) (any, error) {
return s.getInstance(i)
}
func (s *serviceEager[T]) getInstance(i Injector) (T, error) {
// Collect up to 100 invokation frames.
// In the future, we can implement a LFU list, to evict the oldest
// frames and keep the most recent ones, but it would be much more costly.
if atomic.AddUint32(&s.invokationFramesCounter, 1) < MaxInvocationFrames {
s.invokationFramesMu.Lock()
frame, ok := stacktrace.NewFrameFromCaller()
if ok {
s.invokationFrames[frame] = struct{}{}
}
s.invokationFramesMu.Unlock()
}
return s.instance, nil
}
func (s *serviceEager[T]) isHealthchecker() bool {
_, ok1 := any(s.instance).(HealthcheckerWithContext)
_, ok2 := any(s.instance).(Healthchecker)
return ok1 || ok2
}
func (s *serviceEager[T]) healthcheck(ctx context.Context) error {
if instance, ok := any(s.instance).(HealthcheckerWithContext); ok {
if ctx.Err() != nil {
return ctx.Err()
}
return instance.HealthCheck(ctx)
} else if instance, ok := any(s.instance).(Healthchecker); ok {
if ctx.Err() != nil {
return ctx.Err()
}
return instance.HealthCheck()
}
return nil
}
func (s *serviceEager[T]) isShutdowner() bool {
_, ok1 := any(s.instance).(ShutdownerWithContextAndError)
_, ok2 := any(s.instance).(ShutdownerWithError)
_, ok3 := any(s.instance).(ShutdownerWithContext)
_, ok4 := any(s.instance).(Shutdowner)
return ok1 || ok2 || ok3 || ok4
}
func (s *serviceEager[T]) shutdown(ctx context.Context) error {
switch instance := any(s.instance).(type) {
case ShutdownerWithContextAndError:
if ctx.Err() != nil {
return ctx.Err()
}
return instance.Shutdown(ctx)
case ShutdownerWithError:
if ctx.Err() != nil {
return ctx.Err()
}
return instance.Shutdown()
case ShutdownerWithContext:
if ctx.Err() != nil {
return ctx.Err()
}
instance.Shutdown(ctx)
case Shutdowner:
if ctx.Err() != nil {
return ctx.Err()
}
instance.Shutdown()
}
return nil
}
func (s *serviceEager[T]) clone(newScope Injector) any {
return &serviceEager[T]{
name: s.name,
typeName: s.typeName,
instance: s.instance,
providerFrame: s.providerFrame,
invokationFrames: map[stacktrace.Frame]struct{}{},
invokationFramesMu: sync.RWMutex{},
invokationFramesCounter: 0,
}
}
func (s *serviceEager[T]) source() (stacktrace.Frame, []stacktrace.Frame) {
s.invokationFramesMu.RLock()
invokationFrames := make([]stacktrace.Frame, 0, len(s.invokationFrames))
for frame := range s.invokationFrames {
invokationFrames = append(invokationFrames, frame)
}
s.invokationFramesMu.RUnlock()
return s.providerFrame, invokationFrames
}