host

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2026 License: MIT Imports: 11 Imported by: 0

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

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 返回服务名称。

func (*CronService) Start

func (s *CronService) Start(ctx context.Context) error

Start starts the cron scheduler.

Start 启动 Cron 调度器。

func (*CronService) Stop

func (s *CronService) Stop(ctx context.Context) error

Stop stops the cron scheduler and waits for completion. Core logic: Call cron.Stop, wait for Done channel or context timeout.

Stop 停止 Cron 调度器并等待完成。 核心逻辑:调用 cron.Stop,等待 Done channel 或 context 超时。

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 返回主机当前的生命周期状态。

func (*DefaultHost) Stop

func (h *DefaultHost) Stop(ctx context.Context) error

Stop shuts down all registered services.

Stop 关闭所有已注册的服务。

type GRPCService

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

GRPCService wraps grpc.Server as a Hostable service.

GRPCService 封装 grpc.Server 为可托管服务。

func NewGRPCService

func NewGRPCService(name string, server *grpc.Server, lis net.Listener) *GRPCService

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 返回服务名称。

func (*HTTPService) Start

func (s *HTTPService) Start(ctx context.Context) error

Start starts the HTTP server in background. 非 ErrServerClosed 的错误会通过 slog.Error 记录。

Start 在后台启动 HTTP 服务器。 非 ErrServerClosed 的错误会通过 slog.Error 记录。

func (*HTTPService) Stop

func (s *HTTPService) Stop(ctx context.Context) error

Stop shuts down the HTTP server gracefully.

Stop 优雅关闭 HTTP 服务器。

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

Boot is a no-op for host provider.

Boot 主机 Provider 无启动逻辑。

func (*Provider) DependsOn

func (p *Provider) DependsOn() []string

DependsOn returns the keys this provider depends on. Host provider has no dependencies.

DependsOn 返回该 provider 依赖的 key。 Host provider 无依赖。

func (*Provider) IsDefer

func (p *Provider) IsDefer() bool

IsDefer returns false, host should be initialized immediately for service registration.

IsDefer 返回 false,主机应立即初始化以便服务注册。

func (*Provider) Name

func (p *Provider) Name() string

Name returns the provider name "host".

Name 返回 Provider 名称 "host"。

func (*Provider) Provides

func (p *Provider) Provides() []string

Provides returns the host contract key.

Provides 返回主机契约键。

func (*Provider) Register

func (p *Provider) Register(c runtimecontract.Container) error

Register binds the host service factory to the container.

Register 将主机服务工厂绑定到容器。

Jump to

Keyboard shortcuts

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