File: throttler.go

package info (click to toggle)
golang-github-tsenart-tb 0.0~git20151208.0.19f4c3d-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 92 kB
  • ctags: 51
  • sloc: makefile: 2
file content (124 lines) | stat: -rw-r--r-- 3,011 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package tb

import (
	"math"
	"sync"
	"time"
)

// Throttler is a thread-safe wrapper around a map of buckets and an easy to
// use API for generic throttling.
type Throttler struct {
	mu      sync.RWMutex
	freq    time.Duration
	buckets map[string]*Bucket
	closing chan struct{}
}

// NewThrottler returns a Throttler with a single filler go-routine for all
// its Buckets which ticks every freq.
// The number of tokens added on each tick for each bucket is computed
// dynamically to be even accross the duration of a second.
//
// If freq <= 0, the filling go-routine won't be started.
func NewThrottler(freq time.Duration) *Throttler {
	th := &Throttler{
		freq:    freq,
		buckets: map[string]*Bucket{},
		closing: make(chan struct{}),
	}

	if freq > 0 {
		go th.fill(freq)
	}

	return th
}

// Bucket returns a Bucket with rate capacity, keyed by key.
//
// If a Bucket (key, rate) doesn't exist yet, it is created.
//
// You must call Close when you're done with the Throttler in order to not leak
// a go-routine and a system-timer.
func (t *Throttler) Bucket(key string, rate int64) *Bucket {
	t.mu.Lock()
	defer t.mu.Unlock()

	b, ok := t.buckets[key]

	if !ok {
		b = NewBucket(rate, -1)
		b.inc = int64(math.Floor(.5 + (float64(b.capacity) * t.freq.Seconds())))
		b.freq = t.freq
		t.buckets[key] = b
	}

	return b
}

// Wait waits for n amount of tokens to be available.
// If n tokens are immediatelly available it doesn't sleep. Otherwise, it sleeps
// the minimum amount of time required for the remaining tokens to be available.
// It returns the wait duration.
//
// If a Bucket (key, rate) doesn't exist yet, it is created.
// If freq < 1/rate seconds, the effective wait rate won't be correct.
//
// You must call Close when you're done with the Throttler in order to not leak
// a go-routine and a system-timer.
func (t *Throttler) Wait(key string, n, rate int64) time.Duration {
	return t.Bucket(key, rate).Wait(n)
}

// Halt returns a bool indicating if the Bucket identified by key and rate has
// n amount of tokens. If it doesn't, the taken tokens are added back to the
// bucket.
//
// If a Bucket (key, rate) doesn't exist yet, it is created.
// If freq < 1/rate seconds, the results won't be correct.
//
// You must call Close when you're done with the Throttler in order to not leak
// a go-routine and a system-timer.
func (t *Throttler) Halt(key string, n, rate int64) bool {
	b := t.Bucket(key, rate)

	if got := b.Take(n); got != n {
		b.Put(got)
		return true
	}

	return false
}

// Close stops filling the Buckets, closing the filling go-routine.
func (t *Throttler) Close() error {
	close(t.closing)

	t.mu.RLock()
	defer t.mu.RUnlock()

	for _, b := range t.buckets {
		b.Close()
	}

	return nil
}

func (t *Throttler) fill(freq time.Duration) {
	ticker := time.NewTicker(freq)
	defer ticker.Stop()

	for _ = range ticker.C {
		select {
		case <-t.closing:
			return
		default:
		}
		t.mu.RLock()
		for _, b := range t.buckets {
			b.Put(b.inc)
		}
		t.mu.RUnlock()
	}
}