shutdown

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2025 License: MIT Imports: 7 Imported by: 2

README

GoDoc

shutdown

A lightweight Go library that provides utilities for graceful application shutdown handling, particularly useful for long-running services and daemons.

Features

  • Signal Handling: Automatically handles SIGINT (Ctrl-C) and SIGTERM signals
  • Graceful Shutdown: Executes registered cleanup functions in LIFO order
  • Error Management: Propagate fatal errors that should trigger application shutdown
  • Concurrency Support: Run background goroutines that can trigger shutdown on error

Installation

go get github.com/KarpelesLab/shutdown

Usage

import (
    "net"
    "log/slog"

    "github.com/KarpelesLab/shutdown"
)

func main() {
    // Setup signal handlers early in your application
    shutdown.SetupSignals()

    // Run your application components
    go launchHttp()
    
    // Example of running multiple background tasks with error handling
    shutdown.Go(
        task1,
        task2,
        // More tasks...
    )

    // Wait for shutdown (this should be the last call in main)
    shutdown.Wait()
}

func launchHttp() {
    l, err := net.Listen("tcp", ":8080")
    if err != nil {
        // Report fatal errors that should trigger shutdown
        shutdown.Fatalf("failed to listen for the http server: %w", err)
        return
    }

    // Register cleanup functions to be executed during shutdown
    shutdown.Defer(func() {
        slog.Info("Closing HTTP listener")
        l.Close()
    })
    
    // Your HTTP server implementation...
}

// Example task function that returns an error
func task1() error {
    // If this returns an error, it will trigger shutdown
    return nil
}

Core Functions

  • SetupSignals(): Configures signal handlers for graceful shutdown (call early in main)
  • Defer(func()): Registers cleanup functions to be executed during shutdown (LIFO order)
  • Fatalf(format, args...): Reports a fatal error that triggers shutdown
  • Go(funcs ...func() error): Runs functions in background goroutines, errors trigger shutdown
  • Shutdown(): Manually triggers the shutdown process
  • Wait(): Blocks until shutdown is triggered, then runs cleanup functions (call last in main)

Shutdown Triggers

Shutdown can be triggered in several ways:

  1. OS Signals (SIGINT/Ctrl-C or SIGTERM)
  2. Fatal errors reported via Fatalf()
  3. Errors returned from functions started with Go()
  4. Manual trigger via Shutdown()

When shutdown is triggered, all functions registered with Defer() are executed in LIFO order (last registered, first executed).

License

See LICENSE file.

Documentation

Overview

Package shutdown provides utilities for graceful application shutdown handling.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Defer

func Defer(f func())

Defer registers the given function to be run upon program termination. Functions registered with Defer will be executed in LIFO (Last In, First Out) order when shutdown is triggered.

func Fatalf

func Fatalf(format string, args ...any)

Fatalf reports a fatal error that will trigger shutdown. It accepts format string with arguments similar to fmt.Errorf. It is possible to wrap errors using %w formatting directive.

func Go added in v1.0.4

func Go(funcs ...func() error)

Go runs the provided function(s) in background goroutines. If any of these functions return a non-nil error, it will be handled as a fatal error and cause shutdown to trigger as if Fatalf was called. This is useful for starting background tasks that should cause the application to shut down gracefully if they encounter an error.

func RunDefer added in v1.1.1

func RunDefer()

RunDefer executes the functions that were registered with Defer in reverse order. You should never call this directly (the proper way is to call shutdown.Wait() in your func main()), but this may be needed in some edge cases such as when performing online updates, allowing for shutdown to happen without exitting the program when performing updates, for example.

func SetupSignals

func SetupSignals()

SetupSignals sets up signal handlers for graceful shutdown. It listens for interruptions (Ctrl-C/SIGINT or SIGTERM), and triggers a shutdown when a signal is received. If a second signal is sent (indicating shutdown is taking too long), this will force termination by calling os.Exit(1). This function should be called early in your application's main function.

func Shutdown

func Shutdown()

Shutdown will trigger the normal shutdown of the program. It can be called multiple times safely as it uses sync.Once internally.

func Wait

func Wait()

Wait blocks until shutdown is triggered, either by calling Shutdown(), receiving a termination signal (if SetupSignals was called), or if an error is sent via Fatalf. After the shutdown is triggered, this function runs all cleanup functions registered with Defer(). This function should be called as the last function in main().

Types

This section is empty.

Jump to

Keyboard shortcuts

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