forked from forgejo/forgejo
Add Graceful shutdown for Windows and hooks for shutdown of goroutines (#8964)
* Graceful Shutdown for windows and others Restructures modules/graceful, adding shutdown for windows, removing and replacing the old minwinsvc code. Creates a new waitGroup - terminate which allows for goroutines to finish up after the shutdown of the servers. Shutdown and terminate hooks are added for goroutines. * Remove unused functions - these can be added in a different PR * Add startup timeout functionality * Document STARTUP_TIMEOUT
This commit is contained in:
parent
d7ac9727bb
commit
cbaa1de9ec
30 changed files with 666 additions and 497 deletions
|
@ -1,5 +1,3 @@
|
|||
// +build !windows
|
||||
|
||||
// Copyright 2019 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
@ -7,29 +5,37 @@
|
|||
package graceful
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
)
|
||||
|
||||
// awaitShutdown waits for the shutdown signal from the Manager
|
||||
func (srv *Server) awaitShutdown() {
|
||||
select {
|
||||
case <-Manager.IsShutdown():
|
||||
// Shutdown
|
||||
srv.doShutdown()
|
||||
case <-Manager.IsHammer():
|
||||
// Hammer
|
||||
srv.doShutdown()
|
||||
srv.doHammer()
|
||||
}
|
||||
<-Manager.IsHammer()
|
||||
srv.doHammer()
|
||||
}
|
||||
|
||||
// shutdown closes the listener so that no new connections are accepted
|
||||
// and starts a goroutine that will hammer (stop all running requests) the server
|
||||
// after setting.GracefulHammerTime.
|
||||
func (srv *Server) shutdown() {
|
||||
func (srv *Server) doShutdown() {
|
||||
// only shutdown if we're running.
|
||||
if srv.getState() != stateRunning {
|
||||
return
|
||||
}
|
||||
|
||||
srv.setState(stateShuttingDown)
|
||||
if setting.GracefulHammerTime >= 0 {
|
||||
go srv.hammerTime(setting.GracefulHammerTime)
|
||||
}
|
||||
|
||||
if srv.OnShutdown != nil {
|
||||
srv.OnShutdown()
|
||||
|
@ -42,14 +48,7 @@ func (srv *Server) shutdown() {
|
|||
}
|
||||
}
|
||||
|
||||
// hammerTime forces the server to shutdown in a given timeout - whether it
|
||||
// finished outstanding requests or not. if Read/WriteTimeout are not set or the
|
||||
// max header size is very big a connection could hang...
|
||||
//
|
||||
// srv.Serve() will not return until all connections are served. this will
|
||||
// unblock the srv.wg.Wait() in Serve() thus causing ListenAndServe* functions to
|
||||
// return.
|
||||
func (srv *Server) hammerTime(d time.Duration) {
|
||||
func (srv *Server) doHammer() {
|
||||
defer func() {
|
||||
// We call srv.wg.Done() until it panics.
|
||||
// This happens if we call Done() when the WaitGroup counter is already at 0
|
||||
|
@ -62,7 +61,6 @@ func (srv *Server) hammerTime(d time.Duration) {
|
|||
if srv.getState() != stateShuttingDown {
|
||||
return
|
||||
}
|
||||
time.Sleep(d)
|
||||
log.Warn("Forcefully shutting down parent")
|
||||
for {
|
||||
if srv.getState() == stateTerminate {
|
||||
|
@ -74,48 +72,3 @@ func (srv *Server) hammerTime(d time.Duration) {
|
|||
runtime.Gosched()
|
||||
}
|
||||
}
|
||||
|
||||
func (srv *Server) fork() error {
|
||||
runningServerReg.Lock()
|
||||
defer runningServerReg.Unlock()
|
||||
|
||||
// only one server instance should fork!
|
||||
if runningServersForked {
|
||||
return errors.New("another process already forked. Ignoring this one")
|
||||
}
|
||||
|
||||
runningServersForked = true
|
||||
|
||||
// We need to move the file logs to append pids
|
||||
setting.RestartLogsWithPIDSuffix()
|
||||
|
||||
_, err := RestartProcess()
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// RegisterPreSignalHook registers a function to be run before the signal handler for
|
||||
// a given signal. These are not mutex locked and should therefore be only called before Serve.
|
||||
func (srv *Server) RegisterPreSignalHook(sig os.Signal, f func()) (err error) {
|
||||
for _, s := range hookableSignals {
|
||||
if s == sig {
|
||||
srv.PreSignalHooks[sig] = append(srv.PreSignalHooks[sig], f)
|
||||
return
|
||||
}
|
||||
}
|
||||
err = fmt.Errorf("Signal %v is not supported", sig)
|
||||
return
|
||||
}
|
||||
|
||||
// RegisterPostSignalHook registers a function to be run after the signal handler for
|
||||
// a given signal. These are not mutex locked and should therefore be only called before Serve.
|
||||
func (srv *Server) RegisterPostSignalHook(sig os.Signal, f func()) (err error) {
|
||||
for _, s := range hookableSignals {
|
||||
if s == sig {
|
||||
srv.PostSignalHooks[sig] = append(srv.PostSignalHooks[sig], f)
|
||||
return
|
||||
}
|
||||
}
|
||||
err = fmt.Errorf("Signal %v is not supported", sig)
|
||||
return
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue