Documentation
¶
Overview ¶
Package host provides service adapters for wrapping HTTP, Cron, and GRPC servers. These adapters implement runtimecontract.Hostable interface for lifecycle management.
本文件提供服务适配器,用于封装 HTTP、Cron 和 GRPC 服务器。 这些适配器实现 runtimecontract.Hostable 接口,用于生命周期管理。
Package host provides the application host service for managing service lifecycle. The host manages startup/shutdown of HTTP servers, Cron jobs, and GRPC servers. Services can be registered with priority for startup ordering.
Host 服务包,提供应用主机服务,管理多个服务的生命周期。 Host 管理 HTTP 服务器、Cron 任务和 GRPC 服务器的启动/关闭。 服务可按优先级注册,控制启动顺序。 Eg:
// 注册 Provider
app.Register(host.NewProvider())
// 注册服务
h := c.MustMake(runtimecontract.HostKey).(runtimecontract.Host)
h.RegisterService("http", httpService)
h.RegisterService("cron", cronService)
// 启动所有服务
h.Start(ctx)
// 关闭所有服务
h.Shutdown(ctx)
Index ¶
- type CronService
- type DefaultHost
- func (h *DefaultHost) RegisterService(name string, service runtimecontract.Hostable) error
- func (h *DefaultHost) RegisterServiceWithPriority(name string, service runtimecontract.Hostable, hooks runtimecontract.Lifecycle, ...) error
- func (h *DefaultHost) Services() []string
- func (h *DefaultHost) Shutdown(ctx context.Context) error
- func (h *DefaultHost) Start(ctx context.Context) error
- func (h *DefaultHost) State() lifecycle.State
- func (h *DefaultHost) Stop(ctx context.Context) error
- type GRPCService
- type HTTPService
- type Provider
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CronService ¶
type CronService struct {
// contains filtered or unexported fields
}
CronService wraps runtimecontract.Cron as a Hostable service.
CronService 封装 runtimecontract.Cron 为可托管服务。
func NewCronService ¶
func NewCronService(name string, c runtimecontract.Cron) *CronService
NewCronService creates a new Cron service adapter.
NewCronService 创建新的 Cron 服务适配器。
func (*CronService) Name ¶
func (s *CronService) Name() string
Name returns the service name.
Name 返回服务名称。
type DefaultHost ¶
type DefaultHost struct {
// contains filtered or unexported fields
}
DefaultHost implements runtimecontract.Host interface.
DefaultHost 实现 runtimecontract.Host 接口,管理多个服务的生命周期。
func NewDefaultHost ¶
func NewDefaultHost(container runtimecontract.Container) *DefaultHost
NewDefaultHost creates a new host instance with the given container.
NewDefaultHost 根据给定容器创建新的主机实例。
func (*DefaultHost) RegisterService ¶
func (h *DefaultHost) RegisterService(name string, service runtimecontract.Hostable) error
RegisterService registers a service with default priority (100).
RegisterService 注册服务,使用默认优先级(100)。
func (*DefaultHost) RegisterServiceWithPriority ¶
func (h *DefaultHost) RegisterServiceWithPriority(name string, service runtimecontract.Hostable, hooks runtimecontract.Lifecycle, priority int) error
RegisterServiceWithPriority registers a service with custom priority and lifecycle hooks.
RegisterServiceWithPriority 注册服务,使用自定义优先级和生命周期钩子。
Lower priority numbers start earlier, higher numbers start later.
优先级数字越小越早启动,数字越大越晚启动。
func (*DefaultHost) Services ¶
func (h *DefaultHost) Services() []string
Services returns all registered service names.
Services 返回所有已注册的服务名称。
func (*DefaultHost) Shutdown ¶
func (h *DefaultHost) Shutdown(ctx context.Context) error
Shutdown gracefully stops all registered services in reverse priority order. Core logic: Check running state, call manager.Stop, update running flag.
Shutdown 按优先级逆序优雅关闭所有已注册的服务。 核心逻辑:检查运行状态,调用 manager.Stop,更新运行标志。
func (*DefaultHost) Start ¶
func (h *DefaultHost) Start(ctx context.Context) error
Start starts all registered services in priority order. Core logic: Check if already running, then call manager.Start.
Start 按优先级顺序启动所有已注册的服务。 核心逻辑:检查是否已运行,然后调用 manager.Start。
func (*DefaultHost) State ¶
func (h *DefaultHost) State() lifecycle.State
State returns the current lifecycle state of the host.
State 返回主机当前的生命周期状态。
type GRPCService ¶
type GRPCService struct {
// contains filtered or unexported fields
}
GRPCService wraps grpc.Server as a Hostable service.
GRPCService 封装 grpc.Server 为可托管服务。
func NewGRPCService ¶
NewGRPCService creates a new GRPC service adapter.
NewGRPCService 创建新的 GRPC 服务适配器。
func (*GRPCService) Name ¶
func (s *GRPCService) Name() string
Name returns the service name.
Name 返回服务名称。
func (*GRPCService) Start ¶
func (s *GRPCService) Start(ctx context.Context) error
Start starts the GRPC server in background. 非 grpc.ErrServerStopped 的错误会通过 slog.Error 记录。
Start 在后台启动 GRPC 服务器。 非 grpc.ErrServerStopped 的错误会通过 slog.Error 记录。
func (*GRPCService) Stop ¶
func (s *GRPCService) Stop(ctx context.Context) error
Stop gracefully stops the GRPC server, with fallback to force stop on timeout. Core logic: Call GracefulStop in goroutine, wait for done or context timeout.
Stop 优雅关闭 GRPC 服务器,超时时强制关闭。 核心逻辑:在 goroutine 中调用 GracefulStop,等待完成或 context 超时。
type HTTPService ¶
type HTTPService struct {
// contains filtered or unexported fields
}
HTTPService wraps transportcontract.HTTP as a Hostable service.
HTTPService 封装 transportcontract.HTTP 为可托管服务。
func NewHTTPService ¶
func NewHTTPService(name string, h transportcontract.HTTP) *HTTPService
NewHTTPService creates a new HTTP service adapter.
NewHTTPService 创建新的 HTTP 服务适配器。
func (*HTTPService) Name ¶
func (s *HTTPService) Name() string
Name returns the service name.
Name 返回服务名称。
type Provider ¶
type Provider struct{}
Provider registers the host service contract.
Provider 注册主机服务契约。
func NewProvider ¶
func NewProvider() *Provider
NewProvider creates a new host provider instance.
NewProvider 创建新的主机 Provider 实例。
func (*Provider) Boot ¶
func (p *Provider) Boot(runtimecontract.Container) error
Boot is a no-op for host provider.
Boot 主机 Provider 无启动逻辑。
func (*Provider) DependsOn ¶
DependsOn returns the keys this provider depends on. Host provider has no dependencies.
DependsOn 返回该 provider 依赖的 key。 Host provider 无依赖。
func (*Provider) IsDefer ¶
IsDefer returns false, host should be initialized immediately for service registration.
IsDefer 返回 false,主机应立即初始化以便服务注册。