This repository has been archived by the owner on Dec 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
trace.go
81 lines (68 loc) · 1.68 KB
/
trace.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package env
import (
"fmt"
cmap "github.com/orcaman/concurrent-map/v2"
"time"
)
func NewTraceManager(shutdownNotify <-chan struct{}) *TraceManager {
result := &TraceManager{
traceIdentities: cmap.New[*TraceSpec](),
shutdownNotify: shutdownNotify,
}
go result.reapExpired()
return result
}
type TraceSpec struct {
Until time.Time
TraceId string
ChannelMask uint32
}
func (self *TraceSpec) String() string {
return fmt.Sprintf("traceId=%v until=%v", self.TraceId, self.Until)
}
type TraceManager struct {
traceIdentities cmap.ConcurrentMap[string, *TraceSpec]
shutdownNotify <-chan struct{}
}
func (self *TraceManager) GetIdentityTrace(identityId string) *TraceSpec {
spec, found := self.traceIdentities.Get(identityId)
if !found {
return nil
}
specCopy := *spec
return &specCopy
}
func (self *TraceManager) TraceIdentity(identity string, duration time.Duration, id string, channelMask uint32) *TraceSpec {
spec := &TraceSpec{
Until: time.Now().Add(duration),
TraceId: id,
ChannelMask: channelMask,
}
self.traceIdentities.Set(identity, spec)
specCopy := *spec
return &specCopy
}
func (self *TraceManager) RemoveIdentityTrace(identity string) {
self.traceIdentities.Remove(identity)
}
func (self *TraceManager) reapExpired() {
ticker := time.NewTicker(1 * time.Minute)
defer ticker.Stop()
for {
select {
case <-ticker.C:
now := time.Now()
var toRemove []string
self.traceIdentities.IterCb(func(key string, spec *TraceSpec) {
if spec.Until.Before(now) {
toRemove = append(toRemove, key)
}
})
for _, key := range toRemove {
self.traceIdentities.Remove(key)
}
case <-self.shutdownNotify:
return
}
}
}