-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstream.go
154 lines (142 loc) · 3 KB
/
stream.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
package snoo
import (
"sync"
"time"
)
type CommentStream struct {
client *Client
Comments chan *Comment
done chan struct{}
group *sync.WaitGroup
onlyNew bool
linkID string
}
func (s *CommentStream) Stop() {
close(s.done)
// block until the retry goroutine stops
s.group.Wait()
}
func (s *CommentStream) Start() *CommentStream {
var seenComments []*Comment
go func() {
comments, err := s.client.GetLinkComments(s.linkID)
if err != nil {
s.Stop()
return
}
for _, comment := range comments {
seenComments = append(seenComments, comment)
if !s.onlyNew {
s.Comments <- comment
}
}
ticker := time.NewTicker(1 * time.Second)
for {
select {
case _ = <-ticker.C:
comments, err := s.client.GetLinkComments(s.linkID)
if err != nil {
s.Stop()
return
}
for _, comment := range comments {
isSeen := false
for _, seenComment := range seenComments {
if comment.ID == seenComment.ID {
isSeen = true
}
}
if !isSeen {
seenComments = append(seenComments, comment)
s.Comments <- comment
}
}
case <-s.done:
return
}
}
}()
return s
}
type LinkStream struct {
client *Client
Links chan Link
done chan struct{}
group *sync.WaitGroup
onlyNew bool
subreddit string
}
func (s *LinkStream) Stop() {
close(s.done)
// Scanner does not have a Stop() or take a done channel, so for low volume
// streams Scan() blocks until the next keep-alive. Close the resp.Body to
// escape and stop the stream in a timely fashion.
// block until the retry goroutine stops
s.group.Wait()
}
func (s *LinkStream) Start() *LinkStream {
var seenLinks []Link
go func() {
links, err := s.client.GetNewLinks(s.subreddit)
if err != nil {
s.Stop()
return
}
for _, link := range links {
seenLinks = append(seenLinks, link)
if !s.onlyNew {
s.Links <- link
}
}
ticker := time.NewTicker(1 * time.Second)
for {
select {
case _ = <-ticker.C:
links, err := s.client.GetNewLinks(s.subreddit)
if err != nil {
s.Stop()
return
}
for _, link := range links {
isSeen := false
for _, seenLink := range seenLinks {
if link.ID == seenLink.ID {
isSeen = true
}
}
if !isSeen {
seenLinks = append(seenLinks, link)
s.Links <- link
}
}
case <-s.done:
return
}
}
}()
return s
}
// StreamLinks is a streaming device
func (c *Client) StreamLinks(subreddit string, onlyNew bool) *LinkStream {
s := &LinkStream{
client: c,
Links: make(chan Link),
done: make(chan struct{}),
group: &sync.WaitGroup{},
onlyNew: onlyNew,
subreddit: subreddit,
}
return s
}
// StreamLinkComments is a streaming device
func (c *Client) StreamLinkComments(linkID string, onlyNew bool) *CommentStream {
s := &CommentStream{
client: c,
Comments: make(chan *Comment),
done: make(chan struct{}),
group: &sync.WaitGroup{},
onlyNew: onlyNew,
linkID: linkID,
}
return s
}