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 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 Benchmark ¶
Benchmark performs HTTP GET requests and measures response time. It uses a shared HTTP client with connection pooling for better performance.
func ClearBenchmarkCache ¶ added in v0.8.0
func ClearBenchmarkCache()
ClearBenchmarkCache clears all cached benchmark results. This is useful when forcing a re-benchmark, e.g., on SIGHUP reload.
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 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 GetTheFastestMirrorAsync ¶ added in v0.8.0
func 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. The callback will be called with the result when the benchmark completes.
func GetTheFastestMirrorWithCache ¶ added in v0.8.0
GetTheFastestMirrorWithCache finds the fastest mirror, using cache if available. This is the preferred method for production use as it avoids repeated benchmarking.
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 global benchmark cache instance. This can be used for testing or advanced cache management.
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.