| 1 | // Package throttle begrenst de leessnelheid met een token bucket. |
| 2 | package throttle |
| 3 | |
| 4 | import ( |
| 5 | "context" |
| 6 | "io" |
| 7 | "sync" |
| 8 | "time" |
| 9 | ) |
| 10 | |
| 11 | // Limiter is een token bucket in bytes per seconde, gedeeld door alle |
| 12 | // downloads van één server. |
| 13 | type Limiter struct { |
| 14 | mu sync.Mutex |
| 15 | rate float64 // bytes/s; 0 = onbegrensd |
| 16 | burst float64 |
| 17 | tokens float64 |
| 18 | last time.Time |
| 19 | now func() time.Time |
| 20 | sleep func(context.Context, time.Duration) error |
| 21 | } |
| 22 | |
| 23 | // New maakt een limiter. bytesPerSec 0 = geen limiet. De burst is een |
| 24 | // kwart seconde, minstens 16 KiB. |
| 25 | func New(bytesPerSec int64) *Limiter { |
| 26 | l := &Limiter{rate: float64(bytesPerSec), now: time.Now, sleep: sleepCtx} |
| 27 | l.burst = l.rate / 4 |
| 28 | if l.burst < 16<<10 { |
| 29 | l.burst = 16 << 10 |
| 30 | } |
| 31 | l.tokens = l.burst |
| 32 | l.last = l.now() |
| 33 | return l |
| 34 | } |
| 35 | |
| 36 | func sleepCtx(ctx context.Context, d time.Duration) error { |
| 37 | t := time.NewTimer(d) |
| 38 | defer t.Stop() |
| 39 | select { |
| 40 | case <-ctx.Done(): |
| 41 | return ctx.Err() |
| 42 | case <-t.C: |
| 43 | return nil |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | // Rate is de ingestelde snelheid in bytes/s. |
| 48 | func (l *Limiter) Rate() int64 { |
| 49 | if l == nil { |
| 50 | return 0 |
| 51 | } |
| 52 | l.mu.Lock() |
| 53 | defer l.mu.Unlock() |
| 54 | return int64(l.rate) |
| 55 | } |
| 56 | |
| 57 | // Wait blokkeert tot n bytes gelezen mogen worden. |
| 58 | func (l *Limiter) Wait(ctx context.Context, n int) error { |
| 59 | if l == nil || n <= 0 { |
| 60 | return nil |
| 61 | } |
| 62 | l.mu.Lock() |
| 63 | if l.rate <= 0 { |
| 64 | l.mu.Unlock() |
| 65 | return nil |
| 66 | } |
| 67 | now := l.now() |
| 68 | l.tokens += now.Sub(l.last).Seconds() * l.rate |
| 69 | if l.tokens > l.burst { |
| 70 | l.tokens = l.burst |
| 71 | } |
| 72 | l.last = now |
| 73 | l.tokens -= float64(n) |
| 74 | var wait time.Duration |
| 75 | if l.tokens < 0 { |
| 76 | wait = time.Duration(-l.tokens / l.rate * float64(time.Second)) |
| 77 | } |
| 78 | l.mu.Unlock() |
| 79 | if wait > 0 { |
| 80 | return l.sleep(ctx, wait) |
| 81 | } |
| 82 | return nil |
| 83 | } |
| 84 | |
| 85 | // Reader begrenst r met l. |
| 86 | func (l *Limiter) Reader(ctx context.Context, r io.Reader) io.Reader { |
| 87 | if l == nil { |
| 88 | return r |
| 89 | } |
| 90 | return &reader{ctx: ctx, r: r, l: l} |
| 91 | } |
| 92 | |
| 93 | type reader struct { |
| 94 | ctx context.Context |
| 95 | r io.Reader |
| 96 | l *Limiter |
| 97 | } |
| 98 | |
| 99 | func (t *reader) Read(p []byte) (int, error) { |
| 100 | t.l.mu.Lock() |
| 101 | max := int(t.l.burst) |
| 102 | t.l.mu.Unlock() |
| 103 | if len(p) > max { |
| 104 | p = p[:max] |
| 105 | } |
| 106 | n, err := t.r.Read(p) |
| 107 | if n > 0 { |
| 108 | if werr := t.l.Wait(t.ctx, n); werr != nil { |
| 109 | return n, werr |
| 110 | } |
| 111 | } |
| 112 | return n, err |
| 113 | } |
| 114 | |
| 115 | // SetRate past de snelheid aan zonder lopende downloads te onderbreken. |
| 116 | func (l *Limiter) SetRate(bytesPerSec int64) { |
| 117 | if l == nil { |
| 118 | return |
| 119 | } |
| 120 | l.mu.Lock() |
| 121 | defer l.mu.Unlock() |
| 122 | l.rate = float64(bytesPerSec) |
| 123 | l.burst = l.rate / 4 |
| 124 | if l.burst < 16<<10 { |
| 125 | l.burst = 16 << 10 |
| 126 | } |
| 127 | if l.tokens > l.burst { |
| 128 | l.tokens = l.burst |
| 129 | } |
| 130 | } |