-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathshutdown.go
More file actions
194 lines (176 loc) · 4.49 KB
/
Copy pathshutdown.go
File metadata and controls
194 lines (176 loc) · 4.49 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
package main
import (
"context"
"sync"
"time"
libpack_logging "github.com/lukaszraczylo/graphql-monitoring-proxy/logging"
)
// ShutdownManager manages graceful shutdown for all components
type ShutdownManager struct {
ctx context.Context
cancel context.CancelFunc
components []ShutdownComponent
wg sync.WaitGroup
shutdownOnce sync.Once
mu sync.Mutex
}
// ShutdownComponent represents a component that needs graceful shutdown
type ShutdownComponent struct {
Shutdown func(context.Context) error
Name string
}
// NewShutdownManager creates a new shutdown manager
func NewShutdownManager(ctx context.Context) *ShutdownManager {
ctx, cancel := context.WithCancel(ctx)
return &ShutdownManager{
ctx: ctx,
cancel: cancel,
}
}
// RegisterComponent registers a component for graceful shutdown
func (sm *ShutdownManager) RegisterComponent(name string, shutdown func(context.Context) error) {
sm.mu.Lock()
defer sm.mu.Unlock()
sm.components = append(sm.components, ShutdownComponent{
Name: name,
Shutdown: shutdown,
})
}
// RunGoroutine starts a goroutine that respects the shutdown context
func (sm *ShutdownManager) RunGoroutine(name string, fn func(context.Context)) {
sm.wg.Add(1)
go func() {
defer sm.wg.Done()
cfgMutex.RLock()
logger := cfg.Logger
cfgMutex.RUnlock()
if logger != nil {
logger.Debug(&libpack_logging.LogMessage{
Message: "Starting managed goroutine",
Pairs: map[string]any{"name": name},
})
}
fn(sm.ctx)
cfgMutex.RLock()
logger = cfg.Logger
cfgMutex.RUnlock()
if logger != nil {
logger.Debug(&libpack_logging.LogMessage{
Message: "Managed goroutine finished",
Pairs: map[string]any{"name": name},
})
}
}()
}
// Shutdown initiates graceful shutdown of all components
func (sm *ShutdownManager) Shutdown(timeout time.Duration) error {
var err error
sm.shutdownOnce.Do(func() {
err = sm.doShutdown(timeout)
})
return err
}
// doShutdown performs the actual shutdown logic
func (sm *ShutdownManager) doShutdown(timeout time.Duration) error {
cfgMutex.RLock()
logger := cfg.Logger
cfgMutex.RUnlock()
if logger != nil {
logger.Info(&libpack_logging.LogMessage{
Message: "Initiating graceful shutdown",
})
}
// Cancel the context to signal all goroutines to stop
sm.cancel()
// Create a timeout context for component shutdown
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), timeout)
defer shutdownCancel()
// Shutdown all registered components
sm.mu.Lock()
components := make([]ShutdownComponent, len(sm.components))
copy(components, sm.components)
sm.mu.Unlock()
var shutdownWg sync.WaitGroup
for _, comp := range components {
shutdownWg.Add(1)
go func(c ShutdownComponent) {
defer shutdownWg.Done()
cfgMutex.RLock()
logger := cfg.Logger
cfgMutex.RUnlock()
if logger != nil {
logger.Info(&libpack_logging.LogMessage{
Message: "Shutting down component",
Pairs: map[string]any{"component": c.Name},
})
}
if err := c.Shutdown(shutdownCtx); err != nil {
cfgMutex.RLock()
logger := cfg.Logger
cfgMutex.RUnlock()
if logger != nil {
logger.Error(&libpack_logging.LogMessage{
Message: "Error shutting down component",
Pairs: map[string]any{
"component": c.Name,
"error": err.Error(),
},
})
}
}
}(comp)
}
// Wait for all components to shutdown
componentsDone := make(chan struct{})
go func() {
shutdownWg.Wait()
close(componentsDone)
}()
// Wait for goroutines with timeout
goroutinesDone := make(chan struct{})
go func() {
sm.wg.Wait()
close(goroutinesDone)
}()
select {
case <-componentsDone:
cfgMutex.RLock()
logger := cfg.Logger
cfgMutex.RUnlock()
if logger != nil {
logger.Info(&libpack_logging.LogMessage{
Message: "All components shut down successfully",
})
}
case <-shutdownCtx.Done():
cfgMutex.RLock()
logger := cfg.Logger
cfgMutex.RUnlock()
if logger != nil {
logger.Warning(&libpack_logging.LogMessage{
Message: "Component shutdown timed out",
})
}
}
select {
case <-goroutinesDone:
cfgMutex.RLock()
logger := cfg.Logger
cfgMutex.RUnlock()
if logger != nil {
logger.Info(&libpack_logging.LogMessage{
Message: "All goroutines finished",
})
}
case <-time.After(timeout):
cfgMutex.RLock()
logger := cfg.Logger
cfgMutex.RUnlock()
if logger != nil {
logger.Warning(&libpack_logging.LogMessage{
Message: "Some goroutines didn't finish within timeout",
})
}
}
return nil
}