-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregistry.go
184 lines (146 loc) · 3.39 KB
/
registry.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
package winter
import (
"context"
"errors"
"sync"
)
// MiddlewareFunc middleware function for component
type MiddlewareFunc func(h HandlerFunc) HandlerFunc
// LifecycleFunc lifecycle function for component
type LifecycleFunc func(ctx context.Context) (err error)
// Registration a component registration in [Registry]
type Registration interface {
// Name returns name of registration
Name() string
// Startup set startup function
Startup(fn LifecycleFunc) Registration
// Check set check function
Check(fn LifecycleFunc) Registration
// Shutdown set shutdown function
Shutdown(fn LifecycleFunc) Registration
// Middleware set middleware function
Middleware(fn MiddlewareFunc) Registration
}
type Registry interface {
// Component register a component
//
// In order of `startup`, `check` and `shutdown`
Component(name string) Registration
// Startup start all registered components
Startup(ctx context.Context) (err error)
// Shutdown shutdown all registered components
Shutdown(ctx context.Context) (err error)
// Check run all component checks
Check(ctx context.Context, fn func(name string, err error))
// Wrap wrap [HandlerFunc] with all registered component middlewares
Wrap(h HandlerFunc) HandlerFunc
}
type registration struct {
name string
startup LifecycleFunc
check LifecycleFunc
shutdown LifecycleFunc
middleware MiddlewareFunc
}
func (r *registration) Name() string {
return r.name
}
func (r *registration) Startup(fn LifecycleFunc) Registration {
r.startup = fn
return r
}
func (r *registration) Check(fn LifecycleFunc) Registration {
r.check = fn
return r
}
func (r *registration) Shutdown(fn LifecycleFunc) Registration {
r.shutdown = fn
return r
}
func (r *registration) Middleware(fn MiddlewareFunc) Registration {
r.middleware = fn
return r
}
type registry struct {
mu sync.Locker
regs []*registration
init []*registration
}
func (a *registry) Component(name string) Registration {
a.mu.Lock()
defer a.mu.Unlock()
for _, item := range a.regs {
if item.name == name {
panic("duplicated component with name: " + name)
}
}
reg := ®istration{
name: name,
}
a.regs = append(a.regs, reg)
return reg
}
func (a *registry) Startup(ctx context.Context) (err error) {
a.mu.Lock()
defer a.mu.Unlock()
defer func() {
if err == nil {
return
}
for _, item := range a.init {
_ = item.shutdown(ctx)
}
a.init = nil
}()
for _, item := range a.regs {
if item.startup != nil {
if err = item.startup(ctx); err != nil {
return
}
}
a.init = append(a.init, item)
}
return
}
func (a *registry) Check(ctx context.Context, fn func(name string, err error)) {
a.mu.Lock()
defer a.mu.Unlock()
for _, item := range a.regs {
if item.check == nil {
fn(item.name, nil)
} else {
fn(item.name, item.check(ctx))
}
}
return
}
func (a *registry) Wrap(h HandlerFunc) HandlerFunc {
for _, item := range a.regs {
if item.middleware == nil {
continue
}
h = item.middleware(h)
}
return h
}
func (a *registry) Shutdown(ctx context.Context) (err error) {
a.mu.Lock()
defer a.mu.Unlock()
for _, item := range a.init {
if item.shutdown == nil {
continue
}
if err1 := item.shutdown(ctx); err1 != nil {
if err == nil {
err = err1
} else {
err = errors.New(err.Error() + "; " + err1.Error())
}
}
}
a.init = nil
return
}
func NewRegistry() Registry {
return ®istry{mu: &sync.Mutex{}}
}