-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathroundrobin.go
More file actions
49 lines (40 loc) · 768 Bytes
/
roundrobin.go
File metadata and controls
49 lines (40 loc) · 768 Bytes
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
package tmdb
import (
"sync"
)
// RoundRobin struct
type RoundRobin struct {
currentTicker int
maxAllowed int
mu sync.Mutex
}
// InitRoundRobin func
func InitRoundRobin(maxAllowed int) RoundRobin {
return RoundRobin{
maxAllowed: maxAllowed,
currentTicker: 0,
mu: sync.Mutex{},
}
}
// GetTicker func
func (r *RoundRobin) GetTicker() int {
r.mu.Lock()
defer r.mu.Unlock()
ticker := r.currentTicker
if r.currentTicker < r.maxAllowed {
r.currentTicker = r.currentTicker + 1
} else {
r.currentTicker = 0
}
return ticker
}
// Next func
func (r *RoundRobin) Next() {
r.mu.Lock()
defer r.mu.Unlock()
if r.currentTicker < r.maxAllowed {
r.currentTicker = r.currentTicker + 1
} else {
r.currentTicker = 0
}
}