mirror

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2026 License: AGPL-3.0 Imports: 16 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 mirror 在多个目标等价的下载源之间选择并轮换。

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidRotationOption = errors.New(
		"mirror rotation option is invalid",
	)
	ErrInvalidRotationRequest = errors.New(
		"mirror rotation request is invalid",
	)
)
View Source
var ErrInvalidDownloaderOption = errors.New("downloader option is invalid")

ErrInvalidDownloaderOption 表示 Downloader option 或私有依赖无效。

View Source
var ErrPolicyRejected = errors.New("mirror policy selection is rejected")

ErrPolicyRejected 表示合法 Policy 与当前 Catalog 的选择约束冲突。 调用方可把它映射为参数错误;Catalog 自身损坏不匹配此错误。

Functions

This section is empty.

Types

type Attempt

type Attempt struct {
	Source    Source
	Target    Target
	SourceTry int
	GlobalTry int
}

Attempt 描述一次具体 Source 尝试的不可变输入。

type AttemptFunc

type AttemptFunc func(
	ctx context.Context,
	attempt Attempt,
) AttemptOutcome

AttemptFunc 对一个 Source 与原始 Target 执行一次调用方操作。

type AttemptOutcome

type AttemptOutcome struct {
	Kind         OutcomeKind
	FailureKind  FailureKind
	ActualCommit string
	Err          error
}

AttemptOutcome 是 AttemptFunc 返回的结构化结果。

type AttemptReport

type AttemptReport struct {
	Kind         Kind
	SourceKey    string
	SourceTry    int
	GlobalTry    int
	Target       Target
	TargetHash   string
	StartedAt    time.Time
	Duration     time.Duration
	Outcome      OutcomeKind
	FailureKind  FailureKind
	Error        string
	ActualCommit string
}

AttemptReport 保存一次实际 Attempt 的稳定诊断字段。

type Catalog

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

Catalog 保存按 Kind 分组的不可变有序 Source。

func DefaultCatalog

func DefaultCatalog() (*Catalog, error)

DefaultCatalog 通过生产校验路径构造冻结的内置 Source。

func NewCatalog

func NewCatalog(sources []Source) (*Catalog, error)

NewCatalog 校验并防御性复制完整的四类 Source。

func (*Catalog) Source

func (c *Catalog) Source(kind Kind, key string) (Source, bool)

Source 按 Kind 与 key 查找一个不可变 Source。

func (*Catalog) Sources

func (c *Catalog) Sources(kind Kind) []Source

Sources 按配置顺序返回 kind 的 Source 防御性副本。

type DownloadFailure

type DownloadFailure struct {
	Kind       FailureKind
	StatusCode int
	Published  bool
	Err        error
	CleanupErr error
}

DownloadFailure 保存稳定分类、发布事实以及主错误和清理错误链。

func (*DownloadFailure) Error

func (e *DownloadFailure) Error() string

Error 返回不含 URL 或外部错误原文的稳定文本。

func (*DownloadFailure) Unwrap

func (e *DownloadFailure) Unwrap() []error

Unwrap 按主错误、清理错误的顺序保留完整类型链。

type DownloadProgress

type DownloadProgress struct {
	Received int64
	Total    int64
	Percent  float64
}

DownloadProgress 描述已确认完整写入并纳入摘要的字节进度。

type DownloadRequest

type DownloadRequest struct {
	URL              string
	FileName         string
	ExpectedSize     int64
	AllowUnknownSize bool
	MaxSize          int64
	ExpectedSHA256   string
	Progress         ProgressFunc
}

DownloadRequest 描述单次下载已冻结的 URL、文件身份和完整性预期。

type DownloadResult

type DownloadResult struct {
	Path   string
	Size   int64
	SHA256 string
}

DownloadResult 描述已发布或已发生发布事实的制品。

type Downloader

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

Downloader 执行一个已确定资源的一次流式 HTTPS 下载。

func NewDownloader

func NewDownloader(
	layout *config.Layout,
	options ...DownloaderOption,
) (*Downloader, error)

NewDownloader 创建构造后只读、可并发调用的 Downloader。

func (*Downloader) Download

func (d *Downloader) Download(
	ctx context.Context,
	request DownloadRequest,
) (DownloadResult, error)

Download 执行一次已确定资源的 HTTPS 传输、校验和 no-replace 发布。

type DownloaderOption

type DownloaderOption func(*downloaderOptions) error

DownloaderOption 配置 Downloader 的有限时间参数。

func WithDownloaderProgressInterval

func WithDownloaderProgressInterval(interval time.Duration) DownloaderOption

WithDownloaderProgressInterval 设置非终点进度的最小报告间隔。

func WithDownloaderTimeouts

func WithDownloaderTimeouts(
	connectTimeout time.Duration,
	readTimeout time.Duration,
) DownloaderOption

WithDownloaderTimeouts 设置连接阶段与读取空闲超时。

type FailureKind

type FailureKind string

FailureKind 是不参与状态迁移的开放诊断标识。

const (
	// FailureInvalidRequest 表示下载请求的固定字段无效。
	FailureInvalidRequest FailureKind = "invalid_request"
	// FailureURLPolicy 表示 URL 不符合绝对 HTTPS 策略。
	FailureURLPolicy FailureKind = "url_policy"
	// FailureRedirectDowngrade 表示 HTTPS 重定向尝试降级为 HTTP。
	FailureRedirectDowngrade FailureKind = "redirect_downgrade"
	// FailureConnectTimeout 表示请求在响应头返回前超时。
	FailureConnectTimeout FailureKind = "connect_timeout"
	// FailureReadTimeout 表示响应体网络读取空闲超时。
	FailureReadTimeout FailureKind = "read_timeout"
	// FailureNetwork 表示未细分的网络传输故障。
	FailureNetwork FailureKind = "network"
	// FailureHTTPStatus 表示响应状态不是 200。
	FailureHTTPStatus FailureKind = "http_status"
	// FailureSizeMismatch 表示制品大小与预期不一致。
	FailureSizeMismatch FailureKind = "size_mismatch"
	// FailureChecksumMismatch 表示制品摘要与预期不一致。
	FailureChecksumMismatch FailureKind = "checksum_mismatch"
	// FailureCancelled 表示调用方取消了下载。
	FailureCancelled FailureKind = "cancelled"
	// FailureProgress 表示同步进度回调失败。
	FailureProgress FailureKind = "progress"
	// FailureDestinationOccupied 表示最终目标已被其他对象占用。
	FailureDestinationOccupied FailureKind = "destination_occupied"
	// FailureFilesystem 表示本地文件会话操作失败。
	FailureFilesystem FailureKind = "filesystem"
)

func (FailureKind) String

func (k FailureKind) String() string

String 返回 FailureKind 的稳定字面量。

func (FailureKind) Valid

func (k FailureKind) Valid() bool

Valid 报告 FailureKind 是否满足开放的安全 snake_case 语法。

type IntegrityExhaustedError

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

IntegrityExhaustedError 表示自然耗尽中至少发生一次完整性失败。

func (*IntegrityExhaustedError) Error

func (e *IntegrityExhaustedError) Error() string

Error 返回不包含 Source URL 或底层错误文本的稳定诊断。

func (*IntegrityExhaustedError) Reports

func (e *IntegrityExhaustedError) Reports() []AttemptReport

Reports 返回有序 AttemptReport 的防御性副本。

func (*IntegrityExhaustedError) Unwrap

func (e *IntegrityExhaustedError) Unwrap() []error

Unwrap 返回底层错误链的防御性副本。

type Kind

type Kind string

Kind 标识互不混用的网络源类别。

const (
	// KindGit 表示后端 Git 仓库源。
	KindGit Kind = "git"
	// KindUV 表示 uv 二进制发布源。
	KindUV Kind = "uv"
	// KindPython 表示 Python standalone 分发源。
	KindPython Kind = "python"
	// KindPackageIndex 表示主项目 Python 包索引。
	KindPackageIndex Kind = "package-index"
)

func AllKinds

func AllKinds() []Kind

AllKinds 按冻结顺序返回全部 Kind。

func (Kind) String

func (k Kind) String() string

String 返回 Kind 的稳定字面量。

func (Kind) Valid

func (k Kind) Valid() bool

Valid 报告 Kind 是否属于冻结全集。

type OutcomeKind

type OutcomeKind string

OutcomeKind 是 AttemptFunc 返回的结构化状态转换。

const (
	OutcomeSucceeded        OutcomeKind = "succeeded"
	OutcomeRetrySameSource  OutcomeKind = "retry_same_source"
	OutcomeSwitchSource     OutcomeKind = "switch_source"
	OutcomeIntegrityFailure OutcomeKind = "integrity_failure"
	OutcomeTargetFailure    OutcomeKind = "target_failure"
	OutcomeCancelled        OutcomeKind = "cancelled"
)

func (OutcomeKind) String

func (k OutcomeKind) String() string

String 返回 OutcomeKind 的稳定字面量。

func (OutcomeKind) Valid

func (k OutcomeKind) Valid() bool

Valid 报告 OutcomeKind 是否属于冻结全集。

type PackageIndexRewrite

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

PackageIndexRewrite 保存包索引源在锁文件 URL 改写中使用的两个前缀。

两个前缀由目录显式声明,不从 BaseURL 推导:官方源的 artifact 位于 files.pythonhosted.org,与它的索引不同 host,任何「去掉结尾 simple/」 一类的推导都会得到不存在的地址;三家镜像索引与 artifact 同 host 只是巧合。

func (PackageIndexRewrite) PackagesBase

func (r PackageIndexRewrite) PackagesBase() string

PackagesBase 返回替换 https://files.pythonhosted.org/packages/ 的前缀,以 / 结尾。

func (PackageIndexRewrite) SimpleBase

func (r PackageIndexRewrite) SimpleBase() string

SimpleBase 返回替换 https://pypi.org/simple 的前缀,不以 / 结尾。

type PackageIndexSpec

type PackageIndexSpec struct {
	Key          string
	BaseURL      string
	SimpleBase   string
	PackagesBase string
	Official     bool
}

PackageIndexSpec 是带显式锁改写前缀的包索引源构造输入。

type Plan

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

Plan 是单次操作的不可变 Source 顺序快照。

func BuildPlan

func BuildPlan(
	catalog *Catalog,
	policy Policy,
	kind Kind,
) (Plan, error)

BuildPlan 校验 Catalog/Policy 并生成确定性尝试顺序。

func (Plan) Kind

func (p Plan) Kind() Kind

Kind 返回 Plan 的单一 Source 类别。

func (Plan) Offline

func (p Plan) Offline() bool

Offline 报告 Plan 是否为合法离线快照。

func (Plan) Sources

func (p Plan) Sources() []Source

Sources 返回 Plan 内有序 Source 的防御性副本。

type Policy

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

Policy 保存不可变的已解析镜像策略。

func NewPolicy

func NewPolicy(spec PolicySpec) (Policy, error)

NewPolicy 校验并防御性复制 CLI 已解析值。

func (Policy) MirrorOnly

func (p Policy) MirrorOnly() bool

MirrorOnly 报告策略是否排除 official 兜底。

func (Policy) Offline

func (p Policy) Offline() bool

Offline 报告策略是否禁止全部网络尝试。

func (Policy) Preferred

func (p Policy) Preferred(kind Kind) (string, bool)

Preferred 返回 kind 的显式首选 key。

type PolicySpec

type PolicySpec struct {
	Preferred  map[Kind]string
	Offline    bool
	MirrorOnly bool
}

PolicySpec 是 CLI 已解析镜像策略的构造输入。

type ProgressFunc

type ProgressFunc func(progress DownloadProgress) error

ProgressFunc 同步接收单调下载进度。

type Reporter

type Reporter interface {
	ReportAttempt(ctx context.Context, report AttemptReport) error
}

Reporter 同步持久化一次已冻结的 AttemptReport。 有状态实现会被同一 Rotator 的并发 Run 调用,必须自行保证并发安全。

type RotationError

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

RotationError 表示离线或全部普通 Source 自然耗尽。

func (*RotationError) Code

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

Code 返回供上层映射的稳定协议错误码。

func (*RotationError) Error

func (e *RotationError) Error() string

Error 返回不包含 Source URL 或底层错误文本的稳定诊断。

func (*RotationError) Reports

func (e *RotationError) Reports() []AttemptReport

Reports 返回有序 AttemptReport 的防御性副本。

func (*RotationError) Unwrap

func (e *RotationError) Unwrap() []error

Unwrap 返回底层错误链的防御性副本。

type RotationOption

type RotationOption func(*rotationOptions) error

RotationOption 配置构造后只读的 Rotator。

func WithAttemptReporter

func WithAttemptReporter(reporter Reporter) RotationOption

WithAttemptReporter 注入同步 AttemptReport 接收方。

func WithMaxSourceAttempts

func WithMaxSourceAttempts(attempts int) RotationOption

WithMaxSourceAttempts 配置每个 Source 的有限总尝试次数。

func WithRetryDelay

func WithRetryDelay(delay time.Duration) RotationOption

WithRetryDelay 配置同源重试前的正等待时长。

func WithRotatorClock

func WithRotatorClock(clock func() time.Time) RotationOption

WithRotatorClock 注入报告计时使用的时钟。

func WithRotatorWait

func WithRotatorWait(wait WaitFunc) RotationOption

WithRotatorWait 注入同源重试等待。

type RotationResult

type RotationResult struct {
	Source       Source
	ActualCommit string
	Reports      []AttemptReport
}

RotationResult 保存成功源、Git 实际 Commit 和有序报告。

type Rotator

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

Rotator 保存不可变的轮换依赖与有限重试参数。 同一 Rotator 可被多个 goroutine 并发调用 Run。

func NewRotator

func NewRotator(options ...RotationOption) (*Rotator, error)

NewRotator 使用生产报告 context factory 构造只读 Rotator。

func (*Rotator) Run

func (r *Rotator) Run(
	ctx context.Context,
	plan Plan,
	target Target,
	attempt AttemptFunc,
) (RotationResult, error)

Run 按 Plan 顺序执行有限尝试并保持 Target 不变。

type Source

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

Source 是经过校验的不可变 HTTPS 网络源。

simpleBase 与 packagesBase 只对 KindPackageIndex 有意义,且只能由 NewPackageIndexSource 显式赋值,经 PackageIndexRewrite 读取。

func NewPackageIndexSource

func NewPackageIndexSource(spec PackageIndexSpec) (Source, error)

NewPackageIndexSource 校验并构造带显式改写前缀的包索引源。

func NewSource

func NewSource(
	kind Kind,
	key string,
	baseURL string,
	official bool,
) (Source, error)

NewSource 校验并规范化单个网络源。

func (Source) BaseURL

func (s Source) BaseURL() string

BaseURL 返回规范化 HTTPS URL 字符串。

func (Source) Key

func (s Source) Key() string

Key 返回同一 Kind 内唯一的稳定 key。

func (Source) Kind

func (s Source) Kind() Kind

Kind 返回 Source 所属类别。

func (Source) Official

func (s Source) Official() bool

Official 报告 Source 是否为该 Kind 的官方源。

func (Source) PackageIndexRewrite

func (s Source) PackageIndexRewrite() (PackageIndexRewrite, bool)

PackageIndexRewrite 返回包索引源的锁改写前缀;未显式声明时报告 false。

type Target

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

Target 保存跨 Source 尝试不可改变的目标字段。

func NewTarget

func NewTarget(spec TargetSpec) (Target, error)

NewTarget 校验、规范化并密封目标字段。

func (Target) Fingerprint

func (t Target) Fingerprint() string

Fingerprint 返回带字段名规范序列的 SHA-256。

func (Target) LockDigest

func (t Target) LockDigest() string

LockDigest 返回规范化的小写锁文件 SHA-256。

func (Target) ProductVersion

func (t Target) ProductVersion() string

ProductVersion 返回目标产品版本。

func (Target) PythonVersion

func (t Target) PythonVersion() string

PythonVersion 返回目标 Python 版本。

func (Target) ReleaseBranch

func (t Target) ReleaseBranch() string

ReleaseBranch 返回产品版本派生的发布分支。

func (Target) UVVersion

func (t Target) UVVersion() string

UVVersion 返回目标 uv 版本。

func (Target) ValidateForKind

func (t Target) ValidateForKind(kind Kind) error

ValidateForKind 校验 kind 执行所需字段与额外关系。

type TargetSpec

type TargetSpec struct {
	ProductVersion string
	ReleaseBranch  string
	UVVersion      string
	PythonVersion  string
	LockDigest     string
}

TargetSpec 是目标值对象的构造输入。

type WaitFunc

type WaitFunc func(ctx context.Context, delay time.Duration) error

WaitFunc 在同一 Source 重试前执行可取消等待。

Jump to

Keyboard shortcuts

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