-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlimiter.go
More file actions
62 lines (53 loc) · 1.36 KB
/
limiter.go
File metadata and controls
62 lines (53 loc) · 1.36 KB
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
/*
* Copyright 2017, Robert Bieber
*
* This file is part of dailyhindsight.
*
* dailyhindsight is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* dailyhindsight is distributed in the hope that it will be useful,
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with dailyhindsight. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"time"
)
func RunLimited(f func(d Dataset)) {
log := make([]time.Time, 0, len(Datasets))
canContinue := func() bool {
countPerLimit := make(map[time.Duration]int, len(Limits))
for k := range Limits {
countPerLimit[k] = 0
}
t := time.Now()
for _, v := range log {
delta := t.Sub(v)
for k := range countPerLimit {
if delta <= k {
countPerLimit[k]++
}
}
}
for k, limit := range Limits {
if countPerLimit[k] >= limit {
return false
}
}
return true
}
for _, v := range Datasets {
for !canContinue() {
time.Sleep(time.Second)
}
f(v)
log = append(log, time.Now())
}
}