Documentation
¶
Overview ¶
Package mirror 在多个目标等价的下载源之间选择并轮换。
Index ¶
- Constants
- Variables
- func IsLoopbackBase(value string) bool
- type Attempt
- type AttemptFunc
- type AttemptOutcome
- type AttemptReport
- type Catalog
- type DownloadFailure
- type DownloadProgress
- type DownloadRequest
- type DownloadResult
- type Downloader
- type DownloaderOption
- type FailureKind
- type IntegrityExhaustedError
- type Kind
- type OutcomeKind
- type PackageIndexRewrite
- type PackageIndexSpec
- type Plan
- type PlanFunc
- type Policy
- type PolicySpec
- type ProbeCache
- type ProbeFunc
- type ProbeOption
- type ProbeReportFunc
- type ProbeResult
- type ProbeTarget
- type Prober
- type ProgressFunc
- type RankedFunc
- type Ranker
- func (r *Ranker) Plan(ctx context.Context, policy Policy, kind Kind) (Plan, error)
- func (r *Ranker) PlanFunc() PlanFunc
- func (r *Ranker) Results(kind Kind) []ProbeResult
- func (r *Ranker) SetTarget(kind Kind, target ProbeTarget)
- func (r *Ranker) Warm(ctx context.Context, policy Policy, kinds ...Kind) error
- type RankerOption
- func WithRankerCache(cache *ProbeCache) RankerOption
- func WithRankerLogger(logger func(message string)) RankerOption
- func WithRankerProbeFunc(probe ProbeFunc) RankerOption
- func WithRankerProber(prober *Prober) RankerOption
- func WithRankerRanked(ranked RankedFunc) RankerOption
- func WithRankerReport(report ProbeReportFunc) RankerOption
- type Reporter
- type RotationError
- type RotationOption
- type RotationResult
- type Rotator
- type Source
- type Target
- type TargetSpec
- type WaitFunc
Constants ¶
const DefaultProbeCacheTTL = 30 * time.Minute
DefaultProbeCacheTTL 是 C16 第 1 条规定的进程内测速结果有效期。
const ( // DefaultProbeWindowBytes 是 C16 规定的探针 Range 窗口(256 KiB)。 DefaultProbeWindowBytes int64 = 256 * 1024 )
Variables ¶
var ( // ErrInvalidProbeOption 表示 NewProber 的 option 非法或重复。 ErrInvalidProbeOption = errors.New("mirror probe option is invalid") // ErrInvalidProbeRequest 表示 Probe 的 Plan 或 ProbeTarget 参数非法。 ErrInvalidProbeRequest = errors.New("mirror probe request is invalid") )
var ( ErrInvalidRotationOption = errors.New( "mirror rotation option is invalid", ) ErrInvalidRotationRequest = errors.New( "mirror rotation request is invalid", ) )
var ErrInvalidDownloaderOption = errors.New("downloader option is invalid")
ErrInvalidDownloaderOption 表示 Downloader option 或私有依赖无效。
var ErrInvalidRankRequest = errors.New("mirror rank request is invalid")
ErrInvalidRankRequest 表示 RankPlan 的输入 Plan、结果集合或 pinned key 不自洽。
var ErrInvalidRankerOption = errors.New("mirror ranker option is invalid")
ErrInvalidRankerOption 表示 NewRanker 的参数或 option 非法。
var ErrPolicyRejected = errors.New("mirror policy selection is rejected")
ErrPolicyRejected 表示合法 Policy 与当前 Catalog 的选择约束冲突。 调用方可把它映射为参数错误;Catalog 自身损坏不匹配此错误。
Functions ¶
func IsLoopbackBase ¶ added in v0.1.7
IsLoopbackBase 报告地址是否是回环中继地址;用于把回环首项与 HTTPS 上游区分开。
Types ¶
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 ¶
DefaultCatalog 通过生产校验路径构造冻结的内置 Source。
func NewCatalog ¶
NewCatalog 校验并防御性复制完整的四类 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
// Source 是本次尝试的源 key,由轮换调用方在转发前填入;下载器自身不知道源。
Source string
}
DownloadProgress 描述已确认完整写入并纳入摘要的字节进度。
type DownloadRequest ¶
type DownloadRequest struct {
URL string
FileName string
ExpectedSize int64
AllowUnknownSize bool
MaxSize int64
ExpectedSHA256 string
Progress ProgressFunc
}
DownloadRequest 描述单次下载已冻结的 URL、文件身份和完整性预期。
type DownloadResult ¶
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 NetworkFailureKind ¶ added in v0.1.28
func NetworkFailureKind(err error) FailureKind
NetworkFailureKind 从结构化错误链提取安全类别;未知错误不根据文本猜测。
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 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" )
type PackageIndexRewrite ¶
type PackageIndexRewrite struct {
// contains filtered or unexported fields
}
PackageIndexRewrite 保存包索引源在锁文件 URL 改写中使用的两个前缀。
两个前缀由目录显式声明,不从 BaseURL 推导:官方源的 artifact 位于 files.pythonhosted.org,与它的索引不同 host,任何「去掉结尾 simple/」 一类的推导都会得到不存在的地址;三家镜像索引与 artifact 同 host 只是巧合。
func NewLoopbackRewrite ¶ added in v0.1.7
func NewLoopbackRewrite(baseURL string) (PackageIndexRewrite, error)
NewLoopbackRewrite 构造指向回环中继的锁改写前缀(增补 2 C17 第 2 条)。
只接受 http://127.0.0.1:<port>:这是「规则 8 回环例外」的唯一形态,其它主机或协议一律拒绝, 目录里的镜像前缀仍经 NewPackageIndexSource 走 HTTPS 校验。
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 RankPlan ¶ added in v0.1.7
func RankPlan(plan Plan, results []ProbeResult, pinned string) (Plan, error)
RankPlan 按 C16 第 3 条重排:pinned(显式首选 key,可空)钉最前;其余按 BytesPerSecond 降序、 TTFB 升序;OK=false 的源排最后但保留原相对顺序;results 缺失的源视为 OK=false。 返回的 Plan 与输入同 Kind、同源集合;results 里出现 plan 之外的源或重复源都是错误。
type PlanFunc ¶ added in v0.1.7
PlanFunc 按策略给出某个 Kind 的尝试顺序。
默认实现是目录顺序(CatalogPlanFunc);增补 2 C16 起由测速排序的实现注入, 消费方(uv 下载器、Python 安装、依赖同步、Git 克隆、后端注入面)统一经它取顺序, 不再各自直接调用 BuildPlan。ctx 供实现在必要时做有界网络探测。
func CatalogPlanFunc ¶ added in v0.1.7
CatalogPlanFunc 返回按目录顺序构造 Plan 的 PlanFunc;catalog 为 nil 时返回 nil。
type Policy ¶
type Policy struct {
// contains filtered or unexported fields
}
Policy 保存不可变的已解析镜像策略。
type PolicySpec ¶
PolicySpec 是 CLI 已解析镜像策略的构造输入。
type ProbeCache ¶ added in v0.1.7
type ProbeCache struct {
// contains filtered or unexported fields
}
ProbeCache 是进程内按 Kind 缓存的实测顺序,供长驻进程复用;过期即未命中。 零值可用(默认时钟与 TTL),并发安全。
func NewProbeCache ¶ added in v0.1.7
func NewProbeCache(clock func() time.Time, ttl time.Duration) *ProbeCache
NewProbeCache 构造缓存;clock 为 nil 时用 time.Now,ttl 非正时用 DefaultProbeCacheTTL。
func (*ProbeCache) Lookup ¶ added in v0.1.7
func (c *ProbeCache) Lookup(kind Kind, seal string) (Plan, []ProbeResult, bool)
Lookup 返回 kind 下以 seal(输入 plan 的 Seal())登记且未过期的实测顺序副本。 过期项在此处被移除。
func (*ProbeCache) Store ¶ added in v0.1.7
func (c *ProbeCache) Store(kind Kind, seal string, plan Plan, results []ProbeResult)
Store 登记 kind 的实测顺序,替换同 Kind 的既有项;输入不自洽时静默忽略—— 缓存只是加速,写不进去的后果仅是下次重新测速。
type ProbeFunc ¶ added in v0.1.7
type ProbeFunc func(ctx context.Context, plan Plan, target ProbeTarget, report func(ProbeResult)) ([]ProbeResult, error)
ProbeFunc 是可注入的探测实现,形态与 (*Prober).Probe 相同;供其它包的测试替换真实探测。
func (ProbeFunc) Probe ¶ added in v0.1.7
func (f ProbeFunc) Probe(ctx context.Context, plan Plan, target ProbeTarget, report func(ProbeResult)) ([]ProbeResult, error)
Probe 让 ProbeFunc 满足 Ranker 的探测依赖。
type ProbeOption ¶ added in v0.1.7
type ProbeOption func(*probeOptions) error
ProbeOption 配置构造后只读的 Prober。
func WithProbeBudget ¶ added in v0.1.7
func WithProbeBudget(budget time.Duration) ProbeOption
WithProbeBudget 设置一次 Probe 的总预算,默认 3s。
func WithProbeClient ¶ added in v0.1.7
func WithProbeClient(client httpClient) ProbeOption
WithProbeClient 注入 HTTP 客户端;测试用 httptest,生产默认沿用下载器的重定向策略。
func WithProbeClock ¶ added in v0.1.7
func WithProbeClock(clock func() time.Time) ProbeOption
WithProbeClock 注入计时使用的时钟。
type ProbeReportFunc ¶ added in v0.1.7
type ProbeReportFunc func(kind Kind, result ProbeResult, completed, total int) error
ProbeReportFunc 在某个源探测完成时同步回调:completed 为该 Kind 已完成的源数,total 为源总数。 返回错误会让本次排序失败并上抛(协议输出已断时没有必要继续)。
type ProbeResult ¶ added in v0.1.7
type ProbeResult struct {
Source Source
OK bool
TTFB time.Duration
BytesPerSecond int64
Bytes int64
// AcceptRanges 表示返回 206 或响应头 Accept-Ranges: bytes。
AcceptRanges bool
Err error
}
ProbeResult 是单个源的探测结果;Err 非空时 OK 为 false,其余数值为零。
type ProbeTarget ¶ added in v0.1.7
type ProbeTarget struct {
Kind Kind
// Path 例:"0.12.3/uv-x86_64-pc-windows-msvc.zip";git 为 "info/refs?service=git-upload-pack"。
Path string
// WindowBytes 是 Range 窗口;为 0 时不带 Range、只测 TTFB,收到响应头即完成。
WindowBytes int64
}
ProbeTarget 描述一个 Kind 的探针:Path 拼在每个源的 base(package-index 用 packages 前缀)之后。
type Prober ¶ added in v0.1.7
type Prober struct {
// contains filtered or unexported fields
}
Prober 并行探测一个 Plan 内全部源的首字节延迟与窗口吞吐。 同一 Prober 可被多个 goroutine 并发调用 Probe。
func NewProber ¶ added in v0.1.7
func NewProber(options ...ProbeOption) (*Prober, error)
NewProber 构造只读 Prober;未注入的依赖使用生产默认值。
func (*Prober) Probe ¶ added in v0.1.7
func (p *Prober) Probe( ctx context.Context, plan Plan, target ProbeTarget, report func(ProbeResult), ) ([]ProbeResult, error)
Probe 并行探测 plan 内全部源,预算到期即收口;永不返回网络错误(结果写进 ProbeResult.Err)。 只有 ctx 取消或参数非法才返回 error。report 可为 nil,每个源完成时在调用方 goroutine 上同步回调一次,回调之间不会并发。返回的切片按 plan 顺序排列。
type ProgressFunc ¶
type ProgressFunc func(progress DownloadProgress) error
ProgressFunc 同步接收单调下载进度。
type RankedFunc ¶ added in v0.1.7
type RankedFunc func(kind Kind, ranked Plan, results []ProbeResult) error
RankedFunc 在某个 Kind 完成一轮探测并排好序后同步回调一次(缓存命中不回调),供发出 network.probe 的收口事件。 返回错误会让本次排序失败并上抛。
type Ranker ¶ added in v0.1.7
type Ranker struct {
// contains filtered or unexported fields
}
Ranker 把 Prober、RankPlan 与 ProbeCache 组合成一个 PlanFunc(增补 2 C16)。
没有探针目标的 Kind 按目录顺序返回、不联网;探针失败(全部源失败、探测器出错)只会退回目录顺序, 永远不让操作失败。同一 Ranker 可被多个 goroutine 并发使用。
func NewRanker ¶ added in v0.1.7
func NewRanker(catalog *Catalog, options ...RankerOption) (*Ranker, error)
NewRanker 创建按实测排序的 PlanFunc 提供者。
func (*Ranker) Results ¶ added in v0.1.7
func (r *Ranker) Results(kind Kind) []ProbeResult
Results 返回某个 Kind 最近一次探测的结果(按最终尝试顺序);未探测过返回 nil。
func (*Ranker) SetTarget ¶ added in v0.1.7
func (r *Ranker) SetTarget(kind Kind, target ProbeTarget)
SetTarget 登记某个 Kind 的探针目标;uv 与 git 的目标在启动时已知,python 与 package-index 由各自阶段在探测前登记。
type RankerOption ¶ added in v0.1.7
RankerOption 配置构造后只读的 Ranker 依赖。
func WithRankerCache ¶ added in v0.1.7
func WithRankerCache(cache *ProbeCache) RankerOption
WithRankerCache 注入实测顺序缓存;不注入时使用 30 分钟 TTL 的进程内缓存。
func WithRankerLogger ¶ added in v0.1.7
func WithRankerLogger(logger func(message string)) RankerOption
WithRankerLogger 注入探测失败时的诊断日志出口(写操作日志,不是 stdout)。
func WithRankerProbeFunc ¶ added in v0.1.7
func WithRankerProbeFunc(probe ProbeFunc) RankerOption
WithRankerProbeFunc 注入探测函数(测试替身);nil 视为参数错误。
func WithRankerProber ¶ added in v0.1.7
func WithRankerProber(prober *Prober) RankerOption
WithRankerProber 注入探测器;不注入时使用默认 HTTP 客户端与 3 秒预算的 Prober。
func WithRankerRanked ¶ added in v0.1.7
func WithRankerRanked(ranked RankedFunc) RankerOption
WithRankerRanked 注入每轮探测排序完成后的回调(network.probe 收口事件的来源)。
func WithRankerReport ¶ added in v0.1.7
func WithRankerReport(report ProbeReportFunc) RankerOption
WithRankerReport 注入探测进度回调(network.probe 事件的来源)。
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) Error ¶
func (e *RotationError) Error() string
Error 返回不包含 Source URL 或底层错误文本的稳定诊断。
func (*RotationError) Reports ¶
func (e *RotationError) Reports() []AttemptReport
Reports 返回有序 AttemptReport 的防御性副本。
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 注入报告计时使用的时钟。
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 (Source) PackageIndexRewrite ¶
func (s Source) PackageIndexRewrite() (PackageIndexRewrite, bool)
PackageIndexRewrite 返回包索引源的锁改写前缀;未显式声明时报告 false。
type Target ¶
type Target struct {
// contains filtered or unexported fields
}
Target 保存跨 Source 尝试不可改变的目标字段。
func (Target) ValidateForKind ¶
ValidateForKind 校验 kind 执行所需字段与额外关系。