diff options
author | wxiaoguang <wxiaoguang@gmail.com> | 2023-05-26 15:31:55 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-26 07:31:55 +0000 |
commit | 18f26cfbf7f9b36b838c0e8762bfba98c89b9797 (patch) | |
tree | f3bcf0ef2b76601980ab6144e9ce9acd5316a0da /modules/graceful/manager.go | |
parent | e4922d484b9ee94ba22e5ff08b3c25c8be09d9c8 (diff) | |
download | gitea-18f26cfbf7f9b36b838c0e8762bfba98c89b9797.tar.gz gitea-18f26cfbf7f9b36b838c0e8762bfba98c89b9797.zip |
Improve queue and logger context (#24924)
Before there was a "graceful function": RunWithShutdownFns, it's mainly
for some modules which doesn't support context.
The old queue system doesn't work well with context, so the old queues
need it.
After the queue refactoring, the new queue works with context well, so,
use Golang context as much as possible, the `RunWithShutdownFns` could
be removed (replaced by RunWithCancel for context cancel mechanism), the
related code could be simplified.
This PR also fixes some legacy queue-init problems, eg:
* typo : archiver: "unable to create codes indexer queue" => "unable to
create repo-archive queue"
* no nil check for failed queues, which causes unfriendly panic
After this PR, many goroutines could have better display name:
![image](https://github.com/go-gitea/gitea/assets/2114189/701b2a9b-8065-4137-aeaa-0bda2b34604a)
![image](https://github.com/go-gitea/gitea/assets/2114189/f1d5f50f-0534-40f0-b0be-f2c9daa5fe92)
Diffstat (limited to 'modules/graceful/manager.go')
-rw-r--r-- | modules/graceful/manager.go | 62 |
1 files changed, 11 insertions, 51 deletions
diff --git a/modules/graceful/manager.go b/modules/graceful/manager.go index d32788092d..3604c0a3f5 100644 --- a/modules/graceful/manager.go +++ b/modules/graceful/manager.go @@ -23,6 +23,11 @@ const ( stateTerminate ) +type RunCanceler interface { + Run() + Cancel() +} + // There are some places that could inherit sockets: // // * HTTP or HTTPS main listener @@ -55,46 +60,19 @@ func InitManager(ctx context.Context) { }) } -// WithCallback is a runnable to call when the caller has finished -type WithCallback func(callback func()) - -// RunnableWithShutdownFns is a runnable with functions to run at shutdown and terminate -// After the callback to atShutdown is called and is complete, the main function must return. -// Similarly the callback function provided to atTerminate must return once termination is complete. -// Please note that use of the atShutdown and atTerminate callbacks will create go-routines that will wait till their respective signals -// - users must therefore be careful to only call these as necessary. -type RunnableWithShutdownFns func(atShutdown, atTerminate func(func())) - -// RunWithShutdownFns takes a function that has both atShutdown and atTerminate callbacks -// After the callback to atShutdown is called and is complete, the main function must return. -// Similarly the callback function provided to atTerminate must return once termination is complete. -// Please note that use of the atShutdown and atTerminate callbacks will create go-routines that will wait till their respective signals -// - users must therefore be careful to only call these as necessary. -func (g *Manager) RunWithShutdownFns(run RunnableWithShutdownFns) { +// RunWithCancel helps to run a function with a custom context, the Cancel function will be called at shutdown +// The Cancel function should stop the Run function in predictable time. +func (g *Manager) RunWithCancel(rc RunCanceler) { + g.RunAtShutdown(context.Background(), rc.Cancel) g.runningServerWaitGroup.Add(1) defer g.runningServerWaitGroup.Done() defer func() { if err := recover(); err != nil { - log.Critical("PANIC during RunWithShutdownFns: %v\nStacktrace: %s", err, log.Stack(2)) + log.Critical("PANIC during RunWithCancel: %v\nStacktrace: %s", err, log.Stack(2)) g.doShutdown() } }() - run(func(atShutdown func()) { - g.lock.Lock() - defer g.lock.Unlock() - g.toRunAtShutdown = append(g.toRunAtShutdown, - func() { - defer func() { - if err := recover(); err != nil { - log.Critical("PANIC during RunWithShutdownFns: %v\nStacktrace: %s", err, log.Stack(2)) - g.doShutdown() - } - }() - atShutdown() - }) - }, func(atTerminate func()) { - g.RunAtTerminate(atTerminate) - }) + rc.Run() } // RunWithShutdownContext takes a function that has a context to watch for shutdown. @@ -151,21 +129,6 @@ func (g *Manager) RunAtShutdown(ctx context.Context, shutdown func()) { }) } -// RunAtHammer creates a go-routine to run the provided function at shutdown -func (g *Manager) RunAtHammer(hammer func()) { - g.lock.Lock() - defer g.lock.Unlock() - g.toRunAtHammer = append(g.toRunAtHammer, - func() { - defer func() { - if err := recover(); err != nil { - log.Critical("PANIC during RunAtHammer: %v\nStacktrace: %s", err, log.Stack(2)) - } - }() - hammer() - }) -} - func (g *Manager) doShutdown() { if !g.setStateTransition(stateRunning, stateShuttingDown) { g.DoImmediateHammer() @@ -206,9 +169,6 @@ func (g *Manager) doHammerTime(d time.Duration) { g.hammerCtxCancel() atHammerCtx := pprof.WithLabels(g.terminateCtx, pprof.Labels("graceful-lifecycle", "post-hammer")) pprof.SetGoroutineLabels(atHammerCtx) - for _, fn := range g.toRunAtHammer { - go fn() - } } g.lock.Unlock() } |