2019-11-30 22:40:22 +08:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
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) {
|
2022-06-20 18:02:49 +08:00
|
|
|
return deadline, ok
|
2019-11-30 22:40:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2019-12-15 17:51:28 +08:00
|
|
|
func (g *Manager) ShutdownContext() context.Context {
|
2021-05-15 22:22:26 +08:00
|
|
|
return g.shutdownCtx
|
2019-11-30 22:40:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// HammerContext returns a context.Context that is Done at hammer
|
|
|
|
// Callers using this context should ensure that they are registered as a running server
|
|
|
|
// in order that they are waited for.
|
2019-12-15 17:51:28 +08:00
|
|
|
func (g *Manager) HammerContext() context.Context {
|
2021-05-15 22:22:26 +08:00
|
|
|
return g.hammerCtx
|
2019-11-30 22:40:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// TerminateContext returns a context.Context that is Done at terminate
|
|
|
|
// Callers using this context should ensure that they are registered as a terminating server
|
|
|
|
// in order that they are waited for.
|
2019-12-15 17:51:28 +08:00
|
|
|
func (g *Manager) TerminateContext() context.Context {
|
2021-05-15 22:22:26 +08:00
|
|
|
return g.terminateCtx
|
2019-11-30 22:40:22 +08:00
|
|
|
}
|