-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
service_lazy.go
194 lines (151 loc) · 3.88 KB
/
service_lazy.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
package do
import (
"context"
"reflect"
"sync"
"time"
"github.com/samber/do/v2/stacktrace"
)
var _ Service[int] = (*serviceLazy[int])(nil)
var _ serviceHealthcheck = (*serviceLazy[int])(nil)
var _ serviceShutdown = (*serviceLazy[int])(nil)
var _ serviceClone = (*serviceLazy[int])(nil)
type serviceLazy[T any] struct {
mu sync.RWMutex
name string
instance T
// lazy loading
built bool
buildTime time.Duration
provider Provider[T]
providerFrame stacktrace.Frame
invokationFrames []stacktrace.Frame
}
func newServiceLazy[T any](name string, provider Provider[T]) *serviceLazy[T] {
providerFrame, _ := stacktrace.NewFrameFromPtr(reflect.ValueOf(provider).Pointer())
return &serviceLazy[T]{
mu: sync.RWMutex{},
name: name,
built: false,
buildTime: 0,
provider: provider,
providerFrame: providerFrame,
invokationFrames: []stacktrace.Frame{},
}
}
func (s *serviceLazy[T]) getName() string {
return s.name
}
func (s *serviceLazy[T]) getType() ServiceType {
return ServiceTypeLazy
}
func (s *serviceLazy[T]) getEmptyInstance() any {
return empty[T]()
}
func (s *serviceLazy[T]) getInstanceAny(i Injector) (any, error) {
return s.getInstance(i)
}
func (s *serviceLazy[T]) getInstance(i Injector) (T, error) {
frame, ok := stacktrace.NewFrameFromCaller()
s.mu.Lock()
defer s.mu.Unlock()
if ok {
s.invokationFrames = append(s.invokationFrames, frame) // @TODO: potential memory leak
}
if !s.built {
err := s.build(i)
if err != nil {
return empty[T](), err
}
}
return s.instance, nil
}
func (s *serviceLazy[T]) build(i Injector) (err error) {
start := time.Now()
instance, err := handleProviderPanic(s.provider, i)
if err != nil {
return err
}
s.instance = instance
s.built = true
s.buildTime = time.Since(start)
return nil
}
func (s *serviceLazy[T]) isHealthchecker() bool {
s.mu.Lock()
defer s.mu.Unlock()
if !s.built {
return false
}
_, ok1 := any(s.instance).(HealthcheckerWithContext)
_, ok2 := any(s.instance).(Healthchecker)
return ok1 || ok2
}
func (s *serviceLazy[T]) healthcheck(ctx context.Context) error {
s.mu.RLock()
defer s.mu.RUnlock()
if !s.built {
return nil
}
if instance, ok := any(s.instance).(HealthcheckerWithContext); ok {
return instance.HealthCheck(ctx)
} else if instance, ok := any(s.instance).(Healthchecker); ok {
return instance.HealthCheck()
}
return nil
}
func (s *serviceLazy[T]) isShutdowner() bool {
s.mu.Lock()
defer s.mu.Unlock()
if !s.built {
return false
}
_, 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 *serviceLazy[T]) shutdown(ctx context.Context) error {
s.mu.Lock()
defer func() {
// whatever the outcome, reset `build` flag and instance
s.built = false
s.instance = empty[T]()
s.mu.Unlock()
}()
if !s.built {
return nil
}
if instance, ok := any(s.instance).(ShutdownerWithContextAndError); ok {
return instance.Shutdown(ctx)
} else if instance, ok := any(s.instance).(ShutdownerWithError); ok {
return instance.Shutdown()
} else if instance, ok := any(s.instance).(ShutdownerWithContext); ok {
instance.Shutdown(ctx)
return nil
} else if instance, ok := any(s.instance).(Shutdowner); ok {
instance.Shutdown()
return nil
}
return nil
}
func (s *serviceLazy[T]) clone() any {
// reset `build` flag and instance
return &serviceLazy[T]{
mu: sync.RWMutex{},
name: s.name,
built: false,
provider: s.provider,
buildTime: 0,
providerFrame: s.providerFrame,
invokationFrames: []stacktrace.Frame{},
}
}
//nolint:unused
func (s *serviceLazy[T]) source() (stacktrace.Frame, []stacktrace.Frame) {
return s.providerFrame, s.invokationFrames
}
func (s *serviceLazy[T]) getBuildTime() (time.Duration, bool) {
return s.buildTime, s.built
}