Documentation
¶
Overview ¶
Package backend 监督受管 Python 后端的生命周期。
Index ¶
- Variables
- type AcceptedControlCommand
- type ControlMailbox
- func (m *ControlMailbox) BeforeShutdown(commandID string)
- func (m *ControlMailbox) Close()
- func (m *ControlMailbox) InfrastructureError() error
- func (m *ControlMailbox) Receive(ctx context.Context) (protocol.ControlCommand, error)
- func (m *ControlMailbox) SetBeforeShutdown(callback func(string))
- func (m *ControlMailbox) SetReaderError(err error)
- func (m *ControlMailbox) SetStage(stage protocol.Stage)
- func (m *ControlMailbox) SetStageCallback(callback func(protocol.Stage))
- func (m *ControlMailbox) StopAccepting()
- func (m *ControlMailbox) Submit(ctx context.Context, command protocol.ControlCommand) error
- func (m *ControlMailbox) TerminalCommand() (AcceptedControlCommand, bool)
- func (m *ControlMailbox) WithTerminalCancellation(parent context.Context) (context.Context, context.CancelFunc)
- type ControlReceiver
- type Dependencies
- type EntryChecker
- type Error
- type EventEmitter
- type HTTPActivator
- type HTTPCloser
- type HealthChecker
- type Lease
- type LockSet
- type Logger
- type LoggerFactory
- type ManagedProcess
- type ManagedSupervisor
- type Mode
- type PIDProbe
- type RelaySession
- type RelayStarter
- type RepositoryChecker
- type RepositoryResult
- type Request
- type StateStore
- type Timer
- type Transaction
- type TransactionHandle
- type TransactionInput
- type UVRunner
Constants ¶
This section is empty.
Variables ¶
var ( // ErrControlStopped 表示控制 reader 已停止接受新命令。 ErrControlStopped = errors.New("backend control mailbox is stopped") // ErrControlMailboxClosed 表示控制 mailbox 已永久收口。 ErrControlMailboxClosed = errors.New("backend control mailbox is closed") )
var ( ErrEntryNotFound = errors.New("backend entry file not found") ErrEntryUnsafe = errors.New("backend entry file identity is unsafe") )
EntryError 分类受管入口文件的稳定失败原因。
var ErrTransactionNotFound = errTransactionNotFound{}
ErrTransactionNotFound 表示当前没有 backend 事务。
Functions ¶
This section is empty.
Types ¶
type AcceptedControlCommand ¶ added in v0.1.31
type AcceptedControlCommand struct {
protocol.ControlCommand
AcceptedAt time.Time
}
AcceptedControlCommand 记录控制命令的实际入队时刻,仅用于本机先后判断。
type ControlMailbox ¶
type ControlMailbox struct {
// contains filtered or unexported fields
}
ControlMailbox 是 ControlReader 与 Supervisor loop 之间的有界 FIFO。 Submit 不执行进程或网络副作用;终止命令在入队时记录 first-wins latch。
func NewControlMailbox ¶
func NewControlMailbox(capacity int) *ControlMailbox
NewControlMailbox 创建有界控制 FIFO;非正容量使用稳定默认值。
func (*ControlMailbox) BeforeShutdown ¶
func (m *ControlMailbox) BeforeShutdown(commandID string)
BeforeShutdown 返回当前 shutdown gate 回调。
func (*ControlMailbox) Close ¶
func (m *ControlMailbox) Close()
Close 终止 mailbox;已入队命令仍可被 Receive 消费一次。
func (*ControlMailbox) InfrastructureError ¶
func (m *ControlMailbox) InfrastructureError() error
InfrastructureError 返回尚未被 Receive 消费的 reader 故障。
func (*ControlMailbox) Receive ¶
func (m *ControlMailbox) Receive(ctx context.Context) (protocol.ControlCommand, error)
Receive 按 FIFO 取出一个命令,并响应 ctx 取消。
func (*ControlMailbox) SetBeforeShutdown ¶
func (m *ControlMailbox) SetBeforeShutdown(callback func(string))
SetBeforeShutdown 注册 shutdown 被消费前的 reader 停止回调。
func (*ControlMailbox) SetReaderError ¶
func (m *ControlMailbox) SetReaderError(err error)
SetReaderError 将控制 reader 的基础设施故障交给 Supervisor 消费, 使其先收口受管进程再由 CLI 分类最终错误。
func (*ControlMailbox) SetStage ¶
func (m *ControlMailbox) SetStage(stage protocol.Stage)
SetStage 将 backend stage 转发给控制 warning 的消费侧。
func (*ControlMailbox) SetStageCallback ¶
func (m *ControlMailbox) SetStageCallback(callback func(protocol.Stage))
SetStageCallback 注册当前 backend stage 的只读同步回调。
func (*ControlMailbox) StopAccepting ¶
func (m *ControlMailbox) StopAccepting()
StopAccepting 先唤醒背压中的 producer,再阻止后续命令进入 FIFO。
func (*ControlMailbox) Submit ¶
func (m *ControlMailbox) Submit(ctx context.Context, command protocol.ControlCommand) error
Submit 将已校验命令入队;满队列时通过 ctx 背压而非丢弃命令。
func (*ControlMailbox) TerminalCommand ¶
func (m *ControlMailbox) TerminalCommand() (AcceptedControlCommand, bool)
TerminalCommand 返回入队时记录的 first-wins 终止命令。
func (*ControlMailbox) WithTerminalCancellation ¶ added in v0.1.31
func (m *ControlMailbox) WithTerminalCancellation(parent context.Context) (context.Context, context.CancelFunc)
WithTerminalCancellation 绑定监督循环当前的外部调用;终止接受与取消在同一锁内完成。
type ControlReceiver ¶
type ControlReceiver interface {
Receive(context.Context) (protocol.ControlCommand, error)
WithTerminalCancellation(context.Context) (context.Context, context.CancelFunc)
}
ControlReceiver 为监督循环提供已由 ControlReader 校验并按 FIFO 排队的命令。
type Dependencies ¶
type Dependencies struct {
Activation HTTPActivator
Lock LockSet
State StateStore
Repository RepositoryChecker
Entry EntryChecker
UV UVRunner
Health HealthChecker
Logger LoggerFactory
Clock func() time.Time
// WithStartupTimeout 创建启动工作预算作用域,默认使用 context.WithTimeoutCause。
WithStartupTimeout func(context.Context, time.Duration, error) (context.Context, context.CancelFunc)
UVPath string
PythonPath string
PythonPaths []string
PID PIDProbe
HTTP HTTPCloser
ShutdownTimeout time.Duration
RestartDelay time.Duration
Timer func(time.Duration) <-chan time.Time
NewTimer func(time.Duration) Timer
// MirrorPolicy 是已解析的全局镜像策略,用于按增补 1 C11 生成下发给后端的
// 有序源列表。零值表示调用方未配置,按目录默认顺序处理。
MirrorPolicy mirror.Policy
// Ranker 是本进程的测速器(增补 2 C16);nil 表示按目录顺序下发。
Ranker *mirror.Ranker
// Relay 启动受监督期间常驻的回环中继(增补 2 C17 第 8 条);nil 表示不起中继。
Relay RelayStarter
}
Dependencies 是 ManagedSupervisor 的消费侧依赖集合。
type EntryChecker ¶
EntryChecker 验证受管 backend 入口文件的普通文件与 reparse 身份。
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
Error 是 backend 向 CLI 暴露的稳定错误,保留错误码、stage、details 与根因。
func (*Error) TerminalStatus ¶
TerminalStatus 返回 backend 失败对应的生命周期终态。
type EventEmitter ¶
type EventEmitter interface {
EmitState(protocol.StateEvent) error
EmitLog(protocol.LogEvent) error
EmitWarning(protocol.WarningEvent) error
}
EventEmitter 是后端监督使用的窄事件出口;顶层 error/result 由 CLI 统一发出。
type HTTPActivator ¶ added in v0.1.31
HTTPActivator 的成功必须来自对应后端确认 active,不能把请求发出当作放行成功。
type HTTPCloser ¶
HTTPCloser 请求受管后端执行优雅关闭。
type HealthChecker ¶
HealthChecker 执行已注入的后端健康与身份检查。
type Logger ¶
type Logger interface {
Record(context.Context, process.StreamRecord) error
LogPath() string
Close() error
}
Logger 记录受管流并提供日志路径与收口操作。
type LoggerFactory ¶
LoggerFactory 延迟创建本次监督操作的日志 sink。
type ManagedProcess ¶
type ManagedProcess interface {
PID() uint32
Exited() <-chan struct{}
Wait(context.Context) (process.ExitResult, error)
Snapshot() ([]process.Info, error)
Terminate(uint32) error
WaitEmpty(context.Context) error
Close() error
}
ManagedProcess 是 backend 需要的进程生命周期能力。
type ManagedSupervisor ¶
type ManagedSupervisor struct {
// contains filtered or unexported fields
}
ManagedSupervisor 监督单个受管后端 Job 的完整生命周期。
func NewManagedSupervisor ¶
func NewManagedSupervisor(layout *config.Layout, deps Dependencies) (*ManagedSupervisor, error)
NewManagedSupervisor 创建可按请求选择 managed 或 development 的后端监督器。
type PIDProbe ¶
type PIDProbe interface {
Alive(context.Context, uint32) (bool, error)
CreatedAt(context.Context, uint32) (time.Time, error)
}
PIDProbe 判断事务记录对应的旧监督进程是否仍存活。
type RelaySession ¶ added in v0.1.7
type RelaySession interface {
BaseURL() string
SetUpstreams(route relay.Route, upstreams []relay.Upstream) error
Close() error
}
RelaySession 是监督器消费的最小中继能力;生产实现是 *relay.Server。
type RelayStarter ¶ added in v0.1.7
type RelayStarter func(ctx context.Context, cfg relay.Config, deps relay.Deps) (RelaySession, error)
RelayStarter 启动一次受监督期间常驻的中继会话(增补 2 C17 第 8 条)。
type RepositoryChecker ¶
type RepositoryChecker interface {
Check(context.Context) (RepositoryResult, error)
}
RepositoryChecker 返回经验证的活动仓库 revision。
type RepositoryResult ¶
RepositoryResult 是 backend 需要的仓库检查结果。
type Request ¶
type Request struct {
DeferBusinessStart bool
AllowLegacyBusinessStart bool
OperationID string
RuntimePID uint32
Mode Mode
DevelopmentRepo string
// ReuseManagedEnvironment 使用同一 app-root 已存在的受管 venv 启动开发源码。
ReuseManagedEnvironment bool
// ShutdownTimeout 是从发出 POST /api/core/close 到进程退出的等待上限,
// 超时才收 Job(增补 1 C9)。CLI 由 --shutdown-timeout 提供,取值 1~120 秒;
// 为零或负数时回退 Dependencies.ShutdownTimeout。
ShutdownTimeout time.Duration
// Port 是受监督后端的监听端口(增补 1 C12)。CLI 由 --port 提供;为零时按模式
// 取缺省(managed 36163 / development 36164),越界映射 INVALID_ARGUMENT。
// 注入 uv 的 AUTO_MAS_SUPERVISED_PORT、健康检查地址、关闭地址与 baseUrl 全部由它派生。
Port int
Emitter EventEmitter
Control ControlReceiver
BeforeShutdown func(string)
BeforeControlClose func()
// contains filtered or unexported fields
}
Request 描述一次受管后端监督请求。
type StateStore ¶
type StateStore interface {
ReadEnvironment(context.Context) (state.EnvironmentState, error)
ReadBackendTransaction(context.Context) (Transaction, error)
BeginBackendTransaction(context.Context, TransactionInput) (TransactionHandle, error)
UpdateBackendTransaction(context.Context, TransactionHandle, protocol.Stage) error
RemoveBackendTransaction(context.Context, TransactionHandle) error
Close() error
}
StateStore 是后端消费的稳定环境与事务存储窄接口。
type Transaction ¶
type Transaction struct {
PID uint32
StartedAt time.Time
Version string
Stage protocol.Stage
Handle TransactionHandle
}
Transaction 描述已有 backend 事务的最小事实。
type TransactionHandle ¶
type TransactionHandle interface{}
TransactionHandle 是条件删除 backend 事务所需的不可伪造 token。
type TransactionInput ¶
TransactionInput 描述新建 backend 事务的业务身份。
type UVRunner ¶
type UVRunner interface {
Check(context.Context, uv.RunOptions) error
Executable() string
StartManaged(context.Context, []string, uv.ManagedOptions, process.StreamSink) (ManagedProcess, error)
}
UVRunner 是长驻受管 uv 的唯一启动入口。