shutdownkit

package
v0.0.0-...-5d01d84 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2025 License: MIT Imports: 6 Imported by: 0

README

ShutdownKit Module

Fx integration for the two-stage shutdown primitives from signals.

Features

  • Provides named contexts for graceful and forced shutdown phases.
  • Shares a single *sync.WaitGroup for managed goroutines.
  • Triggers graceful on Fx stop and escalates to force after a timeout (default 10s).
  • Helper shutdownkit.Go runs background work tied to the shared WaitGroup.
  • Timeout override via shutdownkit.WithTimeout.

Usage

type deps struct {
    fx.In
    Graceful context.Context `name:"graceful"`
    Force    context.Context `name:"force"`
    WG       *sync.WaitGroup
}

app := fx.New(
    shutdownkit.Module(shutdownkit.WithTimeout(5*time.Second)),
    fx.Invoke(func(d deps) {
        shutdownkit.Go(d.WG, func() {
            select {
            case <-d.Graceful.Done():
                // flush buffers, close listeners
            case <-d.Force.Done():
                // deadline expired, abort quickly
            }
        })
    }),
)

app.Run()

Use shutdownkit.Module() together with signals.NewWithSignals if you need both Fx lifecycle coordination and OS signal triggering in the same process.

Documentation

Overview

Package shutdownkit integrates the core logic from the `signals` package with the Uber Fx lifecycle. It provides a two-stage shutdown context and a shared WaitGroup for managing background goroutines.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Go

func Go(wg *sync.WaitGroup, fn func())

Go runs fn in a managed goroutine tied to the shared WaitGroup. Use this for background work that must complete or exit on shutdown.

func Module

func Module(opt ...Option) fx.Option

Module wires a single shutdown coordinator and integrates it with Fx lifecycle. Provides:

  • context.Context `name:"graceful"`
  • context.Context `name:"force"`
  • *sync.WaitGroup

Types

type Option

type Option func(*opts)

Option configures Module.

func WithTimeout

func WithTimeout(d time.Duration) Option

WithTimeout overrides the graceful wait bound during shutdown. If not set or <=0, defaults to 10s. Keep in sync with fx.StopTimeout if used.

Jump to

Keyboard shortcuts

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