-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdurations.go
More file actions
43 lines (35 loc) · 782 Bytes
/
durations.go
File metadata and controls
43 lines (35 loc) · 782 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
package main
import (
"sync"
"time"
)
type DurationItem struct {
Latency int64 //unixnano
Command string
Args string
}
func (d DurationItem) ToLatency() time.Duration {
return time.Nanosecond * time.Duration(time.Now().UnixNano()-d.Latency)
}
type Durations struct {
m sync.Mutex
list map[uint32]DurationItem
}
func NewDurations() *Durations {
return &Durations{list: make(map[uint32]DurationItem)}
}
func (d *Durations) Set(k uint32, command, args string) {
d.m.Lock()
defer d.m.Unlock()
d.list[k] = DurationItem{time.Now().UnixNano(), command, args}
}
func (d *Durations) Get(k uint32) (item DurationItem, exist bool) {
d.m.Lock()
defer d.m.Unlock()
item, exist = d.list[k]
if !exist {
return item, exist
}
delete(d.list, k)
return item, true
}