server

package
v2.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 25, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package server provides server lifecycle and graceful shutdown helpers.

Use this package to coordinate signal handling, shutdown deadlines, and ordered resource cleanup for HTTP/gRPC service processes.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoServersConfigured = errors.New("no servers configured: use WithHTTPServer() or WithGRPCServer()")

ErrNoServersConfigured indicates no servers were configured for the manager

Functions

This section is empty.

Types

type ServerManager

type ServerManager struct {
	// contains filtered or unexported fields
}

ServerManager handles the graceful shutdown of multiple server types. It can manage HTTP servers, gRPC servers, or both simultaneously.

func NewServerManager

func NewServerManager(
	licenseClient *license.ManagerShutdown,
	telemetry *opentelemetry.Telemetry,
	logger log.Logger,
) *ServerManager

NewServerManager creates a new instance of ServerManager. If logger is nil, a no-op logger is used to ensure nil-safe operation throughout the server lifecycle.

func (*ServerManager) ServersStarted

func (sm *ServerManager) ServersStarted() <-chan struct{}

ServersStarted returns a channel that is closed when server goroutines have been launched. Note: This signals that goroutines were spawned, not that sockets are bound and ready to accept connections. This is useful for tests to coordinate shutdown timing after server launch.

func (*ServerManager) StartWithGracefulShutdown

func (sm *ServerManager) StartWithGracefulShutdown()

StartWithGracefulShutdown initializes all configured servers and sets up graceful shutdown. It terminates the process with os.Exit(1) if no servers are configured (backward compatible behavior). Note: On configuration error, logFatal always terminates the process regardless of logger availability. Use StartWithGracefulShutdownWithError() for proper error handling without process termination.

func (*ServerManager) StartWithGracefulShutdownWithError

func (sm *ServerManager) StartWithGracefulShutdownWithError() error

StartWithGracefulShutdownWithError validates configuration and starts servers. Returns an error if no servers are configured instead of calling Fatal. Blocks until shutdown signal is received or shutdown channel is closed.

func (*ServerManager) WithGRPCServer

func (sm *ServerManager) WithGRPCServer(server *grpc.Server, address string) *ServerManager

WithGRPCServer configures the gRPC server for the ServerManager.

func (*ServerManager) WithHTTPServer

func (sm *ServerManager) WithHTTPServer(app *fiber.App, address string) *ServerManager

WithHTTPServer configures the HTTP server for the ServerManager.

func (*ServerManager) WithShutdownChannel

func (sm *ServerManager) WithShutdownChannel(ch <-chan struct{}) *ServerManager

WithShutdownChannel configures a custom shutdown channel for the ServerManager. This allows tests to trigger shutdown deterministically instead of relying on OS signals.

func (*ServerManager) WithShutdownHook added in v2.3.0

func (sm *ServerManager) WithShutdownHook(hook func(context.Context) error) *ServerManager

WithShutdownHook registers a function to be called during graceful shutdown. Hooks are executed in registration order, AFTER HTTP server shutdown and BEFORE telemetry shutdown. Each hook receives a context bounded by the shutdown timeout. Errors from hooks are logged but do not prevent subsequent hooks or the rest of the shutdown sequence from running (best-effort cleanup).

func (*ServerManager) WithShutdownTimeout

func (sm *ServerManager) WithShutdownTimeout(d time.Duration) *ServerManager

WithShutdownTimeout configures the maximum duration to wait for gRPC GracefulStop before forcing a hard stop. Defaults to 30 seconds.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL