/
Expirer.go
216 lines (186 loc) · 6.39 KB
/
Expirer.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package icmpengine
// Expirer holds a sinlge sleep timer
// Socketsider using heap, rather than ordered DLL, which would allow for different timeout values
// https://golang.org/pkg/container/heap/
// Notes
// Currently have a single expirer, but could easily have one per protocol which would provide more parallelism
// The other suggestion is to make a single expiry for a batch of packets, rather than one per packet.
// Both these ideas could help if performance becomes an issue, which currnetly it is not.
import (
"container/list"
"fmt"
"time"
)
const (
EdebugLevel = 111
)
// CheckExpirerIsRunning checks Expirers is running, and starts it if required
// returns if Expirers was started
// CheckExpirerIsRunning assumes the LOCK is already held by Pinger
func (ie *ICMPEngine) CheckExpirerIsRunning() (started bool) {
if ie.Expirers.DebugLevel > 100 {
ie.Log.Info("CheckExpirerIsRunning() start")
}
if ie.Expirers.Running {
if ie.Expirers.DebugLevel > 100 {
ie.Log.Info("CheckExpirerIsRunning ie.Expirers.Running")
}
started = false
} else {
ie.Expirers.WG.Add(1)
go ie.ExpirerConfig(ie.Expirers.FakeSuccess)
ie.Expirers.Running = true
if ie.Expirers.DebugLevel > 100 {
ie.Log.Info("CheckExpirerIsRunning started")
}
started = true
}
return started
}
// Expirer tracks the ICMP echo timeouts
// The idea is to just have the single and nearest timer running at any single moment
// The "Config" implies that we can configure the FakeSuccess, which is used for testing
func (ie *ICMPEngine) ExpirerConfig(FakeSuccess bool) {
if ie.Expirers.DebugLevel > 100 {
ie.Log.Info(fmt.Sprintf("Expirer start \t FakeSuccess:%t", FakeSuccess))
}
defer ie.Expirers.WG.Done()
ie.RLock()
done := ie.Expirers.DoneCh
ie.RUnlock()
for i, keepLooping := 0, true; keepLooping; i++ {
var el *list.Element
var exists bool
var len int
var SoonestPing Pings
var sleepDuration time.Duration
if ie.Expirers.DebugLevel > 100 {
ie.Log.Info(fmt.Sprintf("Expirer \t i:%d", i))
}
select {
case <-done:
if ie.Expirers.DebugLevel > 10 {
ie.Log.Info("Expirer received done")
}
keepLooping = false
continue
default:
// non-block
}
if ie.Expirers.DebugLevel > 100 {
ie.Log.Info(fmt.Sprintf("Expirer trying to acquire ie.Lock() to check len\t i:%d", i))
}
ie.Lock() // <-------------------------- LOCK!!
len = ie.Pingers.ExpiresDLL.Len()
if len == 0 {
ie.Expirers.Running = false
ie.Unlock() // <-------------------- UNLOCK!!
if ie.Expirers.DebugLevel > 100 {
ie.Log.Info(fmt.Sprintf("Expirer ie.Unlock(). No more elements in expires list, len:%d. Returning", len))
}
keepLooping = false
return
}
el = ie.Pingers.ExpiresDLL.Front()
SoonestPing = copyPing(ie.Pingers.ExpiresDLL.Front())
if FakeSuccess && !SoonestPing.FakeDrop {
if ie.Expirers.DebugLevel > 100 {
ie.Log.Info("Expirer FakeSuccess doing delete")
}
successCh := ie.Pingers.SuccessChs[SoonestPing.NetaddrIP]
ie.Pingers.ExpiresDLL.Remove(el)
delete(ie.Pingers.Pings[SoonestPing.NetaddrIP], SoonestPing.Seq)
ie.Unlock() // <-------------------- UNLOCK!!
if ie.Expirers.DebugLevel > 100 {
ie.Log.Info("Expirer FakeSuccess ie.Unlock()")
}
fakeReceivedTime := time.Now()
rttDuration := fakeReceivedTime.Sub(SoonestPing.Send)
successCh <- PingSuccess{
Seq: SoonestPing.Seq,
Send: SoonestPing.Send,
Received: fakeReceivedTime,
RTT: rttDuration,
}
if ie.Expirers.DebugLevel > 100 {
ie.Log.Info(fmt.Sprintf("Expirer \t i:%d Sent <- PingSuccess FakeSuccess", i))
}
continue
}
ie.Unlock() // <------------------------ UNLOCK!!
sleepDuration = time.Until(SoonestPing.Expiry)
if ie.Expirers.DebugLevel > 1000 {
ie.Log.Info(fmt.Sprintf("Expirer \t i:%d going to sleep duration:%s", i, sleepDuration.String()))
}
select {
case <-time.After(sleepDuration):
if ie.Expirers.DebugLevel > 100 {
ie.Log.Info(fmt.Sprintf("Expirer wakes up after duration:%s", sleepDuration.String()))
}
case <-done:
if ie.Expirers.DebugLevel > 10 {
ie.Log.Info("Expirer was sleeping, but received done")
}
keepLooping = false
// NO DEFAULT - This is BLOCKING
//default:
}
if ie.Expirers.DebugLevel > 100 {
ie.Log.Info("Expirer trying to acquire ie.RLock() to check exists")
}
ie.RLock() // <-------------------------- READ LOCK!!
el, exists = ie.Pingers.Pings[SoonestPing.NetaddrIP][SoonestPing.Seq]
ie.RUnlock() // <------------------------ READ UNLOCK!!
if ie.Expirers.DebugLevel > 100 {
ie.Log.Info(fmt.Sprintf("Expirer ie.RUnlock(), exists:%t", exists))
}
// If the key still exists, then the Receiver did NOT get a return packet, so the timeout has expired
if exists {
if ie.Expirers.DebugLevel > 100 {
ie.Log.Info(fmt.Sprintf("Expirer found expired \t IP:%s \t Seq:%d deleting", SoonestPing.NetaddrIP.String(), SoonestPing.Seq))
}
if ie.Expirers.DebugLevel > 100 {
ie.Log.Info("Expirer exists - trying to acquire ie.Lock()")
}
ie.Lock() // <----------------------- LOCK!!
delete(ie.Pingers.Pings[SoonestPing.NetaddrIP], SoonestPing.Seq)
ie.Pingers.ExpiresDLL.Remove(el)
expiredCh := ie.Pingers.ExpiredChs[SoonestPing.NetaddrIP]
ie.Unlock() // <--------------------- UNLOCK!!
if ie.Expirers.DebugLevel > 100 {
ie.Log.Info("Expirer exists - ie.Unlock()")
}
expiredCh <- PingExpired{
Seq: SoonestPing.Seq,
Send: SoonestPing.Send,
}
if ie.Expirers.DebugLevel > 100 {
ie.Log.Info(fmt.Sprintf("Expirer \t i:%d Sent <- PingExpired", i))
}
} else {
if ie.Expirers.DebugLevel > 100 {
ie.Log.Info("Expirer expiry no longer exists, so we must have received a response. Excellent.")
}
}
}
if ie.Expirers.DebugLevel > 100 {
ie.Log.Info("Expirer - trying to acquire ie.Lock() to ie.Expirers.Running = false, Defer unlock")
}
ie.Lock()
defer ie.Unlock()
len := ie.Pingers.ExpiresDLL.Len()
ie.Expirers.Running = false
if ie.Expirers.DebugLevel > 100 {
ie.Log.Info(fmt.Sprintf("Expirer len:%d ie.ExpirerRunning = false. Expirer complete. defer ie.Unlock()", len))
}
}
// copyPing is a small helper to copy by value the ping
// this is just to help reduce code in the main function
func copyPing(el *list.Element) (ping Pings) {
ping.NetaddrIP = el.Value.(Pings).NetaddrIP
ping.Seq = el.Value.(Pings).Seq
ping.Send = el.Value.(Pings).Send
ping.Expiry = el.Value.(Pings).Expiry
ping.FakeDrop = el.Value.(Pings).FakeDrop
return ping
}