Help us shape the future of Go! Take the 2026 Go Developer Survey

lifecycle

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package lifecycle provides service lifecycle management for gorp framework. Manages lifecycle of multiple host-managed services in one place. Provides startup/stop helper with priority ordering and lifecycle hooks.

生命周期包提供 gorp 框架的服务生命周期管理能力。 在一个位置统一管理多个被 host 托管的服务生命周期。 提供带优先级排序和生命周期钩子的通用启动/停止 helper。

Package lifecycle provides service lifecycle management for gorp framework.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Manager

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

Manager manages service lifecycle transitions in one place.

Manager 统一管理服务生命周期流转。

中文说明: - 这是 “lifecycle helper 更彻底统一” 的核心实现。 - 统一管理服务的启动、停止、优雅关闭。 - 支持生命周期钩子:OnStarting、OnStarted、OnStopping、OnStopped。 - 可被 Host 或命令层直接使用。

func NewManager

func NewManager() *Manager

NewManager creates a lifecycle manager.

NewManager 创建生命周期管理器。

中文说明: - 初始状态为 StateIdle。 - 默认不注册任何服务,等待 Host 或业务侧逐步填充。

func (*Manager) Register

func (m *Manager) Register(name string, service runtimecontract.Hostable, hooks runtimecontract.Lifecycle, priority int)

Register registers one service into the lifecycle manager.

Register 注册一个由生命周期管理器托管的服务。

中文说明: - priority 数值小的先启动,后停止。 - hooks 可以为 nil,适用于不需要生命周期钩子的服务。

func (*Manager) Services

func (m *Manager) Services() []string

Services returns the names of all registered services.

Services 返回所有已注册服务的名称列表。

中文说明: - 这里只返回注册名,不暴露内部 ServiceEntry 细节。 - 主要供 Host 查询当前托管了哪些服务。

func (*Manager) Start

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

Start starts all registered services in priority order.

Start 按优先级顺序启动所有已注册服务。

中文说明: - 按 priority 从小到大启动服务。 - 触发 OnStarting 和 OnStarted 钩子。 - 如果某个服务启动失败,会回滚已启动的服务。

func (*Manager) State

func (m *Manager) State() State

State returns the current lifecycle state.

State 返回当前生命周期状态。

中文说明: - 这是 Host 和外层运行时观察生命周期进度的读取入口。 - 仅读取当前状态,不改变任何生命周期行为。

func (*Manager) Stop

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

Stop stops all registered services in reverse priority order.

Stop 按优先级逆序停止所有已注册服务。

中文说明: - 按 priority 从大到小停止服务。 - 触发 OnStopping 和 OnStopped 钩子。 - 即使某个服务停止失败,也继续停止其他服务。

type RegistryHook added in v0.2.0

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

RegistryHook implements runtimecontract.Lifecycle to automate Post-Start registration and Pre-Stop deregistration with traffic draining.

func NewRegistryHook added in v0.2.0

func NewRegistryHook(registry transportcontract.ServiceRegistry, opts RegistryHookOptions) *RegistryHook

NewRegistryHook creates a new service discovery lifecycle hook.

func (*RegistryHook) OnStarted added in v0.2.0

func (h *RegistryHook) OnStarted(ctx context.Context) error

OnStarted registers the service to the discovery registry only after the server is listening.

func (*RegistryHook) OnStarting added in v0.2.0

func (h *RegistryHook) OnStarting(ctx context.Context) error

OnStarting is a no-op before service start.

func (*RegistryHook) OnStopped added in v0.2.0

func (h *RegistryHook) OnStopped(ctx context.Context) error

OnStopped is a no-op after service stop.

func (*RegistryHook) OnStopping added in v0.2.0

func (h *RegistryHook) OnStopping(ctx context.Context) error

OnStopping deregisters the service and pauses for the drain window before shutting down the server.

type RegistryHookOptions added in v0.2.0

type RegistryHookOptions struct {
	ServiceName string
	Address     string
	Metadata    map[string]string
	DrainDelay  time.Duration // Time to wait after deregistration to allow in-flight traffic to drain.
}

RegistryHookOptions configures the service registration lifecycle hook.

type ServiceEntry

type ServiceEntry struct {
	Name     string
	Service  runtimecontract.Hostable
	Hooks    runtimecontract.Lifecycle
	Priority int // 启动优先级,数值小的先启动。
}

ServiceEntry describes one registered service entry.

ServiceEntry 表示一个已注册的服务条目。

中文说明: - Name 用于生命周期日志、排障和服务列表展示。 - Service 是真正执行 Start/Stop 的运行对象。 - Hooks 用于在服务前后挂生命周期钩子。 - Priority 决定启动顺序和停止逆序。

type State

type State int

State represents the current lifecycle state.

State 表示当前生命周期状态。

const (
	StateIdle State = iota
	StateStarting
	StateRunning
	StateStopping
	StateStopped
)

func (State) String

func (s State) String() string

String returns the string form of the lifecycle state.

String 返回生命周期状态的字符串表示。

Jump to

Keyboard shortcuts

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