shutdown

package
v0.0.0-...-b68e1bd Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2024 License: Unlicense Imports: 1 Imported by: 0

README

实现一个优雅的关闭功能

即在应用程序关闭时,能够优雅的处理正在执行的任务和连接,而不是突然中断它们

代码实现集中在shutdown.go

  1. ShutdownCallback 接口和 ShutdownFunc 类型:用于定义和实现 ShutdownCallbacks,即在关闭时需要执行的回调函数。

  2. ShutdownManager 接口:定义了 ShutdownManager 的行为,包括获取名称、启动 ShutdownManager、开始关闭、结束关闭等。

  3. ErrorHandler 接口和 ErrorFunc 类型:用于定义和实现处理异步错误的函数。

  4. GSInterface 接口:定义了 GracefulShutdown 结构体的行为,包括启动 ShutdownManager、报告错误、添加 ShutdownCallback 等。

  5. GracefulShutdown 结构体:实现了上述接口和类型,并提供了 Start、AddShutdownManager、AddShutdownCallback、SetErrorHandler、StartShutdown、ReportError 等方法,用于启动和管理 ShutdownManager 和 ShutdownCallbacks。

posixsignal.go中,实现了ShutdownManager 接口,使用POSIX信号实现优雅关闭

通过通道sign,接收操作系统的信号,从而调用**gs.StartShutdown(psm)**启动优雅关闭

    sigs := make(chan os.Signal, 1)
    signal.Notify(sigs, syscall.SIGNAL...)

Documentation

Overview

Package shutdown learn from giuhub.com/marmotedu/iam/pkg/shutdown

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrorFunc

type ErrorFunc func(err error)

func (ErrorFunc) OnError

func (f ErrorFunc) OnError(err error)

OnError 定义发生错误时运行所需的操作

type ErrorHandler

type ErrorHandler interface {
	OnError(err error)
}

ErrorHandler 接口和 ErrorFunc 类型:用于定义和实现处理异步错误的函数

type GSInterface

type GSInterface interface {
	StartShutdown(sm ShutdownManager)
	ReportError(err error)
	AddShutdownCallback(shutdownCallback ShutdownCallback)
}

GSInterface 接口:定义了 GracefulShutdown 结构体的行为,包括启动 ShutdownManager、报告错误、添加 ShutdownCallback 等

type GracefulShutdown

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

GracefulShutdown 结构体:实现了上述接口和类型,并提供了 Start、AddShutdownManager、AddShutdownCallback、 SetErrorHandler、StartShutdown、ReportError 等方法, 用于启动和管理 ShutdownManager 和 ShutdownCallbacks

func New

func New() *GracefulShutdown

New 初始化GracefulShutdown

func (*GracefulShutdown) AddShutdownCallback

func (gs *GracefulShutdown) AddShutdownCallback(shutdownCallback ShutdownCallback)

func (*GracefulShutdown) AddShutdownManager

func (gs *GracefulShutdown) AddShutdownManager(manager ShutdownManager)

AddShutdownManager 添加shutdown管理器()

func (*GracefulShutdown) ReportError

func (gs *GracefulShutdown) ReportError(err error)

ReportError是一个可以用来向ErrorHandler报告错误的函数。它在ShutdownManagers中使用。

func (*GracefulShutdown) SetErrorHandler

func (gs *GracefulShutdown) SetErrorHandler(errorHandler ErrorHandler)

func (*GracefulShutdown) Start

func (gs *GracefulShutdown) Start() error

func (*GracefulShutdown) StartShutdown

func (gs *GracefulShutdown) StartShutdown(sm ShutdownManager)

StartShutdown从ShutdownManager调用,将启动shutdown。 首先在Shutdownmanager上调用ShutdownStart, 调用所有的ShutdownCallbacks,等待回调完成, 并在ShutdownManager上调用ShutdownFinish。

type ShutdownCallback

type ShutdownCallback interface {
	OnShutdown(string) error
}

ShutdownCallback 提供了shutdown 前后需要执行的回调函数

type ShutdownFunc

type ShutdownFunc func(string) error

ShutdownFunc是一个helper类型,所以你可以很容易地提供匿名函数ShutdownCallbacks。

func (ShutdownFunc) OnShutdown

func (f ShutdownFunc) OnShutdown(shutdownManager string) error

OnShutdown 定义触发关机时需要运行的操作。

type ShutdownManager

type ShutdownManager interface {
	GetName() string
	Start(gs GSInterface) error
	ShutdownStart() error
	ShutdownFinish() error
}

ShutdownManager 接口:定义了 ShutdownManager 的行为,包括获取名称、启动 ShutdownManager、开始关闭、结束关闭等

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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