-
-
Notifications
You must be signed in to change notification settings - Fork 88
/
generator.go
116 lines (85 loc) 路 1.98 KB
/
generator.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
package main
import "time"
type newSnapFunc func(int64, *Snapshot, chan<- struct{}) *Snapshot
func ClockSnapshot(begin int64, newSnap newSnapFunc, interval time.Duration) (<-chan *Snapshot, chan<- bool) {
c := make(chan *Snapshot)
isSuspended := false
isSuspendedQueue := make(chan bool)
go func() {
var s *Snapshot
t := time.Tick(interval)
for now := range t {
select {
case isSuspended = <-isSuspendedQueue:
default:
}
if isSuspended {
continue
}
finish := make(chan struct{})
id := (now.UnixNano() - begin) / int64(time.Millisecond)
s = newSnap(id, s, finish)
c <- s
}
}()
return c, isSuspendedQueue
}
func PreciseSnapshot(newSnap newSnapFunc, interval time.Duration) (<-chan *Snapshot, chan<- bool) {
c := make(chan *Snapshot)
isSuspended := false
isSuspendedQueue := make(chan bool)
go func() {
var s *Snapshot
begin := time.Now().UnixNano()
for {
select {
case isSuspended = <-isSuspendedQueue:
default:
}
if isSuspended {
time.Sleep(interval)
continue
}
finish := make(chan struct{})
start := time.Now()
id := (start.UnixNano() - begin) / int64(time.Millisecond)
ns := newSnap(id, s, finish)
s = ns
c <- ns
<-finish
pTime := time.Since(start)
if pTime > interval {
continue
} else {
time.Sleep(interval - pTime)
}
}
}()
return c, isSuspendedQueue
}
func SequentialSnapshot(newSnap newSnapFunc, interval time.Duration) (<-chan *Snapshot, chan<- bool) {
c := make(chan *Snapshot)
isSuspended := false
isSuspendedQueue := make(chan bool)
go func() {
var s *Snapshot
begin := time.Now().UnixNano()
for {
select {
case isSuspended = <-isSuspendedQueue:
default:
}
if isSuspended {
time.Sleep(interval)
continue
}
finish := make(chan struct{})
id := (time.Now().UnixNano() - begin) / int64(time.Millisecond)
s = newSnap(id, s, finish)
c <- s
<-finish
time.Sleep(interval)
}
}()
return c, isSuspendedQueue
}