relay

package
v0.1.26 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2026 License: AGPL-3.0 Imports: 21 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 relay 在 127.0.0.1 上提供纯 HTTP 回环中继,让 uv 只从本机取文件: 逐文件多源回退、大文件分片并行、已知哈希先校验再交付,并把聚合进度同步回调出去。

中继只监听回环地址,上游 URL 只能由目录内源的 base 与请求路径拼出; 它不代理任何业务流量,也不跨操作缓存文件。

Index

Constants

View Source
const (
	// DefaultChunkThreshold 是启用分片取回的最小已知文件大小。
	DefaultChunkThreshold int64 = 8 << 20
	// DefaultChunkSize 是分片取回的单片大小。
	DefaultChunkSize int64 = 2 << 20
	// DefaultMaxFiles 是同时下载的文件数上限。
	DefaultMaxFiles = 6
	// DefaultMaxChunksPerFile 是单个文件并行取片的 worker 上限。
	DefaultMaxChunksPerFile = 4
	// DefaultStallTimeout 是响应体读空闲失速判定时间。
	DefaultStallTimeout = 15 * time.Second
	// DefaultConnectTimeout 是响应头返回前的连接超时。
	DefaultConnectTimeout = 15 * time.Second
)

Variables

View Source
var ErrInvalidConfig = errors.New("relay config is invalid")

ErrInvalidConfig 表示 Config 或 Deps 无法用于启动中继。

Functions

This section is empty.

Types

type AttemptOutcome

type AttemptOutcome struct {
	Source  string
	Outcome Outcome
}

AttemptOutcome 记录某个源对某个文件的一次尝试结局。

type Config

type Config struct {
	// MaxUnregisteredBytes 限制缺少锁内身份的制品,零值取 1 GiB。
	MaxUnregisteredBytes int64
	// MaxCachedBytes 与 MaxCachedFiles 限制已完成且无读者的缓存。
	MaxCachedBytes int64
	MaxCachedFiles int
	// StagingDir 是 layout.RelayStagingDir(),中继独占,Close 时清空。
	StagingDir string
	// Upstreams 按路由给出上游列表;至少要有一条非空路由。
	Upstreams map[Route][]Upstream
	// Items 是已知制品,可为空;Register 可追加。
	Items            []Item
	ChunkThreshold   int64
	ChunkSize        int64
	MaxFiles         int
	MaxChunksPerFile int
	StallTimeout     time.Duration
	ConnectTimeout   time.Duration
}

Config 描述一次中继会话的目录、上游与并发参数;零值字段取包内默认值。

type Deps

type Deps struct {
	// PinStaging 由生产调用方固定受管目录及祖先,测试可使用临时目录。
	PinStaging func(context.Context, string) (io.Closer, error)
	// Client 是上游客户端,测试注入。
	Client httpClient
	Clock  func() time.Time
	// Progress 是同步回调;返回错误时中继停止并让后续请求 502。
	Progress func(Progress) error
	Logger   func(level, message string, fields map[string]any)
}

Deps 是中继的可注入依赖;任一字段为 nil 时取生产默认值(Progress 为不回调)。

type FileFailure

type FileFailure struct {
	Item     string
	Attempts []AttemptOutcome
}

FileFailure 记录所有源都失败的文件及其逐源结局。

type Item

type Item struct {
	Path   string
	Size   int64
	SHA256 string
}

Item 描述锁内已知的制品;Path 是 PyPI 原路径("ab/cd/<hash>/x.whl"),SHA256 为十六进制。

type Outcome

type Outcome string

Outcome 是单次上游尝试的稳定结局分类。

const (
	// OutcomeConnectTimeout 表示响应头返回前超时。
	OutcomeConnectTimeout Outcome = "connect_timeout"
	// OutcomeReadTimeout 表示响应体读空闲失速。
	OutcomeReadTimeout Outcome = "read_timeout"
	// OutcomeHTTPStatus 表示上游返回了非预期状态码。
	OutcomeHTTPStatus Outcome = "http_status"
	// OutcomeChecksumMismatch 表示取回内容与登记的 sha256 不符。
	OutcomeChecksumMismatch Outcome = "checksum_mismatch"
	// OutcomeNetwork 表示未细分的网络传输故障。
	OutcomeNetwork Outcome = "network"
	// OutcomeCancelled 表示中继关闭或进度回调失败导致取回被取消。
	OutcomeCancelled Outcome = "cancelled"
)

func (Outcome) String

func (o Outcome) String() string

String 返回 Outcome 的稳定字面量。

type Progress

type Progress struct {
	Route          Route
	Item           string
	Source         string
	Received       int64
	Total          int64
	BytesPerSecond int64
}

Progress 是中继的聚合进度:Item/Source 为当前(最近有字节到达的)文件与源;Total 为已登记 Item 的字节总和,未登记文件在 HEAD 得知大小后并入;Received 为已校验交付的字节 + 进行中 文件的已收字节。

type Route

type Route string

Route 是中继的三个前缀之一。

const (
	// RoutePackages 对应 /packages/<PyPI 原路径>。
	RoutePackages Route = "packages"
	// RouteSimple 对应 /simple/<包名>/。
	RouteSimple Route = "simple"
	// RoutePython 对应 /python/<tag>/<asset>。
	RoutePython Route = "python"
)

func (Route) String

func (r Route) String() string

String 返回 Route 的稳定字面量。

func (Route) Valid

func (r Route) Valid() bool

Valid 报告 Route 是否属于三条固定路由。

type Server

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

Server 是运行中的回环中继。

func Start

func Start(ctx context.Context, cfg Config, deps Deps) (*Server, error)

Start 绑定 127.0.0.1:0 并开始服务;ctx 取消即关闭中继。

func (*Server) BaseURL

func (s *Server) BaseURL() string

BaseURL 返回 "http://127.0.0.1:<port>"。

func (*Server) Close

func (s *Server) Close() error

Close 停止接受连接、取消进行中取回、有界等待全部 goroutine 退出并清空暂存目录。

func (*Server) Register

func (s *Server) Register(items []Item)

Register 追加已知制品(幂等)。

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request)

ServeHTTP 只做路由校验与分发;业务错误一律以状态码表达,不写任何标准输出。

func (*Server) SetUpstreams

func (s *Server) SetUpstreams(route Route, upstreams []Upstream) error

SetUpstreams 在运行期替换某条路由的上游顺序(受监督时后台测速完成后调用)。 已因两次哈希不符被剔除的源不会被重新加回;非法路由或上游返回错误且不改变现有顺序。

func (*Server) Summary

func (s *Server) Summary() Summary

Summary 返回供 result.details.relay 使用的汇总快照。

type Summary

type Summary struct {
	Files    int
	Bytes    int64
	BySource map[string]int64
	Failures []FileFailure
}

Summary 是供 result.details.relay 使用的汇总;Failures 上限 32 项。

type Upstream

type Upstream struct {
	Key string
	// Base 在 packages 路由是 packages 前缀,simple 路由是 simple 前缀,python 路由是源 base + "/"。
	Base string
	// AcceptRange 由探针得知;未知为 false,HEAD 见到 Accept-Ranges 时中继自行更新。
	AcceptRange bool
	// RewriteFrom 只对 simple 路由有意义:该源自己的 packages 前缀,索引响应体内以它开头的
	// URL 会被改写为 <BaseURL>/packages/。为空时不改写。
	RewriteFrom string
}

Upstream 描述某条路由按实测顺序排列的上游前缀(每项以 / 结尾的绝对 HTTPS URL)与源 key。

Jump to

Keyboard shortcuts

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