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 ¶
- Variables
- func WaitSignal(ctx context.Context) os.Signal
- type App
- func (a *App) Health(ctx context.Context) error
- func (a *App) OnAfterStart(h Hook)
- func (a *App) OnAfterStop(h Hook)
- func (a *App) OnBeforeStart(h Hook)
- func (a *App) OnBeforeStop(h Hook)
- func (a *App) Register(c Component, priority int)
- func (a *App) Run(ctx context.Context) error
- func (a *App) Start(ctx context.Context) error
- func (a *App) State() State
- func (a *App) Stop(ctx context.Context) error
- type Application
- type Component
- type Config
- type FuncComponent
- type HealthChecker
- type Hook
- type Logger
- type Option
- type ServerComponent
- type State
Constants ¶
This section is empty.
Variables ¶
View Source
var (
ErrNotRunning = errors.New("app not running")
)
Functions ¶
Types ¶
type App ¶
type App struct {
// contains filtered or unexported fields
}
App 应用实现
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 应用配置
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
type HealthChecker ¶
HealthChecker 健康检查接口
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) Name ¶
func (c *ServerComponent) Name() string
Click to show internal directories.
Click to hide internal directories.