Documentation
¶
Overview ¶
Package lameduck provides coordinated lame-duck behavior for any service implementing this package's Server interface.
By default, lame-duck mode is triggered by receipt of SIGINT or SIGTERM and the default lame-duck period is 3 seconds. Options are provided to alter these (an other) values.
This package is written assuming behavior similar to the standard library's http.Server -- in that its Shutdown and Close methods exhibit behavior matching the lameduck.Server interface -- however, in order to allow other types to be used, a Serve method that returns nil is also needed.
type LameDuckServer struct { // This embedded http.Server provides Shutdown and Close methods // with behavior expected by the lameduck.Server interface. *http.Server } // Serve executes ListenAndServe in a manner compliant with the // lameduck.Server interface. func (s *LameDuckServer) Serve(context.Contxt) error { err := s.Server.ListenAndServe() if err == http.ErrServerClosed { err = nil } return err } // Run will run the receiver's embedded http.Server and provide // lame-duck coverage on receipt of SIGTERM or SIGINT. func (s *LameDuckServer) Run(ctx context.Context) error { return lameduck.Run(ctx, s) }
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Run ¶
Run executes the given Server providing coordinated lame-duck behavior on receipt of one or more configurable signals. By default, the lame-duck period is 3s and is triggered by SIGINT or SIGTERM. Options are available to alter these values.
Note that:
r, err := lameduck.Run(ctx, svr)
...is equivalent to calling:
if r, err := lameduck.NewRunner(svr); err == nil { r.Run(ctx) }
Types ¶
type LameDuckError ¶
LameDuckError is the error type returned by Run for errors related to lame-duck mode.
func (*LameDuckError) Error ¶
func (lde *LameDuckError) Error() string
func (*LameDuckError) Unwrap ¶
func (lde *LameDuckError) Unwrap() error
type Logger ¶
type Logger interface {
Infof(string, ...interface{})
}
Logger is the interface needed for the WithLogger Option.
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option is the interface implemented by types that offer optional behavior while running a Server with lame-duck support.
func Signals ¶
Signals returns an Options that changes the list of Signals that trigger the beginning of lame-duck mode. Using this Option fully replaces the previous list of triggering signals.
func WithLogger ¶
WithLogger returns an Option that alters this package's logging facility to the provided Logger. Note, the default Logger is one derived from 'github.com/golang/glog'. To prevent all logging, use WithoutLogger.
func WithoutLogger ¶
func WithoutLogger() Option
WithoutLogger returns an option the disables all logging from this package.
type Runner ¶ added in v0.5.3
type Runner struct {
// contains filtered or unexported fields
}
Runner is the lame-duck coordinator for a type implementing the Server interface.
func NewRunner ¶ added in v0.5.3
NewRunner returns a lame-duck Runner that providing coordinated lame-duck behavior for the given svr.
See the Run func for details.
func (*Runner) Ready ¶ added in v0.5.3
func (r *Runner) Ready() <-chan struct{}
Ready returns a channel that is closed when the receiver's underlying Server is ready to serve reqeuests.
type Server ¶
type Server interface { // Serve executes the Server. If Serve returns an error, that error will be // returned immediately by Run and no lame-duck coverage will be provided. // Serve(context.Context) error // Shutdown is called by Run (after catching one of the configured signals) // to initiate a graceful shutdown of the Server; this marks the beginning // of lame-duck mode. If Shutdown returns a nil error before the configured // lame-duck period has elapsed, Run will immediately return nil as well. // // The Context provided to Shutdown will have a timeout set to the configured // lame-duck Period. If Shutdown returns context.DeadlineExceeded, Run will // return a LameDuckError with its Expired field set to true and Err set to // the return value from calling Close. // // Any other error returned by Shutdown will be wrapped by a LameDuckError // with its Expired field set to false. Shutdown(context.Context) error // Close is called by Run when Shutdown returns context.DeadlineExceeded and // its return value will be assigned to the Err field of the LameDuckError // returned by Run. Close() error }
Server defines the interface that should be implemented by types intended for lame-duck support. It is expected that these methods exhibit behavior similar to http.Server -- in that a call to Shutdown or Close should cause Serve to return immediately.
However, unlike http.Server's Serve, ListenAndServe, and ListenAndServeTLS methods (which return http.ErrServerClosed in this situation), this Serve method should return a nil error when lame-duck mode is desired.