forked from pokornyIt/cucm_performance_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperformace_clien_rate.go
50 lines (44 loc) · 966 Bytes
/
performace_clien_rate.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
package main
import (
"sync"
"time"
)
const (
RateStandardDelay = time.Second + time.Millisecond*200
RateStandardTestDelay = RateStandardDelay + time.Millisecond*300
RateBaseWaitTime = time.Minute + time.Millisecond*200
RateRequestLimit = 50
)
type RateControl struct {
start time.Time
requests int
mutex sync.Mutex
}
func (r *RateControl) add() {
r.mutex.Lock()
defer r.mutex.Unlock()
r.requests++
}
func (r *RateControl) reset() {
r.mutex.Lock()
defer r.mutex.Unlock()
r.start = time.Now()
r.requests = 0
}
func (r *RateControl) delay() time.Duration {
defer r.add()
timePeriod := time.Now().Sub(r.start)
waitTime := RateBaseWaitTime - timePeriod
if r.requests < RateRequestLimit {
if timePeriod > RateStandardTestDelay*time.Duration(r.requests) {
r.reset()
return time.Millisecond
}
return RateStandardDelay
}
if timePeriod > time.Minute {
waitTime = time.Millisecond
}
r.reset()
return waitTime
}