forked from zhravan/golearn
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtime_delay.go
More file actions
90 lines (74 loc) · 2.18 KB
/
Copy pathtime_delay.go
File metadata and controls
90 lines (74 loc) · 2.18 KB
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
package timedelay
// Real implementations using time.Now, time.Add and comparisons for the exercise.
import (
"errors"
"time"
)
// WaitFor pauses execution until roughly ms milliseconds have passed.
func WaitFor(ms int) {
target := time.Now().Add(time.Duration(ms) * time.Millisecond)
for time.Now().Before(target) {
time.Sleep(1 * time.Millisecond)
}
}
// NotifyAfter returns a channel that receives true after roughly ms milliseconds.
func NotifyAfter(ms int) chan bool {
ch := make(chan bool, 1)
target := time.Now().Add(time.Duration(ms) * time.Millisecond)
go func() {
for time.Now().Before(target) {
time.Sleep(1 * time.Millisecond)
}
ch <- true
}()
return ch
}
// WaitUntil blocks until the provided target time is reached (returns immediately if in the past).
func WaitUntil(target time.Time) {
for time.Now().Before(target) {
time.Sleep(1 * time.Millisecond)
}
}
// NotifyAt returns a channel that receives true when target time is reached (sends immediately if in the past).
func NotifyAt(target time.Time) chan bool {
ch := make(chan bool, 1)
go func() {
for time.Now().Before(target) {
time.Sleep(1 * time.Millisecond)
}
ch <- true
}()
return ch
}
// ElapsedMillis returns how many milliseconds have elapsed since t.
func ElapsedMillis(t time.Time) int64 {
return time.Since(t).Milliseconds()
}
// WaitForOrTimeout waits up to ms milliseconds but fails if total waiting exceeds timeoutMs.
func WaitForOrTimeout(ms int, timeoutMs int) error {
target := time.Now().Add(time.Duration(ms) * time.Millisecond)
deadline := time.Now().Add(time.Duration(timeoutMs) * time.Millisecond)
for {
now := time.Now()
if !now.Before(target) {
return nil
}
if !now.Before(deadline) {
return errors.New("timeout exceeded before target reached")
}
time.Sleep(1 * time.Millisecond)
}
}
// ScheduleAfter runs fn after roughly ms milliseconds and returns a channel closed when fn finishes.
func ScheduleAfter(ms int, fn func()) chan struct{} {
done := make(chan struct{})
target := time.Now().Add(time.Duration(ms) * time.Millisecond)
go func() {
for time.Now().Before(target) {
time.Sleep(1 * time.Millisecond)
}
fn()
close(done)
}()
return done
}