runtime

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package runtime 提供应用生命周期管理。

核心功能:

  • 组件启动/停止顺序管理(按优先级)
  • 生命周期钩子(BeforeStart/AfterStart/BeforeStop/AfterStop)
  • 信号处理和优雅关闭

使用示例:

app := runtime.New(cfg, logger)
app.Register(httpServer, 100)
app.Register(grpcServer, 200)
if err := app.Run(ctx); err != nil {
    log.Fatal(err)
}

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotRunning = errors.New("app not running")
)

Functions

func WaitSignal

func WaitSignal(ctx context.Context) os.Signal

WaitSignal 等待中断信号

Types

type App

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

App 应用实现

func New

func New(cfg Config, opts ...Option) *App

New 创建应用

func (*App) Health

func (a *App) Health(ctx context.Context) error

Health 健康检查

func (*App) OnAfterStart

func (a *App) OnAfterStart(h Hook)

OnAfterStart 注册启动后钩子

func (*App) OnAfterStop

func (a *App) OnAfterStop(h Hook)

OnAfterStop 注册停止后钩子

func (*App) OnBeforeStart

func (a *App) OnBeforeStart(h Hook)

OnBeforeStart 注册启动前钩子

func (*App) OnBeforeStop

func (a *App) OnBeforeStop(h Hook)

OnBeforeStop 注册停止前钩子

func (*App) Register

func (a *App) Register(c Component, priority int)

Register 注册组件(priority 越小越先启动,越后停止)

func (*App) Run

func (a *App) Run(ctx context.Context) error

Run 运行应用(阻塞直到收到信号)

func (*App) Start

func (a *App) Start(ctx context.Context) error

Start 启动应用

func (*App) State

func (a *App) State() State

State 获取状态

func (*App) Stop

func (a *App) Stop(ctx context.Context) error

Stop 停止应用

type Application

type Application interface {
	// Run 启动应用并阻塞直到收到关闭信号
	Run(ctx context.Context) error
	// Start 启动应用
	Start(ctx context.Context) error
	// Stop 停止应用
	Stop(ctx context.Context) error
	// State 获取当前状态
	State() State
	// Health 健康检查
	Health(ctx context.Context) error
}

Application 应用接口

type Component

type Component interface {
	Name() string
	Start(ctx context.Context) error
	Stop(ctx context.Context) error
}

Component 组件接口

type Config

type Config struct {
	Name            string        `yaml:"name" mapstructure:"name"`
	ShutdownTimeout time.Duration `yaml:"shutdown_timeout" mapstructure:"shutdown_timeout"`
}

Config 应用配置

func DefaultConfig

func DefaultConfig() Config

DefaultConfig 默认配置

type FuncComponent

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

FuncComponent 函数式组件

func NewFuncComponent

func NewFuncComponent(name string, start, stop func(ctx context.Context) error) *FuncComponent

NewFuncComponent 创建函数式组件

func (*FuncComponent) Name

func (c *FuncComponent) Name() string

func (*FuncComponent) Start

func (c *FuncComponent) Start(ctx context.Context) error

func (*FuncComponent) Stop

func (c *FuncComponent) Stop(ctx context.Context) error

type HealthChecker

type HealthChecker interface {
	Health(ctx context.Context) error
}

HealthChecker 健康检查接口

type Hook

type Hook func(ctx context.Context) error

Hook 生命周期钩子

type Logger

type Logger = logger.Logger

Logger 日志接口

type Option added in v0.0.3

type Option func(*App)

Option 应用选项

func WithLogger added in v0.0.3

func WithLogger(log Logger) Option

WithLogger 设置日志

type ServerComponent

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

ServerComponent 服务器组件(异步启动)

func NewServerComponent

func NewServerComponent(name string, start func() error, stop func(ctx context.Context) error) *ServerComponent

NewServerComponent 创建服务器组件

func (*ServerComponent) Err

func (c *ServerComponent) Err() <-chan error

Err 返回错误通道

func (*ServerComponent) Name

func (c *ServerComponent) Name() string

func (*ServerComponent) Start

func (c *ServerComponent) Start(ctx context.Context) error

func (*ServerComponent) Stop

func (c *ServerComponent) Stop(ctx context.Context) error

type State

type State int

State 应用状态

const (
	StateCreated State = iota
	StateStarting
	StateRunning
	StateStopping
	StateStopped
	StateFailed
)

func (State) String

func (s State) String() string

Jump to

Keyboard shortcuts

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