Documentation
¶
Index ¶
- Constants
- func Benchmark(ctx context.Context, base, query string, times int) (time.Duration, error)
- func ClearBenchmarkCache()
- func GetDefaultMirror(mirrors []string) string
- func GetTheFastestMirror(mirrors []string, testURL string) (string, error)
- func GetTheFastestMirrorAsync(distType int, mirrors []string, testURL string, ...)
- func GetTheFastestMirrorWithCache(distType int, mirrors []string, testURL string) (string, error)
- type AsyncBenchmarkCallback
- type AsyncBenchmarkResult
- type BenchmarkCache
- type CachedResult
- type Engine
- func (e *Engine) Benchmark(ctx context.Context, base, query string, times int) (time.Duration, error)
- func (e *Engine) Cache() *BenchmarkCache
- func (e *Engine) ClearCache()
- func (e *Engine) GetTheFastestMirror(mirrors []string, testURL string) (string, error)
- func (e *Engine) GetTheFastestMirrorAsync(distType int, mirrors []string, testURL string, ...)
- func (e *Engine) GetTheFastestMirrorWithCache(distType int, mirrors []string, testURL string) (string, error)
- type Result
- type Results
Constants ¶
const ( // Configuration constants BenchmarkMaxTimeout = 150 * time.Second // detect resource timeout BenchmarkMaxTries = 3 // maximum number of attempts BenchmarkDetectTimeout = 30 * time.Second // for select fast mirror )
const DefaultCacheTTL = 24 * time.Hour
DefaultCacheTTL is the default time-to-live for cached benchmark results.
const MaxBenchmarkConcurrency = 8
MaxBenchmarkConcurrency caps how many mirror benchmarks run in parallel. Mirror lists can have 50+ entries; spawning that many concurrent TCP connections wastes resources and can trip rate limits on shared CDNs.
Variables ¶
This section is empty.
Functions ¶
func ClearBenchmarkCache ¶ added in v0.8.0
func ClearBenchmarkCache()
ClearBenchmarkCache clears the default engine's cached benchmark results. New, per-Server callers should use Engine.ClearCache instead.
func GetDefaultMirror ¶ added in v0.8.0
GetDefaultMirror returns the first mirror from the list as a fallback. This is used when async benchmark is in progress or when benchmark fails.
func GetTheFastestMirror ¶
GetTheFastestMirror is the package-level shim that delegates to the default engine.
func GetTheFastestMirrorAsync ¶ added in v0.8.0
func GetTheFastestMirrorAsync(distType int, mirrors []string, testURL string, callback AsyncBenchmarkCallback)
GetTheFastestMirrorAsync is the package-level shim that delegates to the default engine.
Types ¶
type AsyncBenchmarkCallback ¶ added in v0.8.0
type AsyncBenchmarkCallback func(result AsyncBenchmarkResult)
AsyncBenchmarkCallback is called when an async benchmark completes.
type AsyncBenchmarkResult ¶ added in v0.8.0
AsyncBenchmarkResult represents the result of an async benchmark operation.
type BenchmarkCache ¶ added in v0.8.0
type BenchmarkCache struct {
// contains filtered or unexported fields
}
BenchmarkCache stores cached benchmark results to avoid repeated testing. Results are cached per distribution type with TTL-based expiration.
func GetBenchmarkCache ¶ added in v0.8.0
func GetBenchmarkCache() *BenchmarkCache
GetBenchmarkCache returns the default engine's benchmark cache. New, per-Server callers should use Engine.Cache instead.
func NewBenchmarkCache ¶ added in v0.12.0
func NewBenchmarkCache() *BenchmarkCache
NewBenchmarkCache returns a fresh, empty cache.
func (*BenchmarkCache) ClearCache ¶ added in v0.8.0
func (bc *BenchmarkCache) ClearCache()
ClearCache clears all cached benchmark results.
func (*BenchmarkCache) GetCachedResult ¶ added in v0.8.0
func (bc *BenchmarkCache) GetCachedResult(distType int) (string, bool)
GetCachedResult returns a cached benchmark result if available and not expired.
func (*BenchmarkCache) SetCachedResult ¶ added in v0.8.0
func (bc *BenchmarkCache) SetCachedResult(distType int, fastestMirror string, ttl time.Duration)
SetCachedResult stores a benchmark result in the cache.
type CachedResult ¶ added in v0.8.0
CachedResult represents a cached benchmark result with expiration.
func (CachedResult) IsExpired ¶ added in v0.8.0
func (c CachedResult) IsExpired() bool
IsExpired returns true if the cached result has expired.
type Engine ¶ added in v0.12.0
type Engine struct {
// contains filtered or unexported fields
}
Engine encapsulates everything that used to live as package-level state: the result cache, the singleflight group that collapses concurrent cache-miss requests, and the HTTP client used to probe mirrors.
Each Server should construct its own Engine via NewEngine so a refresh on one Server does not flush another Server's mirror selection cache. The package-level helpers (GetTheFastestMirror, ClearBenchmarkCache, ...) are thin wrappers around a process-wide Default() Engine, kept for backward compatibility with existing tests and any single-Server callers.
func Default ¶ added in v0.12.0
func Default() *Engine
Default returns the process-wide engine used by the package-level helpers.
func NewEngine ¶ added in v0.12.0
func NewEngine() *Engine
NewEngine returns a fresh, independent Engine. Use one per Server.
func (*Engine) Benchmark ¶ added in v0.12.0
func (e *Engine) Benchmark(ctx context.Context, base, query string, times int) (time.Duration, error)
Benchmark performs HTTP GET requests against base+query using the engine's shared HTTP client and reports the average response time.
func (*Engine) Cache ¶ added in v0.12.0
func (e *Engine) Cache() *BenchmarkCache
Cache exposes the engine's result cache for advanced callers / tests.
func (*Engine) ClearCache ¶ added in v0.12.0
func (e *Engine) ClearCache()
ClearCache drops all cached benchmark results held by this engine.
func (*Engine) GetTheFastestMirror ¶ added in v0.12.0
GetTheFastestMirror finds the fastest responding mirror from the provided list. Concurrency is capped at MaxBenchmarkConcurrency. Once `maxResults` valid results are collected, the parent context is cancelled so in-flight benchmarks abort promptly instead of running to completion.
func (*Engine) GetTheFastestMirrorAsync ¶ added in v0.12.0
func (e *Engine) GetTheFastestMirrorAsync(distType int, mirrors []string, testURL string, callback AsyncBenchmarkCallback)
GetTheFastestMirrorAsync runs benchmark in the background and calls the callback when complete. This allows the application to start immediately with a default mirror while benchmarking runs.
Concurrent async calls for the same distType are collapsed via the engine's singleflight group: only one goroutine probes mirrors, and every caller's callback is invoked with the same result. This matches the deduplication behaviour of GetTheFastestMirrorWithCache so a SIGHUP reload that fires sync + async benchmarks in quick succession does not double-probe upstream mirrors.
func (*Engine) GetTheFastestMirrorWithCache ¶ added in v0.12.0
func (e *Engine) GetTheFastestMirrorWithCache(distType int, mirrors []string, testURL string) (string, error)
GetTheFastestMirrorWithCache finds the fastest mirror, using cache if available. This is the preferred method for production use as it avoids repeated benchmarking. Concurrent cache-miss callers for the same distType are collapsed into a single execution via the engine's singleflight group.