@@ -12,7 +12,6 @@ import (
12
12
"os/signal"
13
13
"runtime/pprof"
14
14
"strconv"
15
- "sync"
16
15
"syscall"
17
16
"time"
18
17
@@ -22,51 +21,6 @@ import (
22
21
"code.gitea.io/gitea/modules/setting"
23
22
)
24
23
25
- // Manager manages the graceful shutdown process
26
- type Manager struct {
27
- isChild bool
28
- forked bool
29
- lock * sync.RWMutex
30
- state state
31
- shutdownCtx context.Context
32
- hammerCtx context.Context
33
- terminateCtx context.Context
34
- managerCtx context.Context
35
- shutdownCtxCancel context.CancelFunc
36
- hammerCtxCancel context.CancelFunc
37
- terminateCtxCancel context.CancelFunc
38
- managerCtxCancel context.CancelFunc
39
- runningServerWaitGroup sync.WaitGroup
40
- createServerWaitGroup sync.WaitGroup
41
- terminateWaitGroup sync.WaitGroup
42
-
43
- toRunAtShutdown []func ()
44
- toRunAtTerminate []func ()
45
- }
46
-
47
- func newGracefulManager (ctx context.Context ) * Manager {
48
- manager := & Manager {
49
- isChild : len (os .Getenv (listenFDsEnv )) > 0 && os .Getppid () > 1 ,
50
- lock : & sync.RWMutex {},
51
- }
52
- manager .createServerWaitGroup .Add (numberOfServersToCreate )
53
- manager .start (ctx )
54
- return manager
55
- }
56
-
57
- type systemdNotifyMsg string
58
-
59
- const (
60
- readyMsg systemdNotifyMsg = "READY=1"
61
- stoppingMsg systemdNotifyMsg = "STOPPING=1"
62
- reloadingMsg systemdNotifyMsg = "RELOADING=1"
63
- watchdogMsg systemdNotifyMsg = "WATCHDOG=1"
64
- )
65
-
66
- func statusMsg (msg string ) systemdNotifyMsg {
67
- return systemdNotifyMsg ("STATUS=" + msg )
68
- }
69
-
70
24
func pidMsg () systemdNotifyMsg {
71
25
return systemdNotifyMsg ("MAINPID=" + strconv .Itoa (os .Getpid ()))
72
26
}
@@ -89,27 +43,13 @@ func (g *Manager) notify(msg systemdNotifyMsg) {
89
43
}
90
44
}
91
45
92
- func (g * Manager ) start (ctx context.Context ) {
93
- // Make contexts
94
- g .terminateCtx , g .terminateCtxCancel = context .WithCancel (ctx )
95
- g .shutdownCtx , g .shutdownCtxCancel = context .WithCancel (ctx )
96
- g .hammerCtx , g .hammerCtxCancel = context .WithCancel (ctx )
97
- g .managerCtx , g .managerCtxCancel = context .WithCancel (ctx )
98
-
99
- // Next add pprof labels to these contexts
100
- g .terminateCtx = pprof .WithLabels (g .terminateCtx , pprof .Labels ("graceful-lifecycle" , "with-terminate" ))
101
- g .shutdownCtx = pprof .WithLabels (g .shutdownCtx , pprof .Labels ("graceful-lifecycle" , "with-shutdown" ))
102
- g .hammerCtx = pprof .WithLabels (g .hammerCtx , pprof .Labels ("graceful-lifecycle" , "with-hammer" ))
103
- g .managerCtx = pprof .WithLabels (g .managerCtx , pprof .Labels ("graceful-lifecycle" , "with-manager" ))
104
-
46
+ func (g * Manager ) start () {
105
47
// Now label this and all goroutines created by this goroutine with the graceful-lifecycle manager
106
48
pprof .SetGoroutineLabels (g .managerCtx )
107
- defer pprof .SetGoroutineLabels (ctx )
49
+ defer pprof .SetGoroutineLabels (g .ctx )
50
+
51
+ g .isChild = len (os .Getenv (listenFDsEnv )) > 0 && os .Getppid () > 1
108
52
109
- // Set the running state & handle signals
110
- if ! g .setStateTransition (stateInit , stateRunning ) {
111
- panic ("invalid graceful manager state: transition from init to running failed" )
112
- }
113
53
g .notify (statusMsg ("Starting Gitea" ))
114
54
g .notify (pidMsg ())
115
55
go g .handleSignals (g .managerCtx )
@@ -118,11 +58,9 @@ func (g *Manager) start(ctx context.Context) {
118
58
startupDone := make (chan struct {})
119
59
go func () {
120
60
defer close (startupDone )
121
- // Wait till we're done getting all of the listeners and then close
122
- // the unused ones
61
+ // Wait till we're done getting all the listeners and then close the unused ones
123
62
g .createServerWaitGroup .Wait ()
124
- // Ignore the error here there's not much we can do with it
125
- // They're logged in the CloseProvidedListeners function
63
+ // Ignore the error here there's not much we can do with it, they're logged in the CloseProvidedListeners function
126
64
_ = CloseProvidedListeners ()
127
65
g .notify (readyMsg )
128
66
}()
@@ -133,7 +71,7 @@ func (g *Manager) start(ctx context.Context) {
133
71
return
134
72
case <- g .IsShutdown ():
135
73
func () {
136
- // When waitgroup counter goes negative it will panic - we don't care about this so we can just ignore it.
74
+ // When WaitGroup counter goes negative it will panic - we don't care about this so we can just ignore it.
137
75
defer func () {
138
76
_ = recover ()
139
77
}()
@@ -255,29 +193,3 @@ func (g *Manager) DoGracefulRestart() {
255
193
g .doShutdown ()
256
194
}
257
195
}
258
-
259
- // DoImmediateHammer causes an immediate hammer
260
- func (g * Manager ) DoImmediateHammer () {
261
- g .notify (statusMsg ("Sending immediate hammer" ))
262
- g .doHammerTime (0 * time .Second )
263
- }
264
-
265
- // DoGracefulShutdown causes a graceful shutdown
266
- func (g * Manager ) DoGracefulShutdown () {
267
- g .lock .Lock ()
268
- if ! g .forked {
269
- g .lock .Unlock ()
270
- g .notify (stoppingMsg )
271
- } else {
272
- g .lock .Unlock ()
273
- g .notify (statusMsg ("Shutting down after fork" ))
274
- }
275
- g .doShutdown ()
276
- }
277
-
278
- // RegisterServer registers the running of a listening server, in the case of unix this means that the parent process can now die.
279
- // Any call to RegisterServer must be matched by a call to ServerDone
280
- func (g * Manager ) RegisterServer () {
281
- KillParent ()
282
- g .runningServerWaitGroup .Add (1 )
283
- }
0 commit comments