aboutsummaryrefslogtreecommitdiffstats
path: root/modules/graceful
diff options
context:
space:
mode:
authorwxiaoguang <wxiaoguang@gmail.com>2023-05-26 15:31:55 +0800
committerGitHub <noreply@github.com>2023-05-26 07:31:55 +0000
commit18f26cfbf7f9b36b838c0e8762bfba98c89b9797 (patch)
treef3bcf0ef2b76601980ab6144e9ce9acd5316a0da /modules/graceful
parente4922d484b9ee94ba22e5ff08b3c25c8be09d9c8 (diff)
downloadgitea-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')
-rw-r--r--modules/graceful/context.go43
-rw-r--r--modules/graceful/manager.go62
-rw-r--r--modules/graceful/manager_unix.go1
-rw-r--r--modules/graceful/manager_windows.go1
4 files changed, 11 insertions, 96 deletions
diff --git a/modules/graceful/context.go b/modules/graceful/context.go
index 6b5b207ff3..4fcbcb04b6 100644
--- a/modules/graceful/context.go
+++ b/modules/graceful/context.go
@@ -5,51 +5,8 @@ package graceful
import (
"context"
- "time"
)
-// ChannelContext is a context that wraps a channel and error as a context
-type ChannelContext struct {
- done <-chan struct{}
- err error
-}
-
-// NewChannelContext creates a ChannelContext from a channel and error
-func NewChannelContext(done <-chan struct{}, err error) *ChannelContext {
- return &ChannelContext{
- done: done,
- err: err,
- }
-}
-
-// Deadline returns the time when work done on behalf of this context
-// should be canceled. There is no Deadline for a ChannelContext
-func (ctx *ChannelContext) Deadline() (deadline time.Time, ok bool) {
- return deadline, ok
-}
-
-// Done returns the channel provided at the creation of this context.
-// When closed, work done on behalf of this context should be canceled.
-func (ctx *ChannelContext) Done() <-chan struct{} {
- return ctx.done
-}
-
-// Err returns nil, if Done is not closed. If Done is closed,
-// Err returns the error provided at the creation of this context
-func (ctx *ChannelContext) Err() error {
- select {
- case <-ctx.done:
- return ctx.err
- default:
- return nil
- }
-}
-
-// Value returns nil for all calls as no values are or can be associated with this context
-func (ctx *ChannelContext) Value(key interface{}) interface{} {
- return nil
-}
-
// ShutdownContext returns a context.Context that is Done at shutdown
// Callers using this context should ensure that they are registered as a running server
// in order that they are waited for.
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()
}
diff --git a/modules/graceful/manager_unix.go b/modules/graceful/manager_unix.go
index f949abfd21..b1fd6da76d 100644
--- a/modules/graceful/manager_unix.go
+++ b/modules/graceful/manager_unix.go
@@ -41,7 +41,6 @@ type Manager struct {
terminateWaitGroup sync.WaitGroup
toRunAtShutdown []func()
- toRunAtHammer []func()
toRunAtTerminate []func()
}
diff --git a/modules/graceful/manager_windows.go b/modules/graceful/manager_windows.go
index f3606084aa..f676f86d04 100644
--- a/modules/graceful/manager_windows.go
+++ b/modules/graceful/manager_windows.go
@@ -50,7 +50,6 @@ type Manager struct {
shutdownRequested chan struct{}
toRunAtShutdown []func()
- toRunAtHammer []func()
toRunAtTerminate []func()
}