-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.go
69 lines (60 loc) · 1.09 KB
/
map.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
package main
import (
"net"
"sync"
"time"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
)
type mkey struct {
ip, port gopacket.Endpoint
}
func newMKey(ip net.IP, port layers.UDPPort) mkey {
return mkey{
ip: layers.NewIPEndpoint(ip),
port: layers.NewUDPPortEndpoint(port),
}
}
type mval struct {
port layers.UDPPort
access int64
}
// TTLmap is a map with item expirety and renewal
// a new map fires goroutine to clean expired items
type TTLmap struct {
m map[mkey]*mval
l sync.Mutex
}
func newTTLmap(ttl time.Duration) (m *TTLmap) {
m = &TTLmap{
m: make(map[mkey]*mval),
}
go func() {
t := time.NewTicker(ttl)
defer t.Stop()
for tick := range t.C {
m.l.Lock()
for k, v := range m.m {
if tick.Unix()-v.access >= int64(ttl) {
delete(m.m, k)
}
}
m.l.Unlock()
}
}()
return
}
func (m *TTLmap) getPort(k mkey) (v *mval, ok bool) {
m.l.Lock()
defer m.l.Unlock()
v, ok = m.m[k]
return
}
func (m *TTLmap) putPort(k mkey, p layers.UDPPort) {
m.l.Lock()
defer m.l.Unlock()
m.m[k] = &mval{
port: p,
access: time.Now().Unix(),
}
}