| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package faketime |
| 17 |
|
| 18 |
import ( |
| 19 |
"container/heap" |
| 20 |
"fmt" |
| 21 |
"sync" |
| 22 |
"time" |
| 23 |
|
| 24 |
"inet.af/netstack/tcpip" |
| 25 |
) |
| 26 |
|
| 27 |
|
| 28 |
type NullClock struct{} |
| 29 |
|
| 30 |
var _ tcpip.Clock = (*NullClock)(nil) |
| 31 |
|
| 32 |
|
| 33 |
func (*NullClock) Now() time.Time { |
| 34 |
return time.Time{} |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
func (*NullClock) NowMonotonic() tcpip.MonotonicTime { |
| 39 |
return tcpip.MonotonicTime{} |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
func (*NullClock) AfterFunc(time.Duration, func()) tcpip.Timer { |
| 44 |
return nil |
| 45 |
} |
| 46 |
|
| 47 |
type notificationChannels struct { |
| 48 |
mu struct { |
| 49 |
sync.Mutex |
| 50 |
|
| 51 |
ch []<-chan struct{} |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
func (n *notificationChannels) add(ch <-chan struct{}) { |
| 56 |
n.mu.Lock() |
| 57 |
defer n.mu.Unlock() |
| 58 |
n.mu.ch = append(n.mu.ch, ch) |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
func (n *notificationChannels) wait() { |
| 66 |
for { |
| 67 |
n.mu.Lock() |
| 68 |
ch := n.mu.ch |
| 69 |
n.mu.ch = nil |
| 70 |
n.mu.Unlock() |
| 71 |
|
| 72 |
if len(ch) == 0 { |
| 73 |
break |
| 74 |
} |
| 75 |
|
| 76 |
for _, c := range ch { |
| 77 |
<-c |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
type ManualClock struct { |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
runningTimers notificationChannels |
| 89 |
|
| 90 |
mu struct { |
| 91 |
sync.RWMutex |
| 92 |
|
| 93 |
|
| 94 |
now time.Time |
| 95 |
|
| 96 |
|
| 97 |
times timeHeap |
| 98 |
|
| 99 |
|
| 100 |
timers map[time.Time]map[*manualTimer]struct{} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
func NewManualClock() *ManualClock { |
| 106 |
c := &ManualClock{} |
| 107 |
|
| 108 |
c.mu.Lock() |
| 109 |
defer c.mu.Unlock() |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
c.mu.now = time.Unix(0, 0) |
| 114 |
c.mu.timers = make(map[time.Time]map[*manualTimer]struct{}) |
| 115 |
|
| 116 |
return c |
| 117 |
} |
| 118 |
|
| 119 |
var _ tcpip.Clock = (*ManualClock)(nil) |
| 120 |
|
| 121 |
|
| 122 |
func (mc *ManualClock) Now() time.Time { |
| 123 |
mc.mu.RLock() |
| 124 |
defer mc.mu.RUnlock() |
| 125 |
return mc.mu.now |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
func (mc *ManualClock) NowMonotonic() tcpip.MonotonicTime { |
| 130 |
var mt tcpip.MonotonicTime |
| 131 |
return mt.Add(mc.Now().Sub(time.Unix(0, 0))) |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
func (mc *ManualClock) AfterFunc(d time.Duration, f func()) tcpip.Timer { |
| 136 |
mt := &manualTimer{ |
| 137 |
clock: mc, |
| 138 |
f: f, |
| 139 |
} |
| 140 |
|
| 141 |
mc.mu.Lock() |
| 142 |
defer mc.mu.Unlock() |
| 143 |
|
| 144 |
mt.mu.Lock() |
| 145 |
defer mt.mu.Unlock() |
| 146 |
|
| 147 |
mc.resetTimerLocked(mt, d) |
| 148 |
return mt |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
func (mc *ManualClock) resetTimerLocked(mt *manualTimer, d time.Duration) { |
| 155 |
if !mt.mu.firesAt.IsZero() { |
| 156 |
panic("tried to reset an active timer") |
| 157 |
} |
| 158 |
|
| 159 |
t := mc.mu.now.Add(d) |
| 160 |
|
| 161 |
if !mc.mu.now.Before(t) { |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
ch := make(chan struct{}) |
| 168 |
mc.runningTimers.add(ch) |
| 169 |
|
| 170 |
go func() { |
| 171 |
defer close(ch) |
| 172 |
|
| 173 |
mt.f() |
| 174 |
}() |
| 175 |
|
| 176 |
return |
| 177 |
} |
| 178 |
|
| 179 |
mt.mu.firesAt = t |
| 180 |
|
| 181 |
timers, ok := mc.mu.timers[t] |
| 182 |
if !ok { |
| 183 |
timers = make(map[*manualTimer]struct{}) |
| 184 |
mc.mu.timers[t] = timers |
| 185 |
heap.Push(&mc.mu.times, t) |
| 186 |
} |
| 187 |
|
| 188 |
timers[mt] = struct{}{} |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
func (mc *ManualClock) stopTimerLocked(mt *manualTimer) { |
| 195 |
t := mt.mu.firesAt |
| 196 |
mt.mu.firesAt = time.Time{} |
| 197 |
|
| 198 |
if t.IsZero() { |
| 199 |
panic("tried to stop an inactive timer") |
| 200 |
} |
| 201 |
|
| 202 |
timers, ok := mc.mu.timers[t] |
| 203 |
if !ok { |
| 204 |
err := fmt.Sprintf("tried to stop an active timer but the clock does not have anything scheduled for the timer @ t = %s %p\nScheduled timers @:", t.UTC(), mt) |
| 205 |
for t := range mc.mu.timers { |
| 206 |
err += fmt.Sprintf("%s\n", t.UTC()) |
| 207 |
} |
| 208 |
panic(err) |
| 209 |
} |
| 210 |
|
| 211 |
if _, ok := timers[mt]; !ok { |
| 212 |
panic(fmt.Sprintf("did not have an entry in timers for an active timer @ t = %s", t.UTC())) |
| 213 |
} |
| 214 |
|
| 215 |
delete(timers, mt) |
| 216 |
|
| 217 |
if len(timers) == 0 { |
| 218 |
delete(mc.mu.timers, t) |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
func (mc *ManualClock) RunImmediatelyScheduledJobs() { |
| 225 |
mc.Advance(0) |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
func (mc *ManualClock) Advance(d time.Duration) { |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
mc.runningTimers.wait() |
| 235 |
|
| 236 |
mc.mu.Lock() |
| 237 |
defer mc.mu.Unlock() |
| 238 |
|
| 239 |
until := mc.mu.now.Add(d) |
| 240 |
for mc.mu.times.Len() > 0 { |
| 241 |
t := heap.Pop(&mc.mu.times).(time.Time) |
| 242 |
if t.After(until) { |
| 243 |
|
| 244 |
heap.Push(&mc.mu.times, t) |
| 245 |
break |
| 246 |
} |
| 247 |
|
| 248 |
timers := mc.mu.timers[t] |
| 249 |
delete(mc.mu.timers, t) |
| 250 |
|
| 251 |
mc.mu.now = t |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
for mt := range timers { |
| 261 |
mt.mu.Lock() |
| 262 |
mt.mu.firesAt = time.Time{} |
| 263 |
mt.mu.Unlock() |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
mc.mu.Unlock() |
| 270 |
|
| 271 |
for mt := range timers { |
| 272 |
mt.f() |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
mc.runningTimers.wait() |
| 280 |
mc.mu.Lock() |
| 281 |
} |
| 282 |
|
| 283 |
mc.mu.now = until |
| 284 |
} |
| 285 |
|
| 286 |
func (mc *ManualClock) resetTimer(mt *manualTimer, d time.Duration) { |
| 287 |
mc.mu.Lock() |
| 288 |
defer mc.mu.Unlock() |
| 289 |
|
| 290 |
mt.mu.Lock() |
| 291 |
defer mt.mu.Unlock() |
| 292 |
|
| 293 |
if !mt.mu.firesAt.IsZero() { |
| 294 |
mc.stopTimerLocked(mt) |
| 295 |
} |
| 296 |
|
| 297 |
mc.resetTimerLocked(mt, d) |
| 298 |
} |
| 299 |
|
| 300 |
func (mc *ManualClock) stopTimer(mt *manualTimer) bool { |
| 301 |
mc.mu.Lock() |
| 302 |
defer mc.mu.Unlock() |
| 303 |
|
| 304 |
mt.mu.Lock() |
| 305 |
defer mt.mu.Unlock() |
| 306 |
|
| 307 |
if mt.mu.firesAt.IsZero() { |
| 308 |
return false |
| 309 |
} |
| 310 |
|
| 311 |
mc.stopTimerLocked(mt) |
| 312 |
return true |
| 313 |
} |
| 314 |
|
| 315 |
type manualTimer struct { |
| 316 |
clock *ManualClock |
| 317 |
f func() |
| 318 |
|
| 319 |
mu struct { |
| 320 |
sync.Mutex |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
firesAt time.Time |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
var _ tcpip.Timer = (*manualTimer)(nil) |
| 330 |
|
| 331 |
|
| 332 |
func (mt *manualTimer) Reset(d time.Duration) { |
| 333 |
mt.clock.resetTimer(mt, d) |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
func (mt *manualTimer) Stop() bool { |
| 338 |
return mt.clock.stopTimer(mt) |
| 339 |
} |
| 340 |
|
| 341 |
type timeHeap []time.Time |
| 342 |
|
| 343 |
var _ heap.Interface = (*timeHeap)(nil) |
| 344 |
|
| 345 |
func (h timeHeap) Len() int { |
| 346 |
return len(h) |
| 347 |
} |
| 348 |
|
| 349 |
func (h timeHeap) Less(i, j int) bool { |
| 350 |
return h[i].Before(h[j]) |
| 351 |
} |
| 352 |
|
| 353 |
func (h timeHeap) Swap(i, j int) { |
| 354 |
h[i], h[j] = h[j], h[i] |
| 355 |
} |
| 356 |
|
| 357 |
func (h *timeHeap) Push(x interface{}) { |
| 358 |
*h = append(*h, x.(time.Time)) |
| 359 |
} |
| 360 |
|
| 361 |
func (h *timeHeap) Pop() interface{} { |
| 362 |
last := (*h)[len(*h)-1] |
| 363 |
*h = (*h)[:len(*h)-1] |
| 364 |
return last |
| 365 |
} |