shutdown

package
v1.20.7 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2025 License: MIT Imports: 10 Imported by: 0

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

View Source
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 Env

type Env struct {
	Key   string
	Value string
}

Env represents an environment variable to be inherited by the new process.

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 New

func New() *Shutdown

New creates a new Shutdown instance with default settings.

func (*Shutdown) AddCustomEnvs

func (s *Shutdown) AddCustomEnvs(envs []Env)

AddCustomEnvs adds custom environment variables to be inherited by the new process.

func (*Shutdown) AddInheritedFiles

func (s *Shutdown) AddInheritedFiles(files []*os.File)

AddInheritedFiles adds files to be inherited by the new process during reboot.

func (*Shutdown) IsListening

func (s *Shutdown) IsListening() bool

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) Logger

func (s *Shutdown) Logger() Logger

Logger returns the current logger.

func (*Shutdown) Reboot

func (s *Shutdown) Reboot(ctx context.Context)

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

func (s *Shutdown) SetLogger(logger Logger)

SetLogger sets the logger for shutdown events. If nil, no logging is performed.

func (*Shutdown) SetTimeout

func (s *Shutdown) SetTimeout(timeout time.Duration)

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.

func (*Shutdown) Shutdown

func (s *Shutdown) Shutdown(ctx context.Context)

Shutdown gracefully shuts down the application. It executes the hooks within the specified timeout and then exits with code 0.

If ctx is provided, it's used instead of the configured timeout. The function will block until shutdown is complete or timeout occurs.

func (*Shutdown) Stop

func (s *Shutdown) Stop()

Stop stops listening for signals.

func (*Shutdown) Timeout

func (s *Shutdown) Timeout() time.Duration

Timeout returns the current shutdown timeout.

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.

Jump to

Keyboard shortcuts

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