Files
pipely/internal/download/limiter.go
T

20 lines
820 B
Go
Raw Normal View History

2026-07-07 10:29:28 +08:00
package download
// GlobalDownloadGate wraps a GatedSemaphore for download-specific concurrency control.
// See internal/middleware/rate_limit.go for the underlying GatedSemaphore implementation.
//
// The gate ensures that the total number of concurrent download streams never exceeds
// the configured limit (default 50), which keeps total bandwidth usage under 10 Mbps
// when each stream is throttled to ~25 KB/s.
type GlobalDownloadGate struct {
sem chan struct{}
}
func NewGlobalDownloadGate(maxConcurrent int) *GlobalDownloadGate {
return &GlobalDownloadGate{sem: make(chan struct{}, maxConcurrent)}
}
func (g *GlobalDownloadGate) Acquire() { g.sem <- struct{}{} }
func (g *GlobalDownloadGate) Release() { <-g.sem }
func (g *GlobalDownloadGate) Available() int { return cap(g.sem) - len(g.sem) }