Documentation
¶
Overview ¶
Package lifecycle 提供生命周期管理功能,用于 enhance 框架。
该模块管理应用和组件的生命周期回调,包括初始化、启动、停止等阶段。 参考 Spring 的 Lifecycle 接口设计。
架构设计 ¶
- ApplicationPhase: 应用生命周期阶段枚举
- PhaseListener: 生命周期阶段监听器接口
- LifecycleManager: 生命周期管理器
- LifecycleBuilder: 生命周期构建器,支持链式配置
- PhaseTransition: 阶段转换辅助结构
- BeanInitFunc: Bean 初始化钩子函数类型
- BeanDestroyFunc: Bean 销毁钩子函数类型
核心功能 ¶
- 阶段管理: 支持 INIT → RUNNING → STOPPED 三阶段转换
- 监听器: 支持注册阶段变更监听器
- Bean 生命周期: 支持 Bean 初始化和销毁钩子
- 错误处理: 支持自定义错误处理回调
使用方式 ¶
实现 PhaseListener 接口:
type MyListener struct{}
func (m *MyListener) OnPhaseChange(oldPhase, newPhase lifecycle.ApplicationPhase) error {
fmt.Printf("Phase changed: %s -> %s\n", oldPhase, newPhase)
return nil
}
注册到生命周期管理器:
manager := lifecycle.NewLifecycleManager()
manager.AddListener(&MyListener{})
manager.SetPhase(lifecycle.PhaseRunning)
执行顺序 ¶
阶段转换必须是正向的:INIT → RUNNING → STOPPED。 反向转换会返回错误。
Index ¶
- func RegisterHook(hook Hook)
- func RegisterHookFunc(onInit, onStart, onStop func(context.Context) error)
- type ApplicationPhase
- type BeanDestroyFunc
- type BeanInitFunc
- type Hook
- type HookFunc
- type HookRegistry
- func (r *HookRegistry) Count() int
- func (r *HookRegistry) GetAll() []Hook
- func (r *HookRegistry) InitAll(ctx context.Context) error
- func (r *HookRegistry) Register(hook Hook)
- func (r *HookRegistry) RegisterFunc(onInit, onStart, onStop func(context.Context) error)
- func (r *HookRegistry) StartAll(ctx context.Context) error
- func (r *HookRegistry) StopAll(ctx context.Context) error
- type LifecycleBuilder
- func (b *LifecycleBuilder) Build() *LifecycleManager
- func (b *LifecycleBuilder) InitialPhase(phase ApplicationPhase) *LifecycleBuilder
- func (b *LifecycleBuilder) Listener(listener PhaseListener) *LifecycleBuilder
- func (b *LifecycleBuilder) OnError(handler func(oldPhase, newPhase ApplicationPhase, err error)) *LifecycleBuilder
- type LifecycleManager
- type PhaseListener
- type PhaseTransition
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RegisterHookFunc ¶
RegisterHookFunc 通过函数注册到全局钩子注册表
Types ¶
type ApplicationPhase ¶
type ApplicationPhase int
ApplicationPhase 应用生命周期阶段。
简化为 3 阶段:PhaseInit → PhaseRunning → PhaseStopped
const ( PhaseInit ApplicationPhase = iota // 初始化阶段:创建容器、加载配置、注册 Bean PhaseRunning // 运行阶段:应用正常运行,处理请求 PhaseStopped // 已停止:应用完全停止 )
应用生命周期阶段定义。
type BeanDestroyFunc ¶
BeanDestroyFunc Bean 销毁钩子函数类型。
在 Bean 销毁前调用,替代 Spring 风格的 DisposableBean 接口。 使用函数类型而非接口,更符合 Go 惯用法。
type BeanInitFunc ¶
BeanInitFunc Bean 初始化钩子函数类型。
在 Bean 创建后调用,替代 Spring 风格的 InitializingBean 接口。 使用函数类型而非接口,更符合 Go 惯用法。
type Hook ¶
type Hook interface {
OnInit(ctx context.Context) error
OnStart(ctx context.Context) error
OnStop(ctx context.Context) error
}
Hook 生命周期钩子接口。
func NewHookFunc ¶
NewHookFunc 创建函数式钩子
func OnStartFunc ¶
OnStartFunc 创建仅实现 OnStart 的钩子
type HookRegistry ¶
type HookRegistry struct {
// contains filtered or unexported fields
}
HookRegistry 钩子注册表
管理多个 Hook,按注册顺序执行 OnInit/OnStart,逆序执行 OnStop。 使用 RWMutex 优化读多写少场景,读操作无写锁竞争。
func (*HookRegistry) InitAll ¶
func (r *HookRegistry) InitAll(ctx context.Context) error
InitAll 按注册顺序执行所有 OnInit
func (*HookRegistry) RegisterFunc ¶
func (r *HookRegistry) RegisterFunc(onInit, onStart, onStop func(context.Context) error)
RegisterFunc 通过函数注册钩子
type LifecycleBuilder ¶
type LifecycleBuilder struct {
// contains filtered or unexported fields
}
LifecycleBuilder 生命周期构建器,支持链式配置。
func NewLifecycleBuilder ¶
func NewLifecycleBuilder() *LifecycleBuilder
NewLifecycleBuilder 创建生命周期构建器。
func (*LifecycleBuilder) Build ¶
func (b *LifecycleBuilder) Build() *LifecycleManager
Build 构建生命周期管理器
func (*LifecycleBuilder) InitialPhase ¶
func (b *LifecycleBuilder) InitialPhase(phase ApplicationPhase) *LifecycleBuilder
InitialPhase 设置初始阶段
func (*LifecycleBuilder) Listener ¶
func (b *LifecycleBuilder) Listener(listener PhaseListener) *LifecycleBuilder
Listener 添加监听器
func (*LifecycleBuilder) OnError ¶
func (b *LifecycleBuilder) OnError(handler func(oldPhase, newPhase ApplicationPhase, err error)) *LifecycleBuilder
OnError 设置错误处理回调
type LifecycleManager ¶
type LifecycleManager struct {
// contains filtered or unexported fields
}
LifecycleManager 生命周期管理器。
func NewLifecycleManager ¶
func NewLifecycleManager() *LifecycleManager
NewLifecycleManager 创建生命周期管理器。
func (*LifecycleManager) AddListener ¶
func (m *LifecycleManager) AddListener(listener PhaseListener)
AddListener 添加生命周期阶段监听器
func (*LifecycleManager) GetPhase ¶
func (m *LifecycleManager) GetPhase() ApplicationPhase
GetPhase 返回当前生命周期阶段
func (*LifecycleManager) SetErrorHandler ¶
func (m *LifecycleManager) SetErrorHandler(handler func(oldPhase, newPhase ApplicationPhase, err error))
SetErrorHandler 设置错误处理回调
func (*LifecycleManager) SetPhase ¶
func (m *LifecycleManager) SetPhase(newPhase ApplicationPhase) error
SetPhase 设置新的生命周期阶段并通知所有监听器
type PhaseListener ¶
type PhaseListener interface {
// OnPhaseChange 处理阶段变更事件。
OnPhaseChange(oldPhase, newPhase ApplicationPhase) error
}
PhaseListener 生命周期阶段监听器。
type PhaseTransition ¶
type PhaseTransition struct {
OldPhase ApplicationPhase
NewPhase ApplicationPhase
}
PhaseTransition 阶段转换辅助结构。