-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtracker.go
61 lines (50 loc) · 1.12 KB
/
tracker.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
54
55
56
57
58
59
60
61
package multilimiter
import (
"sync"
"sync/atomic"
"time"
)
type ConcurrencyTracker struct {
current int32
total int32
max int32
locker sync.Mutex
started time.Time
elapsed time.Duration
}
func (me *ConcurrencyTracker) Start() {
// atomic.StoreInt32(&me.total, 1)
me.started = time.Now()
}
func (me *ConcurrencyTracker) Stop() {
me.elapsed = time.Now().Sub(me.started)
}
func (me *ConcurrencyTracker) Elapsed() time.Duration {
return me.elapsed
}
func (me *ConcurrencyTracker) Add() {
me.locker.Lock()
newMax := atomic.AddInt32(&me.current, 1)
atomic.AddInt32(&me.total, 1)
if newMax > me.max {
me.max = newMax
}
me.locker.Unlock()
}
func (me *ConcurrencyTracker) Subtract() {
me.locker.Lock()
atomic.AddInt32(&me.current, -1)
me.locker.Unlock()
}
func (me *ConcurrencyTracker) Current() int32 {
return atomic.LoadInt32(&me.current)
}
func (me *ConcurrencyTracker) Total() int32 {
return atomic.LoadInt32(&me.total)
}
func (me *ConcurrencyTracker) Max() int32 {
return atomic.LoadInt32(&me.max)
}
func (me *ConcurrencyTracker) Rate() float64 {
return float64(me.Total()) / me.Elapsed().Seconds()
}