runtime

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: 7 Imported by: 0

Documentation

Overview

Application scenarios: - Define minimal business-facing runtime contracts shared across framework layers. - Expose the smallest unit-of-work abstraction without binding to a concrete ORM or transaction engine. - Keep application service semantics lightweight and portable.

适用场景: - 定义框架各层共享的最小业务运行时契约。 - 在不绑定具体 ORM 或事务引擎的情况下暴露最小工作单元抽象。 - 让应用服务语义保持轻量和可移植。

Application scenarios: - Define the minimal runtime container contract used across framework packages. - Keep dependency resolution, provider registration, and singleton binding semantics stable. - Let different container implementations conform to one shared runtime abstraction.

适用场景: - 定义框架各包共享的最小运行时容器契约。 - 稳定维护依赖解析、provider 注册和单例绑定语义。 - 让不同容器实现都能遵循同一套运行时抽象。

Application scenarios: - Define the runtime cron scheduling contract used by bootstrap and providers. - Support both anonymous and named jobs with start/stop lifecycle control. - Keep cron execution context-aware for framework-managed background tasks. - Expose job introspection for diagnostic endpoints (e.g. /debug/cron).

适用场景: - 定义 bootstrap 和 provider 使用的运行时 cron 调度契约。 - 支持匿名任务和具名任务,并提供启动/停止生命周期控制。 - 让框架管理的后台任务具备 context 感知能力。 - 暴露任务内省能力,供诊断端点(如 /debug/cron)使用。

Application scenarios: - Define sentinel errors for container operations. - Provide structured error types for circular dependency detection and container lifecycle.

适用场景: - 定义容器操作的哨兵错误。 - 为循环依赖检测和容器生命周期提供结构化错误类型。

Application scenarios: - Define host and root-path contracts used by framework runtime assembly. - Provide a shared lifecycle model for long-running services managed by the host. - Keep service hosting abstractions independent from concrete server implementations.

适用场景: - 定义框架运行时装配使用的 host 与根路径契约。 - 为 host 管理的长生命周期服务提供共享生命周期模型。 - 让服务托管抽象不依赖具体服务器实现。

Application scenarios: - Define the provider contract used by the runtime container and bootstrap flows. - Keep provider registration, boot order, and deferred loading semantics explicit. - Offer one stable provider abstraction across all framework capabilities.

适用场景: - 定义运行时容器与 bootstrap 流程使用的 provider 契约。 - 显式表达 provider 的注册、启动顺序和延迟加载语义。 - 为所有框架能力提供统一稳定的 provider 抽象。

Application scenarios: - Define runtime configurator contracts used during bootstrap assembly. - Let features inject HTTP, cron, and gRPC runtime behavior without hard-coding one implementation. - Keep configurator interfaces minimal and composition-friendly.

适用场景: - 定义 bootstrap 装配阶段使用的运行时配置器契约。 - 让特性模块在不写死实现的前提下注入 HTTP、cron 和 gRPC 运行时行为。 - 保持配置器接口最小化且易于组合。

Index

Constants

View Source
const (
	RootKey = "framework.root"
	HostKey = "framework.host"
)
View Source
const (
	HTTPRuntimeConfiguratorKey = "app.runtime.http_configurator"
	CronRuntimeConfiguratorKey = "app.runtime.cron_configurator"
	GRPCRuntimeBuilderKey      = "app.runtime.grpc_builder"
)
View Source
const ContainerKey = "framework.container"

ContainerKey is the container self-binding key.

ContainerKey 是容器自绑定时使用的 key。

View Source
const CronKey = "framework.cron"

CronKey is the container key for the cron capability.

CronKey 是 cron 能力的容器键。

Variables

View Source
var ErrCircularDependency = errors.New("circular dependency detected")

ErrCircularDependency is returned when a circular dependency is detected during service resolution.

ErrCircularDependency 在服务解析期间检测到循环依赖时返回。

View Source
var ErrContainerDestroyed = errors.New("container is destroyed")

ErrContainerDestroyed is returned when Make/MakeNamed is called after Destroy.

ErrContainerDestroyed 在 Destroy 之后调用 Make/MakeNamed 时返回。

Functions

This section is empty.

Types

type AppService

type AppService interface{}

AppService marks a framework-level business service.

AppService 标记框架层业务服务。

type CircularDependencyError

type CircularDependencyError struct {
	Key   string   // the key that triggered the cycle
	Chain []string // the full resolution chain, e.g. ["A", "B", "A"]
}

CircularDependencyError provides details about a circular dependency chain.

CircularDependencyError 提供循环依赖链的详细信息。

func (*CircularDependencyError) Error

func (e *CircularDependencyError) Error() string

func (*CircularDependencyError) Is

func (e *CircularDependencyError) Is(target error) bool

Is allows errors.Is(err, ErrCircularDependency) to match CircularDependencyError.

Is 使得 errors.Is(err, ErrCircularDependency) 可以匹配 CircularDependencyError。

func (*CircularDependencyError) Unwrap

func (e *CircularDependencyError) Unwrap() error

type Container

type Container interface {
	// Bind registers a factory under a key.
	// If the key is already bound, a warning is logged and the previous binding is replaced.
	//
	// Bind 将 factory 绑定到目标 key。
	// 如果 key 已绑定,打印警告日志并替换原有绑定。
	Bind(key string, factory Factory, singleton bool)

	// NamedBind registers a named factory under a key.
	// Allows multiple implementations of the same key to coexist under different names.
	//
	// NamedBind 将命名 factory 绑定到目标 key。
	// 允许同一 key 的多个实现以不同名称共存。
	NamedBind(name, key string, factory Factory, singleton bool)

	// IsBind reports whether a key is bound.
	//
	// IsBind 返回指定 key 是否已绑定。
	IsBind(key string) bool

	// IsBindNamed reports whether a named binding exists.
	//
	// IsBindNamed 返回指定命名绑定是否存在。
	IsBindNamed(name, key string) bool

	// Make resolves a service by key.
	//
	// Make 按 key 解析服务。
	Make(key string) (any, error)

	// MakeNamed resolves a named service by name and key.
	//
	// MakeNamed 按名称和 key 解析命名服务。
	MakeNamed(name, key string) (any, error)

	// MustMake resolves a service by key and panics on error.
	//
	// MustMake 按 key 解析服务,失败时 panic。
	MustMake(key string) any

	// MustMakeNamed resolves a named service by name and key and panics on error.
	//
	// MustMakeNamed 按名称和 key 解析命名服务,失败时 panic。
	MustMakeNamed(name, key string) any

	// RegisterCloser registers an io.Closer to be called during Destroy.
	// Closers are called in reverse registration order.
	//
	// RegisterCloser 注册一个 io.Closer,在 Destroy 时调用。
	// Closer 按注册逆序调用。
	RegisterCloser(key string, closer io.Closer)

	// Destroy calls all registered closers in reverse order and marks the container as destroyed.
	// After Destroy, Make/MakeNamed return ErrContainerDestroyed.
	//
	// Destroy 按注册逆序调用所有 Closer,并将容器标记为已销毁。
	// 销毁后 Make/MakeNamed 返回 ErrContainerDestroyed。
	Destroy() error

	// RegisterProvider registers one provider into the container.
	//
	// RegisterProvider 将单个 provider 注册进容器。
	RegisterProvider(p ServiceProvider) error

	// RegisterProviders registers multiple providers into the container.
	//
	// RegisterProviders 将多个 provider 注册进容器。
	RegisterProviders(providers ...ServiceProvider) error

	// RegisteredProviders returns the names of all registered providers with their load/boot status.
	//
	// RegisteredProviders 返回所有已注册 provider 的名称及其加载/引导状态。
	RegisteredProviders() []ProviderInfo

	// DebugPrint returns a human-readable snapshot of the container state for diagnostics.
	//
	// DebugPrint 返回容器状态的人类可读快照,用于诊断。
	DebugPrint() string

	// ProviderDAG returns the provider dependency graph for visualization and analysis.
	// Useful for debugging provider load order and detecting circular dependencies.
	//
	// ProviderDAG 返回 provider 依赖图,用于可视化和分析。
	// 用于调试 provider 加载顺序和检测循环依赖。
	ProviderDAG() ProviderDAG
}

Container defines the runtime dependency injection surface.

Container 定义运行时依赖注入接口面。

type Cron

type Cron interface {
	// Add registers a cron job.
	//
	// Add 注册一个 cron 任务。
	Add(spec string, fn func(ctx context.Context) error) (entryID int, err error)

	// AddNamed registers a named cron job.
	//
	// AddNamed 注册一个具名 cron 任务。
	AddNamed(name, spec string, fn func(ctx context.Context) error) (entryID int, err error)

	// Start starts the cron scheduler.
	//
	// Start 启动 cron 调度器。
	Start()

	// Stop stops the cron scheduler and returns a context for shutdown coordination.
	//
	// Stop 停止 cron 调度器,并返回一个用于关闭协调的 context。
	Stop() context.Context

	// Jobs returns information about all registered cron jobs, including
	// schedule, last/next run time, and execution status.
	//
	// Jobs 返回所有已注册 cron 任务的信息,包括调度表达式、上次/下次执行时间及执行状态。
	Jobs() []CronJobEntry
}

Cron defines the runtime cron scheduling capability.

Cron 定义运行时 cron 调度能力。

type CronJobEntry

type CronJobEntry struct {
	// ID is the unique entry identifier assigned by the scheduler.
	//
	// ID 是调度器分配的唯一任务标识。
	ID int `json:"id"`

	// Name is the human-readable job name. Empty for anonymous jobs.
	//
	// Name 是任务的可读名称。匿名任务为空。
	Name string `json:"name"`

	// Spec is the cron expression (e.g. "0 */5 * * * *").
	//
	// Spec 是 cron 表达式(如 "0 */5 * * * *")。
	Spec string `json:"spec"`

	// Status is the last known execution status.
	//
	// Status 是最近一次执行的状态。
	Status CronJobStatus `json:"status"`

	// LastRunTime is the time of the last execution, zero if never run.
	//
	// LastRunTime 是上次执行时间,从未执行则为零值。
	LastRunTime time.Time `json:"last_run_time,omitempty"`

	// NextRunTime is the time of the next scheduled execution, zero if stopped.
	//
	// NextRunTime 是下次计划执行时间,已停止则为零值。
	NextRunTime time.Time `json:"next_run_time,omitempty"`

	// LastError is the error from the last execution, nil if succeeded or never run.
	//
	// LastError 是上次执行的错误,成功或从未执行则为 nil。
	LastError string `json:"last_error,omitempty"`

	// LastDuration is the duration of the last execution, zero if never run.
	//
	// LastDuration 是上次执行的耗时,从未执行则为零值。
	LastDuration time.Duration `json:"last_duration,omitempty"`
}

CronJobEntry describes a registered cron job and its recent execution state.

CronJobEntry 描述一个已注册的 cron 任务及其最近执行状态。

type CronJobStatus

type CronJobStatus string

CronJobStatus represents the execution status of a cron job.

CronJobStatus 表示 cron 任务的执行状态。

const (
	// CronJobStatusSuccess indicates the last execution succeeded.
	//
	// CronJobStatusSuccess 表示上次执行成功。
	CronJobStatusSuccess CronJobStatus = "success"

	// CronJobStatusError indicates the last execution failed.
	//
	// CronJobStatusError 表示上次执行失败。
	CronJobStatusError CronJobStatus = "error"

	// CronJobStatusPending indicates the job has not yet executed.
	//
	// CronJobStatusPending 表示任务尚未执行过。
	CronJobStatusPending CronJobStatus = "pending"

	// CronJobStatusRunning indicates the job is currently executing.
	//
	// CronJobStatusRunning 表示任务正在执行中。
	CronJobStatusRunning CronJobStatus = "running"
)

type CronRuntimeConfigurator

type CronRuntimeConfigurator interface {
	// ConfigureCronRuntime configures the cron runtime and returns the number of registered jobs.
	//
	// ConfigureCronRuntime 配置 cron 运行时,并返回注册任务数量。
	ConfigureCronRuntime(Container) (int, error)
}

CronRuntimeConfigurator configures the cron runtime inside a container.

CronRuntimeConfigurator 定义容器内的 cron 运行时配置器。

type DAGEdge

type DAGEdge struct {
	From string // 依赖方 provider
	To   string // 被依赖方 provider(可能为空,表示依赖外部绑定)
	Key  string // 依赖的契约键
}

DAGEdge represents a dependency edge in the DAG.

DAGEdge 表示 DAG 中的依赖边。

type Factory

type Factory func(Container) (any, error)

Factory creates one service value from the container.

Factory 定义从容器创建服务值的工厂函数。

type GRPCRuntimeBuilder

type GRPCRuntimeBuilder interface {
	// BuildGRPCServer builds and returns a gRPC server.
	//
	// BuildGRPCServer 构建并返回一个 gRPC 服务端。
	BuildGRPCServer() *grpc.Server
}

GRPCRuntimeBuilder builds a gRPC server runtime.

GRPCRuntimeBuilder 定义 gRPC 服务端运行时构建器。

type HTTPRuntimeConfigurator

type HTTPRuntimeConfigurator interface {
	// ConfigureHTTPRuntime configures the HTTP runtime.
	//
	// ConfigureHTTPRuntime 配置 HTTP 运行时。
	ConfigureHTTPRuntime(Container) error
}

HTTPRuntimeConfigurator configures the HTTP runtime inside a container.

HTTPRuntimeConfigurator 定义容器内的 HTTP 运行时配置器。

type Host

type Host interface {
	Start(ctx context.Context) error
	Stop(ctx context.Context) error
	Shutdown(ctx context.Context) error
	RegisterService(name string, service Hostable) error
	Services() []string
}

Host defines the host runtime that manages named services.

Host 定义负责管理具名服务的宿主运行时。

type Hostable

type Hostable interface {
	Name() string
	Start(ctx context.Context) error
	Stop(ctx context.Context) error
}

Hostable defines one host-managed service.

Hostable 定义一个可被 host 管理的服务。

type Lifecycle

type Lifecycle interface {
	OnStarting(ctx context.Context) error
	OnStarted(ctx context.Context) error
	OnStopping(ctx context.Context) error
	OnStopped(ctx context.Context) error
}

Lifecycle defines start/stop lifecycle hooks.

Lifecycle 定义启动/停止生命周期钩子。

type ProviderDAG

type ProviderDAG struct {
	Nodes     []ProviderDAGNode // 所有节点
	Edges     []DAGEdge         // 依赖边
	Cycles    [][]string        // 检测到的循环依赖
	LoadOrder []string          // 推荐加载顺序
}

ProviderDAG represents the provider dependency graph.

ProviderDAG 表示 provider 依赖图。

type ProviderDAGNode

type ProviderDAGNode struct {
	Name      string   // Provider 名称
	Provides  []string // 提供的契约键
	DependsOn []string // 依赖的契约键
	IsDefer   bool     // 是否延迟加载
	Loaded    bool     // 是否已加载
	Booted    bool     // 是否已启动
}

ProviderDAGNode represents a node in the provider dependency graph.

ProviderDAGNode 表示 provider 依赖图中的一个节点。

type ProviderInfo

type ProviderInfo struct {
	Name    string
	Loaded  bool
	Booted  bool
	IsDefer bool
}

ProviderInfo describes a registered provider's state.

ProviderInfo 描述已注册 provider 的状态。

type Root

type Root interface {
	BasePath() string
	StoragePath() string
	RuntimePath() string
	LogPath() string
	ConfigPath() string
	TempPath() string
}

Root describes runtime filesystem layout access.

Root 描述运行时文件系统布局访问能力。

type ServiceProvider

type ServiceProvider interface {
	// Name returns the unique provider name.
	//
	// Name 返回唯一的 provider 名称。
	Name() string

	// Register binds provider services into the container.
	//
	// Register 将 provider 服务绑定到容器中。
	Register(c Container) error

	// Boot performs boot-time initialization after registration.
	//
	// Boot 在注册完成后执行启动期初始化。
	Boot(c Container) error

	// IsDefer reports whether registration should be deferred until needed.
	//
	// IsDefer 表示该 provider 是否应延迟到真正需要时再装载。
	IsDefer() bool

	// Provides returns the keys that can be resolved from this provider.
	//
	// Provides 返回该 provider 可以提供的 key 列表。
	Provides() []string

	// DependsOn returns the keys that this provider depends on.
	// Used for dependency graph construction and load order computation.
	//
	// DependsOn 返回该 provider 依赖的 key 列表。
	// 用于构建依赖图和计算加载顺序。
	DependsOn() []string
}

ServiceProvider defines one runtime service provider.

ServiceProvider 定义一个运行时服务 provider。

type UnitOfWork

type UnitOfWork interface {
	// Do executes the callback within the unit-of-work boundary.
	//
	// Do 在工作单元边界内执行回调。
	Do(ctx context.Context, fn func(ctx context.Context) error) error
}

UnitOfWork defines a lightweight transactional boundary.

UnitOfWork 定义轻量事务边界。

Jump to

Keyboard shortcuts

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