Documentation
¶
Index ¶
Constants ¶
const (
// RequestIDContextKey is a context key for request ID.
RequestIDContextKey contextKey = "request_id"
)
Variables ¶
This section is empty.
Functions ¶
func BackgroundWithID ¶
BackgroundWithID returns a new background context with an existing request ID in ctx, if any.
func Cancel ¶
Cancel cancels the base context of the global environment.
Passed err will be returned by Wait(). Once canceled, Go() will not start new goroutines.
Note that calling Cancel(nil) is perfectly valid. Unlike Stop(), Cancel(nil) cancels the base context and can gracefully stop goroutines started by Server.Serve or HTTPServer.ListenAndServe.
This returns true if the caller is the first that calls Cancel. For second and later calls, Cancel does nothing and returns false.
func Go ¶
Go starts a goroutine that executes f in the global environment.
f takes a drived context from the base context. The context will be canceled when f returns.
Goroutines started by this function will be waited for by Wait until all such goroutines return.
If f returns non-nil error, Cancel is called immediately with that error.
f should watch ctx.Done() channel and return quickly when the channel is closed.
func IsSignaled ¶
IsSignaled returns true if err returned by Wait indicates that the program has received SIGINT or SIGTERM.
func Stop ¶
func Stop()
Stop just declares no further Go will be called.
Calling Stop is optional if and only if Cancel is guaranteed to be called at some point. For instance, if the program runs until SIGINT or SIGTERM, Stop is optional.
Types ¶
type Environment ¶
type Environment struct {
// contains filtered or unexported fields
}
Environment implements context-based goroutine management.
func NewEnvironment ¶
func NewEnvironment(ctx context.Context) *Environment
NewEnvironment creates a new Environment.
This does *not* install signal handlers for SIGINT/SIGTERM for new environments. Only the global environment will be canceled on these signals.
func (*Environment) Cancel ¶
func (e *Environment) Cancel(err error) bool
Cancel cancels the base context.
Passed err will be returned by Wait(). Once canceled, Go() will not start new goroutines.
Note that calling Cancel(nil) is perfectly valid. Unlike Stop(), Cancel(nil) cancels the base context and can gracefully stop goroutines started by Server.Serve or HTTPServer.ListenAndServe.
This returns true if the caller is the first that calls Cancel. For second and later calls, Cancel does nothing and returns false.
func (*Environment) Go ¶
func (e *Environment) Go(f func(ctx context.Context) error)
Go starts a goroutine that executes f.
f takes a drived context from the base context. The context will be canceled when f returns.
Goroutines started by this function will be waited for by Wait until all such goroutines return.
If f returns non-nil error, Cancel is called immediately with that error.
f should watch ctx.Done() channel and return quickly when the channel is closed.
func (*Environment) GoWithID ¶
func (e *Environment) GoWithID(f func(ctx context.Context) error)
GoWithID calls Go with a context having a new request tracking ID.
func (*Environment) Stop ¶
func (e *Environment) Stop()
Stop just declares no further Go will be called.
Calling Stop is optional if and only if Cancel is guaranteed to be called at some point. For instance, if the program runs until SIGINT or SIGTERM, Stop is optional.
func (*Environment) Wait ¶
func (e *Environment) Wait() error
Wait waits for Stop or Cancel, and for all goroutines started by Go to finish.
The returned err is the one passed to Cancel, or nil. err can be tested by IsSignaled to determine whether the program got SIGINT or SIGTERM.