forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
limiter.go
53 lines (47 loc) · 1.07 KB
/
limiter.go
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
package engine
import (
"github.com/influxdb/influxdb/protocol"
)
type Limiter struct {
shouldLimit bool
limit int
limits map[string]int
}
func NewLimiter(limit int) *Limiter {
return &Limiter{
limit: limit,
limits: map[string]int{},
shouldLimit: limit > 0,
}
}
func (self *Limiter) calculateLimitAndSlicePoints(series *protocol.Series) {
if self.shouldLimit {
// if the limit is 0, stop returning any points
limit := self.limitForSeries(*series.Name)
defer func() { self.limits[*series.Name] = limit }()
if limit == 0 {
series.Points = nil
return
}
limit -= len(series.Points)
if limit <= 0 {
sliceTo := len(series.Points) + limit
series.Points = series.Points[0:sliceTo]
limit = 0
}
}
}
func (self *Limiter) hitLimit(seriesName string) bool {
if !self.shouldLimit {
return false
}
return self.limitForSeries(seriesName) <= 0
}
func (self *Limiter) limitForSeries(name string) int {
currentLimit, ok := self.limits[name]
if !ok {
currentLimit = self.limit
self.limits[name] = currentLimit
}
return currentLimit
}