shutdownx

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2026 License: MIT Imports: 6 Imported by: 0

README

shutdownx

优雅退出编排工具。提供显式 shutdown hook 管理、LIFO 执行顺序和信号绑定。

Graceful shutdown orchestration. Provides explicit shutdown hook management, LIFO execution order, and signal binding.

Documentation

Overview

Package shutdownx 优雅退出编排工具。提供显式 shutdown hook 管理、LIFO 执行顺序和信号绑定。

Example
package main

import (
	"context"
	"fmt"

	"github.com/ZoneCNH/kernel/shutdownx"
)

func main() {
	mgr := shutdownx.NewManager()

	mgr.Register(shutdownx.HookFunc{
		NameValue: "database",
		Fn: func(ctx context.Context) error {
			fmt.Println("closing database")
			return nil
		},
	})
	mgr.Register(shutdownx.HookFunc{
		NameValue: "http-server",
		Fn: func(ctx context.Context) error {
			fmt.Println("stopping http server")
			return nil
		},
	})

	err := mgr.Shutdown(context.Background())
	fmt.Println("error:", err)

}
Output:
stopping http server
closing database
error: <nil>

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NotifyContext

func NotifyContext(parent context.Context, signals ...os.Signal) (context.Context, context.CancelFunc)

NotifyContext returns a context that is cancelled when any of the specified signals are received. The caller MUST call the returned cancel function to release signal handler resources.

Types

type Hook

type Hook interface {
	Name() string
	Shutdown(ctx context.Context) error
}

Hook is a named shutdown action.

type HookFunc

type HookFunc struct {
	NameValue string
	Fn        func(context.Context) error
}

HookFunc adapts a function into a Hook.

func (HookFunc) Name

func (h HookFunc) Name() string

func (HookFunc) Shutdown

func (h HookFunc) Shutdown(ctx context.Context) error

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager orchestrates shutdown hooks in LIFO (last-registered, first-executed) order.

func NewManager

func NewManager(hooks ...Hook) *Manager

NewManager creates a Manager with the given hooks.

func (*Manager) Hooks

func (m *Manager) Hooks() []Hook

Hooks returns a defensive copy of registered hooks.

func (*Manager) Register

func (m *Manager) Register(hook Hook)

Register adds a hook. Must be called before Shutdown.

func (*Manager) Shutdown

func (m *Manager) Shutdown(ctx context.Context) error

Shutdown executes all hooks in reverse registration order. Respects context deadline/cancellation. Returns aggregated errors.

Jump to

Keyboard shortcuts

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