backend

package
v0.1.22 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2026 License: AGPL-3.0 Imports: 22 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package backend 监督受管 Python 后端的生命周期。

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrControlStopped 表示控制 reader 已停止接受新命令。
	ErrControlStopped = errors.New("backend control mailbox is stopped")
	// ErrControlMailboxClosed 表示控制 mailbox 已永久收口。
	ErrControlMailboxClosed = errors.New("backend control mailbox is closed")
)
View Source
var (
	ErrEntryNotFound = errors.New("backend entry file not found")
	ErrEntryUnsafe   = errors.New("backend entry file identity is unsafe")
)

EntryError 分类受管入口文件的稳定失败原因。

View Source
var ErrTransactionNotFound = errTransactionNotFound{}

ErrTransactionNotFound 表示当前没有 backend 事务。

Functions

This section is empty.

Types

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

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() (protocol.ControlCommand, bool)

TerminalCommand 返回入队时记录的 first-wins 终止命令。

type ControlReceiver

type ControlReceiver interface {
	Receive(context.Context) (protocol.ControlCommand, error)
}

ControlReceiver 为监督循环提供已由 ControlReader 校验并按 FIFO 排队的命令。

type Dependencies

type Dependencies struct {
	Lock            LockSet
	State           StateStore
	Repository      RepositoryChecker
	Entry           EntryChecker
	UV              UVRunner
	Health          HealthChecker
	Logger          LoggerFactory
	Clock           func() time.Time
	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

type EntryChecker interface {
	Check(context.Context, string) error
}

EntryChecker 验证受管 backend 入口文件的普通文件与 reparse 身份。

type Error

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

Error 是 backend 向 CLI 暴露的稳定错误,保留错误码、stage、details 与根因。

func (*Error) Code

func (e *Error) Code() protocol.Code

Code 返回稳定协议错误码。

func (*Error) Committed

func (e *Error) Committed() bool

Committed 报告监督是否已经越过进程启动/收口提交点。

func (*Error) Details

func (e *Error) Details() map[string]any

Details 返回 details 的防御性副本;没有细节时仍返回空对象。

func (*Error) Error

func (e *Error) Error() string

func (*Error) Message

func (e *Error) Message() string

Message 返回给 CLI 的稳定用户文案。

func (*Error) Stage

func (e *Error) Stage() protocol.Stage

Stage 返回错误发生的生命周期 stage。

func (*Error) TerminalStatus

func (e *Error) TerminalStatus() string

TerminalStatus 返回 backend 失败对应的生命周期终态。

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap 返回内部根因。

type EventEmitter

type EventEmitter interface {
	EmitState(protocol.StateEvent) error
	EmitLog(protocol.LogEvent) error
	EmitWarning(protocol.WarningEvent) error
}

EventEmitter 是后端监督使用的窄事件出口;顶层 error/result 由 CLI 统一发出。

type HTTPCloser

type HTTPCloser interface {
	Close(context.Context) error
}

HTTPCloser 请求受管后端执行优雅关闭。

type HealthChecker

type HealthChecker interface {
	Check(context.Context, health.Expectation, health.Probe) error
}

HealthChecker 执行已注入的后端健康与身份检查。

type Lease

type Lease interface {
	Close() error
}

Lease 是一次后端 Mutex 租约。

type LockSet

type LockSet interface {
	Acquire(context.Context) (Lease, error)
	Close() error
}

LockSet 提供后端 Mutex 的零等待租约。

type Logger

type Logger interface {
	Record(context.Context, process.StreamRecord) error
	LogPath() string
	Close() error
}

Logger 记录受管流并提供日志路径与收口操作。

type LoggerFactory

type LoggerFactory func(context.Context, Request) (Logger, error)

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 的后端监督器。

func NewProductionManagedSupervisor

func NewProductionManagedSupervisor(
	context.Context,
	*config.Layout,
	io.Writer,
	func() time.Time,
	mirror.Policy,
) (*ManagedSupervisor, error)

NewProductionManagedSupervisor 在非 Windows 平台失败关闭;首版不支持跨平台进程监督。

func (*ManagedSupervisor) Supervise

func (s *ManagedSupervisor) Supervise(ctx context.Context, request Request) (returnErr error)

Supervise 启动并长驻监督指定模式的后端,直到调用方取消或 Job 根进程退出。

type Mode

type Mode string

Mode 选择后端源码与环境的监督策略。

const (
	// ModeManaged 使用 Runtime 管理的仓库、环境和版本身份。
	ModeManaged Mode = "managed"
	// ModeDevelopment 只监督调用方显式指定的源码目录与既有 .venv。
	ModeDevelopment Mode = "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

type RepositoryResult struct {
	Healthy bool
	Version string
	Commit  string
	Reason  string
}

RepositoryResult 是 backend 需要的仓库检查结果。

type Request

type Request struct {
	OperationID     string
	RuntimePID      uint32
	Mode            Mode
	DevelopmentRepo string
	// 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()
}

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 Timer

type Timer interface {
	C() <-chan time.Time
	Stop() bool
}

Timer 是可停止的重启等待计时器,避免 timer channel 在收口后泄漏。

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

type TransactionInput struct {
	OperationID string
	PID         uint32
	Version     string
	Stage       protocol.Stage
}

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 的唯一启动入口。

Jump to

Keyboard shortcuts

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