/
cats.go
299 lines (240 loc) · 6.09 KB
/
cats.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package libcats
import (
"fmt"
_ "image/gif"
_ "image/jpeg"
"io/ioutil"
"net/http"
"runtime"
"strings"
"sync"
"time"
"encoding/json"
"github.com/gregjones/httpcache"
"github.com/gregjones/httpcache/diskcache"
"github.com/peterbourgon/diskv"
)
type ImageMetaData struct {
Url string
Title string
Author string
Permalink string
Id string
}
type MetaDataCallback interface {
MetaDataReceived(url string, title string, author string, permalink string, id string)
}
var transport *httpcache.Transport
var loggerFunc func(string, ...interface{}) = func(format string, args ...interface{}) {
fmt.Printf("%s\n", fmt.Sprintf(format, args...))
}
func Init(cachePath string) {
runtime.GOMAXPROCS(runtime.NumCPU())
transport = newTransportWithDiskCache(cachePath)
}
type CallbackToken struct {
isLoadingMux *sync.Mutex
wg sync.WaitGroup
done chan struct{}
nextBatch chan struct{}
isLoading bool
metaDataCallback MetaDataCallback
}
func (token *CallbackToken) LoadNextBatch() {
token.isLoadingMux.Lock()
if !token.isLoading {
token.isLoading = true
token.isLoadingMux.Unlock()
token.nextBatch <- struct{}{}
} else {
token.isLoadingMux.Unlock()
}
}
func (token *CallbackToken) Close() {
close(token.done)
token.wg.Wait()
loggerFunc("callback closed.")
}
func CreateMetaDataCallback(callback MetaDataCallback, lastPost string) *CallbackToken {
token := &CallbackToken{
metaDataCallback: callback,
done: make(chan struct{}),
nextBatch: make(chan struct{}),
wg: sync.WaitGroup{},
isLoading: false,
isLoadingMux: &sync.Mutex{},
}
go produceCatUrls(token, lastPost)
loggerFunc("started produceCatUrls, GOMAXPROCS %d", runtime.GOMAXPROCS(-1))
return token
}
func produceCatUrls(token *CallbackToken, lastPost string) {
token.wg.Add(1)
defer token.wg.Done()
loggerFunc("starting produceCatUrls")
backoff := 100
for {
token.isLoadingMux.Lock()
token.isLoading = true
token.isLoadingMux.Unlock()
response, ok := <-getCats(lastPost, token.done)
if !ok {
// channel was closed
return
}
if response.Error != nil {
loggerFunc("failed to produceCatUrls. " + response.Error.Error())
select {
case <-token.done:
return
case <-time.After(time.Duration(backoff) * time.Millisecond):
backoff *= 2
loggerFunc("increased backoff to %d ms", backoff)
continue
}
}
backoff = 100
for _, v := range response.Response.Data.Children {
if v.Kind == "t3" && v.Data.Url != "" {
url := v.Data.Url
// sometimes the url is missing an extension
dotIndex := strings.LastIndex(url, ".")
if dotIndex == -1 || dotIndex < strings.LastIndex(url, "/") {
url = url + ".jpg"
}
select {
case <-token.done:
return
default:
start := time.Now()
permalink := "http://www.reddit.com/" + v.Data.Permalink
token.metaDataCallback.MetaDataReceived(url, v.Data.Title, v.Data.Author, permalink, v.Data.Name)
lastPost = v.Data.Name
loggerFunc("%s took %s", " <- MetaDataReceived", time.Since(start))
}
}
}
token.isLoadingMux.Lock()
token.isLoading = false
token.isLoadingMux.Unlock()
select {
case <-token.done:
return
case <-token.nextBatch:
continue
}
}
}
type redditResponseOrError struct {
Response *redditResponseDto
Error error
}
func getCats(afterPost string, done chan struct{}) chan redditResponseOrError {
returnChan := make(chan redditResponseOrError)
go func() {
defer close(returnChan)
url := "http://www.reddit.com/r/aww.json"
if afterPost != "" {
url += "?after=" + afterPost
}
boe, ok := <-downloadBytes(url, done)
if !ok {
return
}
if boe.Error != nil {
select {
case <-done:
case returnChan <- redditResponseOrError{Error: boe.Error}:
}
return
}
redditResponse := redditResponseDto{}
if err := json.Unmarshal(boe.Bytes, &redditResponse); err != nil {
select {
case <-done:
case returnChan <- redditResponseOrError{Error: boe.Error}:
}
return
}
select {
case <-done:
case returnChan <- redditResponseOrError{Response: &redditResponse}:
}
}()
return returnChan
}
type bytesOrError struct {
Bytes []byte
Error error
}
func downloadBytes(url string, done chan struct{}) chan bytesOrError {
returnChan := make(chan bytesOrError, 1)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
defer close(returnChan)
returnChan <- bytesOrError{Error: err}
return returnChan
}
go func() {
start := time.Now()
defer close(returnChan)
result := bytesOrError{
Bytes: nil,
Error: nil,
}
resp, err := getClient().Do(req)
if err != nil {
result.Error = err
returnChan <- result
return
}
defer resp.Body.Close()
fromCache := "NO"
if resp.Header.Get("X-From-Cache") == "1" {
fromCache = "YES"
}
defer timeIt(start, fmt.Sprintf("downloading %s from cache: %s", url, fromCache))
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
result.Error = err
} else {
result.Bytes = body
}
returnChan <- result
}()
// The http request funnels though the second goroutine
// so we can return when the cancellation doesn't happen
returnChan2 := make(chan bytesOrError, 1)
go func() {
defer close(returnChan2)
select {
case bytesOrError, ok := <-returnChan:
if !ok {
return
} else {
returnChan2 <- bytesOrError
}
case <-done:
loggerFunc("trying to cancel request")
if tr, ok := http.DefaultTransport.(*http.Transport); ok {
loggerFunc("trying to cancel request 2")
tr.CancelRequest(req)
loggerFunc("successfully canceled request")
}
}
}()
return returnChan2
}
func newTransportWithDiskCache(basePath string) *httpcache.Transport {
d := diskv.New(diskv.Options{
BasePath: basePath,
CacheSizeMax: 100 * 1024 * 10, // 10MB
})
c := diskcache.NewWithDiskv(d)
return httpcache.NewTransport(c)
}
func getClient() *http.Client {
c := transport.Client()
//c.Timeout = time.Duration(30 * time.Second) //TODO Client Transport of type *httpcache.Transport doesn't support CanelRequest; Timeout not supported
return c
}