2019-06-23 05:21:30 +03:00
|
|
|
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
2019-06-22 22:51:07 +03:00
|
|
|
import http
|
|
|
|
import json
|
|
|
|
import sync
|
|
|
|
import time
|
|
|
|
|
|
|
|
const (
|
|
|
|
NR_THREADS = 8
|
|
|
|
)
|
|
|
|
|
|
|
|
struct Story {
|
|
|
|
title string
|
2019-06-24 18:16:01 +03:00
|
|
|
url string
|
2019-06-22 22:51:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Fetcher {
|
|
|
|
mut:
|
|
|
|
mu sync.Mutex
|
|
|
|
ids []int
|
|
|
|
cursor int
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (f mut Fetcher) fetch() {
|
|
|
|
for {
|
|
|
|
f.mu.lock()
|
|
|
|
if f.cursor >= f.ids.len {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
id := f.ids[f.cursor]
|
|
|
|
f.cursor++
|
|
|
|
f.mu.unlock()
|
|
|
|
resp := http.get('https://hacker-news.firebaseio.com/v0/item/${id}.json')
|
|
|
|
story := json.decode(Story, resp) or {
|
2019-06-23 11:41:42 +03:00
|
|
|
println('failed to decode a story')
|
|
|
|
exit(1)
|
2019-06-22 22:51:07 +03:00
|
|
|
}
|
2019-06-24 18:16:01 +03:00
|
|
|
println('#$f.cursor) $story.title | $story.url')
|
2019-06-22 22:51:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetches top HN stories in 8 coroutines
|
|
|
|
fn main() {
|
|
|
|
resp := http.get('https://hacker-news.firebaseio.com/v0/topstories.json')
|
|
|
|
ids := json.decode( []int, resp) or {
|
|
|
|
println('failed to fetch topstories.json')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fetcher := &Fetcher{ids: ids}
|
|
|
|
for i := 0; i < NR_THREADS; i++ {
|
|
|
|
go fetcher.fetch()
|
|
|
|
}
|
|
|
|
println(fetcher.ids)
|
2019-06-27 20:02:47 +03:00
|
|
|
time.sleep(5)
|
2019-06-22 22:51:07 +03:00
|
|
|
}
|
|
|
|
|