mux package provides utilities for serving multiple protocol servers (HTTP and gRPC) on different ports with graceful shutdown capabilities.
Overview
The multiplexer allows you to start TCP listeners and serve registered protocol servers, handling graceful shutdown when the context is cancelled. It uses theoklog/run package to manage multiple server lifecycles concurrently.
Key Features
- Multi-protocol support: Serve HTTP and gRPC servers simultaneously
- Graceful shutdown: Coordinated shutdown with configurable grace period
- Concurrent execution: Run multiple servers using run groups
- Error handling: Proper error propagation and logging
API Reference
Serve
ctx: Context for controlling server lifecycle and graceful shutdownopts: Variable number of Option functions for configuration
- Error if server startup or shutdown fails
/home/daytona/workspace/source/server/mux/mux.go:21
Options
WithHTTPTarget
addr: TCP address to listen on (e.g.,:8080orlocalhost:8080)srv: Configured*http.Serverinstance
/home/daytona/workspace/source/server/mux/option.go:13
WithGRPCTarget
addr: TCP address to listen on (e.g.,:9090orlocalhost:9090)srv: Configured*grpc.Serverinstance
/home/daytona/workspace/source/server/mux/option.go:21
WithGracePeriod
d: Duration to wait for graceful shutdown (values less than or equal to 0 use default)
/home/daytona/workspace/source/server/mux/option.go:32
Usage Examples
Basic HTTP Server
Multi-Protocol Server
Multiple HTTP Servers
Graceful Shutdown
The multiplexer handles graceful shutdown automatically when the context is cancelled:- Signal Detection: Context cancellation triggers shutdown sequence
- Grace Period: Servers are given time to complete in-flight requests
- Forced Shutdown: After grace period expires, servers are forcefully stopped
- Error Logging: Shutdown errors are logged for debugging
Implementation Details
Default Configuration
- Grace Period: 10 seconds (
defaultGracePeriod) - Error Handling: Errors are logged with
[ERROR]prefix - Minimum Targets: At least one serve target must be configured
Server Lifecycle
The multiplexer usesoklog/run to manage server lifecycles:
- Startup: TCP listeners are created for each target
- Execution: Each server runs in its own goroutine
- Monitoring: Context cancellation is monitored
- Shutdown: All servers shut down gracefully when triggered
/home/daytona/workspace/source/server/mux/mux.go:41-73
Error Handling
The package handles several error conditions:- No targets configured: Returns error if no serve targets are provided
- Listen failures: Returns error if TCP listener cannot be created
- Serve errors: Logged but don’t necessarily stop other servers
- Shutdown errors: Logged when graceful shutdown fails
Best Practices
- Always use signal context: Handle OS signals for clean shutdown
- Configure appropriate grace period: Consider your request timeouts
- Test shutdown behavior: Ensure in-flight requests complete properly
- Monitor logs: Check for shutdown errors in production
- Set reasonable timeouts: Configure read/write timeouts on HTTP servers
See Also
- SPA Server - Single-page application server utilities