-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeouttable.go
108 lines (88 loc) · 1.85 KB
/
timeouttable.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package pinger
import (
"errors"
"net"
"sync"
"time"
)
type key struct {
ip [4]byte
seq int
}
type entry struct {
timer *time.Timer
done chan<- struct{}
}
type timeoutTable struct {
mux sync.Mutex
m map[key]entry
timeout time.Duration
handlerFunc timeoutHandlerFunc
}
type timeoutHandlerFunc func(ip net.IP, seq int)
func newTimeoutTable(timeout time.Duration,
handler timeoutHandlerFunc) (*timeoutTable, error) {
if timeout <= 0 {
return nil, errors.New("timeout must be greater than zero")
}
return &timeoutTable{
timeout: timeout,
handlerFunc: handler,
m: make(map[key]entry, 512),
}, nil
}
func (tt *timeoutTable) add(ip net.IP, seq int) error {
done := make(chan struct{})
key := makeKey(ip, seq)
timer := time.NewTimer(tt.timeout)
entry := entry{timer, done}
tt.mux.Lock()
tt.m[key] = entry
tt.mux.Unlock()
go func(done <-chan struct{}) {
select {
case <-timer.C: // The retrieval timed out
tt.mux.Lock()
delete(tt.m, key)
tt.mux.Unlock()
if tt.handlerFunc != nil {
tt.handlerFunc(ip, seq)
}
case <-done: // The item was retrieved and timeout-processing is not req'd
return
}
}(done)
return nil
}
func (tt *timeoutTable) remove(ip net.IP, seq int) bool {
key := makeKey(ip, seq)
return tt.removeKey(key)
}
func (tt *timeoutTable) removeKey(key key) bool {
tt.mux.Lock()
entry, ok := tt.m[key]
if !ok {
tt.mux.Unlock()
return false
}
delete(tt.m, key)
tt.mux.Unlock()
// Cancel timeout-processing goroutine
close(entry.done)
// Cancel timeout
if !entry.timer.Stop() {
<-entry.timer.C
}
return true
}
func (tt *timeoutTable) cleardown() {
for key := range tt.m {
tt.removeKey(key)
}
}
func makeKey(ip net.IP, seq int) key {
ipv4 := ip.To4()
ipv4Array := [4]byte{ipv4[0], ipv4[1], ipv4[2], ipv4[3]}
key := key{ipv4Array, seq}
return key
}