-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraceful.go
300 lines (263 loc) · 7.75 KB
/
graceful.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
// Package graceful provides a mechanism for managing the lifecycle of services with dependencies.
//
// It ensures that services are started in the correct order and stopped in the reverse order,
// handling dependencies and errors gracefully.
//
// # Example
//
// import (
// "context"
// "fmt"
// "time"
// "github.com/your/package/graceful"
// )
//
// type ServiceA struct{}
//
// func (a *ServiceA) Start(ctx context.Context) error {
// fmt.Println("Service A starting...")
// time.Sleep(1 * time.Second) // Simulate service startup
// fmt.Println("Service A started.")
// return nil
// }
//
// func (a *ServiceA) Stop(ctx context.Context) error {
// fmt.Println("Service A stopping...")
// time.Sleep(1 * time.Second) // Simulate service shutdown
// fmt.Println("Service A stopped.")
// return nil
// }
//
// type ServiceB struct{}
//
// func (b *ServiceB) Start(ctx context.Context) error {
// fmt.Println("Service B starting...")
// time.Sleep(1 * time.Second) // Simulate service startup
// fmt.Println("Service B started.")
// return nil
// }
//
// func (b *ServiceB) Stop(ctx context.Context) error {
// fmt.Println("Service B stopping...")
// time.Sleep(1 * time.Second) // Simulate service shutdown
// fmt.Println("Service B stopped.")
// return nil
// }
//
// func main() {
// mgr := graceful.New()
// a := &ServiceA{}
// b := &ServiceB{}
//
// mgr.Add("service-a", a)
// mgr.Add("service-b", b, "service-a")
//
// ctx := context.Background()
//
// quit := make(chan os.Signal, 1)
// signal.Notify(terminate, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, os.Interrupt)
//
// // Start all services in the correct order.
// if err := mgr.Start(ctx); err != nil {
// fmt.Println("Error starting services:", err)
// return
// }
//
// // Wait for a signal to stop services.
// fmt.Println("Press Ctrl+C to stop services...")
// <-quit
//
// // Stop all services gracefully.
// if err := mgr.Stop(ctx); err != nil {
// fmt.Println("Error stopping services:", err)
// return
// }
//
// fmt.Println("Services stopped gracefully.")
// }
package graceful
import (
"context"
"fmt"
"sync"
)
type (
// Service is an interface representing a service that can be started and stopped.
Service interface {
// Start starts the service in the given context.
Start(ctx context.Context) error
// Stop stops the service in the given context.
Stop(ctx context.Context) error
}
// ServiceDef defines a service with its dependencies.
ServiceDef struct {
Service Service // service implementation
Name string // service name
Deps []string // list of dependencies
once sync.Once // Ensures Start is called only once for each service.
}
// Services is a map of service names to their definitions.
Services map[string]*ServiceDef
// Graceful manages the lifecycle of a set of services with dependencies.
// It ensures that services are started in the correct order and stopped in the reverse order.
Graceful struct {
svcs Services // Map of services.
graph sync.Map // Dependency graph of services.
order []string // Ordered list of service names.
cherr chan error // Channel for errors encountered during service lifecycle.
}
// GracefulError is an error that occurred during service lifecycle.
GracefulError struct {
Service string // Service name that failed
Reason string // Reason for the error
Err error // Underlying error
}
)
// Error returns a formatted error string.
func (e *GracefulError) Error() string {
return fmt.Sprintf("Error in service %s: %s: %v", e.Service, e.Reason, e.Err)
}
// NewGracefulError creates a new GracefulError.
func NewGracefulError(service, reason string, err error) *GracefulError {
return &GracefulError{Service: service, Reason: reason, Err: err}
}
// sort calculates the topological order of the services based on their dependencies.
// It implements [Kahn's algorithm] for topological sorting.
//
// - Time complexity: O(V+E), where V is the number of services and E is the number of dependencies.
// - Space complexity: O(V+E).
//
// [Kahn's algorithm]: https://www.geeksforgeeks.org/kahns-algorithm-vs-dfs-approach-a-comparative-analysis/
func (g *Graceful) sort() ([]string, error) {
// Calculate in-degree for each node (number of incoming edges)
degree := make(map[string]int)
for _, cmp := range g.svcs {
for _, dep := range cmp.Deps {
degree[dep]++
}
}
// Initialize a queue with nodes having in-degree 0 (no incoming edges)
queue := make([]string, 0)
for name := range g.svcs {
if _, ok := degree[name]; !ok {
degree[name] = 0
}
if degree[name] == 0 {
queue = append(queue, name)
}
}
// Initialize an empty slice to store the topological order
order := make([]string, 0)
// Perform Kahn's algorithm
for len(queue) > 0 {
// Dequeue a node
name := queue[0]
queue = queue[1:]
// Add the node to the topological order
order = append(order, name)
// Update in-degree of neighbors (remove outgoing edge)
deps, ok := g.graph.Load(name)
if !ok {
return nil, NewGracefulError(name, "dependency graph missing entry", nil)
}
list, ok := deps.([]string)
if !ok {
return nil, NewGracefulError(name, "invalid dependency type", nil)
}
for _, dep := range list {
// Check if dep is actually in the degree map
if _, ok := degree[dep]; ok {
degree[dep]--
// If in-degree of neighbor becomes 0, enqueue it
if degree[dep] == 0 {
queue = append(queue, dep)
}
}
}
}
// If there are still nodes with non-zero in-degree, the graph has a cycle and is not a DAG
for _, zero := range degree {
if zero > 0 {
return nil, NewGracefulError("", "dependency cycle detected", nil)
}
}
// Reverse the order to get the correct sequence for service startup
for i, j := 0, len(order)-1; i < j; i, j = i+1, j-1 {
order[i], order[j] = order[j], order[i]
}
return order, nil
}
// Add adds a new service to the graceful manager.
func (g *Graceful) Add(name string, svc Service, deps ...string) {
g.svcs[name] = &ServiceDef{Service: svc, Name: name, Deps: deps}
g.graph.Store(name, deps)
}
// Start starts all registered services in the order defined by their dependencies.
// It starts services concurrently and waits for all services to start successfully.
func (g *Graceful) Start(ctx context.Context) error {
g.cherr = make(chan error)
started := make(map[string]bool)
sorted, err := g.sort()
if err != nil {
return err
}
for _, name := range sorted {
svc, ok := g.svcs[name]
if !ok {
return NewGracefulError(name, "service not found", nil)
}
if svc == nil {
return NewGracefulError(name, "service is nil", nil)
}
svc.once.Do(func() {
for _, dep := range svc.Deps {
for {
_, ok := started[dep]
if ok {
break
}
}
}
go func() {
if err := svc.Service.Start(ctx); err != nil {
g.cherr <- NewGracefulError(name, "service start failed", err)
}
}()
g.order = append(g.order, name)
started[name] = true
})
}
return nil
}
// Stop stops all registered services in the reverse order they were started.
// It stops services concurrently and waits for all services to stop gracefully.
func (g *Graceful) Stop(ctx context.Context) error {
var wg sync.WaitGroup
// Use the reverse of the started order to stop services
for i := len(g.order) - 1; i >= 0; i-- {
name := g.order[i]
wg.Add(1)
go func() {
defer wg.Done()
for _, cmp := range g.svcs {
if cmp.Name == name {
if err := cmp.Service.Stop(ctx); err != nil {
g.cherr <- NewGracefulError(name, "service stop failed", err)
}
return
}
}
}()
}
wg.Wait()
select {
case err := <-g.cherr:
return err
default:
return nil
}
}
// New creates a new Graceful manager.
func New() *Graceful {
return &Graceful{svcs: make(Services)}
}