Documentation
¶
Overview ¶
Package shutdown provides graceful shutdown and reboot functionality for Go applications.
This package allows applications to gracefully shut down or reboot by handling system signals (SIGINT, SIGTERM, SIGUSR2) and executing cleanup hooks before exiting.
Examples ¶
```go
// Create a shutdown manager
shutdown := shutdown.New()
// Set hooks
shutdown.SetHooks(
func() result.VoidResult {
// First sweep: close connections, stop accepting new requests
return result.OkVoid()
},
func() result.VoidResult {
// Before exiting: final cleanup
return result.OkVoid()
},
)
// Start listening for signals
shutdown.Listen()
// Or manually trigger shutdown
shutdown.Shutdown(context.Background())
```
Index ¶
- Constants
- type CustomEnvs
- type Env
- type InheritedFiles
- type Logger
- type Shutdown
- func (s *Shutdown) AddCustomEnvs(envs []Env)
- func (s *Shutdown) AddInheritedFiles(files []*os.File)
- func (s *Shutdown) IsListening() bool
- func (s *Shutdown) Listen()
- func (s *Shutdown) Logger() Logger
- func (s *Shutdown) Reboot(ctx context.Context)
- func (s *Shutdown) SetHooks(firstSweep, beforeExiting func() result.VoidResult)
- func (s *Shutdown) SetLogger(logger Logger)
- func (s *Shutdown) SetTimeout(timeout time.Duration)
- func (s *Shutdown) Shutdown(ctx context.Context)
- func (s *Shutdown) Stop()
- func (s *Shutdown) Timeout() time.Duration
Constants ¶
const MinShutdownTimeout = 15 * time.Second
MinShutdownTimeout is the default minimum timeout for graceful shutdown.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CustomEnvs ¶
type CustomEnvs struct {
// contains filtered or unexported fields
}
CustomEnvs manages custom environment variables.
type InheritedFiles ¶
type InheritedFiles struct {
// contains filtered or unexported fields
}
InheritedFiles manages files to be inherited by the new process.
type Logger ¶
type Logger interface {
Infof(ctx context.Context, format string, args ...interface{})
Errorf(ctx context.Context, format string, args ...interface{})
}
Logger is an interface for logging shutdown events. If nil, no logging is performed.
type Shutdown ¶
type Shutdown struct {
// contains filtered or unexported fields
}
Shutdown manages graceful shutdown and reboot of the application.
func (*Shutdown) AddCustomEnvs ¶
AddCustomEnvs adds custom environment variables to be inherited by the new process.
func (*Shutdown) AddInheritedFiles ¶
AddInheritedFiles adds files to be inherited by the new process during reboot.
func (*Shutdown) IsListening ¶
IsListening returns whether the shutdown manager is currently listening for signals.
func (*Shutdown) Listen ¶
func (s *Shutdown) Listen()
Listen starts listening for shutdown signals (SIGINT, SIGTERM, SIGUSR2). This method blocks until a signal is received.
On Unix systems:
- SIGINT, SIGTERM: Triggers graceful shutdown
- SIGUSR2: Triggers graceful reboot (if supported)
func (*Shutdown) Reboot ¶
Reboot gracefully reboots the application by starting a new process and then shutting down the current one.
NOTE: Reboot is not supported on Windows. On Windows, this method will log a warning and exit.
func (*Shutdown) SetHooks ¶
func (s *Shutdown) SetHooks(firstSweep, beforeExiting func() result.VoidResult)
SetHooks sets the hooks to be executed during shutdown.
- firstSweep: Executed first (e.g., close connections, stop accepting new requests)
- beforeExiting: Executed before process exits (e.g., final cleanup)
If a hook is nil, it's replaced with a no-op function.
func (*Shutdown) SetLogger ¶
SetLogger sets the logger for shutdown events. If nil, no logging is performed.
func (*Shutdown) SetTimeout ¶
SetTimeout sets the timeout for graceful shutdown. If timeout < 0, it's set to a very large value (effectively infinite). If 0 <= timeout < MinShutdownTimeout, it's set to MinShutdownTimeout.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package inheritnet provides a family of Listen functions that either open a fresh connection or provide an inherited connection from when the process was started.
|
Package inheritnet provides a family of Listen functions that either open a fresh connection or provide an inherited connection from when the process was started. |