Documentation
¶
Overview ¶
Package grace runs HTTP servers with graceful shutdown support.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Run ¶
func Run( ctx context.Context, listenAddr string, handler http.Handler, logger *slog.Logger, opts ...Option, ) error
Run creates and runs an HTTP server until ctx is canceled or startup fails.
Parameters:
- ctx controls the server lifetime; cancellation triggers graceful shutdown.
- listenAddr is passed to http.Server.Addr, for example ":8080".
- handler is assigned to http.Server.Handler.
- logger is used for lifecycle logs; if nil, lifecycle logging is disabled.
- opts override individual defaults or replace them through WithOptions.
Run returns an error when startup fails (for example, address already in use) or when graceful shutdown fails.
func RunServer ¶
RunServer starts server with ListenAndServe and manages graceful shutdown.
Parameters:
- ctx controls the server lifetime; cancellation triggers graceful shutdown.
- server is the fully configured http.Server instance to run.
- logger is used for lifecycle logs; if nil, lifecycle logging is disabled.
RunServer uses the default shutdown timeout. It returns an error when startup fails or when graceful shutdown fails.
func SignalContext ¶
func SignalContext(parent context.Context, signals ...os.Signal) (context.Context, context.CancelFunc)
SignalContext returns a copy of parent that is canceled when one of the configured signals is received. The cancellation cause records the signal name, allowing Run and RunServer to include it in shutdown logs.
If no signals are provided, SignalContext listens for the platform defaults. On Unix platforms those are os.Interrupt and syscall.SIGTERM.
The returned stop function unregisters signal delivery and cancels the context. Callers should defer stop after creating the context.
Types ¶
type Option ¶
type Option func(*Options)
Option mutates Options used by Run.
func WithIdleTimeout ¶
WithIdleTimeout overrides the default idle connection timeout.
func WithMaxHeaderValueCount ¶
WithMaxHeaderValueCount overrides the maximum number of request header values.
func WithOptions ¶
WithOptions replaces all server options used by Run.
Prefer the focused With... options when overriding only individual settings.
func WithReadHeaderTimeout ¶
WithReadHeaderTimeout overrides the default request-header read timeout.
func WithShutdownTimeout ¶
WithShutdownTimeout overrides the default graceful shutdown timeout.
func WithWriteTimeout ¶
WithWriteTimeout overrides the default response write timeout.
type Options ¶
type Options struct {
// ReadHeaderTimeout limits how long to read request headers.
ReadHeaderTimeout time.Duration
// WriteTimeout limits the time spent writing the response.
WriteTimeout time.Duration
// IdleTimeout sets how long to keep idle connections open.
IdleTimeout time.Duration
// MaxHeaderValueCount limits the number of request header values accepted.
MaxHeaderValueCount int
// ShutdownTimeout bounds how long to wait for graceful shutdown.
ShutdownTimeout time.Duration
}
Options configures the HTTP server settings applied by Run.