forked from sachaos/viddy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.go
79 lines (56 loc) · 1.33 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
package main
import "time"
type newSnapFunc func(int64, *Snapshot, chan<- struct{}) *Snapshot
func ClockSnapshot(begin int64, newSnap newSnapFunc, interval time.Duration) <-chan *Snapshot {
c := make(chan *Snapshot)
go func() {
var s *Snapshot
t := time.Tick(interval)
for now := range t {
finish := make(chan struct{})
id := (now.UnixNano() - begin) / int64(time.Millisecond)
s = newSnap(id, s, finish)
c <- s
}
}()
return c
}
func PreciseSnapshot(newSnap newSnapFunc, interval time.Duration) <-chan *Snapshot {
c := make(chan *Snapshot)
go func() {
var s *Snapshot
begin := time.Now().UnixNano()
for {
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
}
func SequentialSnapshot(newSnap newSnapFunc, interval time.Duration) <-chan *Snapshot {
c := make(chan *Snapshot)
go func() {
var s *Snapshot
begin := time.Now().UnixNano()
for {
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
}