Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ErrShutdownTimeout = errors.New("go-l4proxy/l4proxy: shutdown timeout")
ErrShutdownTimeout tells that shutdown has timeout.
Functions ¶
This section is empty.
Types ¶
type ServerRunner ¶
type ServerRunner struct {
// Serve starts a server.
// Serve must blocks until the server closed.
// Serve must not be nil.
Serve func() error
// Shutdown gracefully shutdowns the server.
// Shutdown must not be nil.
// It will be called only when the context given to
// the ServerRunner.Run is done.
// Otherwise, Shutdown is not called even the Server exited.
// Typically [http.Server.Shutdown] should be set.
Shutdown func(context.Context) error
// Close immediately closes a server.
// Unlike Shutdown, it should not block.
// Close, if non-nil, will be called only when shutdown timeout occurred.
// Typically [net/http.Server.Close] should be set.
// Note that the [net/http.Server.Shutdown] does not close remaining
// connection after shutdown timeout occurred but this Runner try to.
Close func() error
// ShutdownTimeout is the shutdown timeout duration.
// ShutdownTimeout = 0 means no timeout.
// ShutdownTimeout < 0 means shutdown immediately.
ShutdownTimeout time.Duration
}
ServerRunner runs a server with an ability of graceful shutdown.
Example:
svr := &http.Server{Addr: ":8080"}
r := &ServerRunner{
Serve: svr.ListenAndServe,
Shutdown: svr.Shutdown,
Close: svr.Close,
ShutdownTimeout: 30 * time.Second,
}
sigCtx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
if err := r.Run(sigCtx); err != nil {
panic(err)
}
func (*ServerRunner) Run ¶
func (r *ServerRunner) Run(sigCtx context.Context) error
Run runs a server. A server will be shutdown when the sigCtx is done. It returns non-nil error if r.Serve returns non-nil error. When a timeout occurred while shutting down, a ErrShutdownTimeout can be returned.
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
tcp-limit-concurrency
command
|
|
|
tcp-proxy
command
|
|
|
tcp-proxy-tls
command
|
|
|
tcp-server-runner
command
|
|
|
tcp-server-simple
command
|
|
|
tcp-server-tls
command
|
|
|
tcp-server-whitelist
command
|
|
|
tcp-socket-abstract
command
|
|
|
tcp-socket-path
command
|
|
|
udp-proxy
command
|
|
|
udp-server-runner
command
|
|
|
udp-server-simple
command
|
|
|
udp-server-whitelist
command
|
|
|
udp-socket-abstract
command
|
|
|
udp-socket-path
command
|
|
Click to show internal directories.
Click to hide internal directories.