2020-01-23 23:04:46 +03:00
|
|
|
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
2019-06-23 05:21:30 +03:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
2019-12-30 07:42:23 +03:00
|
|
|
import net.http
|
2019-06-22 22:51:07 +03:00
|
|
|
import json
|
|
|
|
import sync
|
|
|
|
|
|
|
|
const (
|
2019-12-30 12:45:56 +03:00
|
|
|
nr_threads = 4
|
2019-06-22 22:51:07 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
struct Story {
|
|
|
|
title string
|
2019-12-22 01:41:42 +03:00
|
|
|
url string
|
2019-06-22 22:51:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Fetcher {
|
|
|
|
mut:
|
2019-12-22 01:41:42 +03:00
|
|
|
mu &sync.Mutex
|
|
|
|
ids []int
|
|
|
|
cursor int
|
|
|
|
wg &sync.WaitGroup
|
2019-06-22 22:51:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (f mut Fetcher) fetch() {
|
|
|
|
for {
|
|
|
|
if f.cursor >= f.ids.len {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
id := f.ids[f.cursor]
|
2019-08-29 11:48:03 +03:00
|
|
|
f.mu.lock()
|
2019-06-22 22:51:07 +03:00
|
|
|
f.cursor++
|
|
|
|
f.mu.unlock()
|
2019-08-29 11:48:03 +03:00
|
|
|
cursor := f.cursor
|
2019-07-29 20:18:26 +03:00
|
|
|
resp := http.get('https://hacker-news.firebaseio.com/v0/item/${id}.json') or {
|
|
|
|
println('failed to fetch data from /v0/item/${id}.json')
|
|
|
|
exit(1)
|
|
|
|
}
|
2019-12-22 01:41:42 +03:00
|
|
|
story := json.decode(Story,resp.text) 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-23 16:36:24 +03:00
|
|
|
println('#$cursor) $story.title | $story.url')
|
2019-08-29 11:48:03 +03:00
|
|
|
f.wg.done()
|
2019-06-22 22:51:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-30 16:06:16 +03:00
|
|
|
// Fetches top HN stories in 4 coroutines
|
2019-06-22 22:51:07 +03:00
|
|
|
fn main() {
|
2019-07-29 20:18:26 +03:00
|
|
|
resp := http.get('https://hacker-news.firebaseio.com/v0/topstories.json') or {
|
|
|
|
println('failed to fetch data from /v0/topstories.json')
|
|
|
|
return
|
|
|
|
}
|
2019-12-22 01:41:42 +03:00
|
|
|
mut ids := json.decode([]int,resp.text) or {
|
2019-07-29 20:18:26 +03:00
|
|
|
println('failed to decode topstories.json')
|
2019-06-22 22:51:07 +03:00
|
|
|
return
|
|
|
|
}
|
2019-07-30 16:06:16 +03:00
|
|
|
if ids.len > 10 {
|
|
|
|
// ids = ids[:10]
|
2019-09-15 12:26:05 +03:00
|
|
|
mut tmp := [0].repeat(10)
|
2020-02-24 19:55:16 +03:00
|
|
|
for i in 0..10 {
|
2019-07-30 16:06:16 +03:00
|
|
|
tmp[i] = ids[i]
|
|
|
|
}
|
|
|
|
ids = tmp
|
|
|
|
}
|
2019-12-22 01:41:42 +03:00
|
|
|
mut fetcher := &Fetcher{
|
|
|
|
ids: ids
|
2020-01-19 22:32:22 +03:00
|
|
|
mu: sync.new_mutex()
|
|
|
|
wg: sync.new_waitgroup()
|
2019-12-22 01:41:42 +03:00
|
|
|
}
|
2019-10-25 17:24:40 +03:00
|
|
|
fetcher.wg.add(ids.len)
|
2020-02-24 19:55:16 +03:00
|
|
|
for i in 0..nr_threads {
|
2019-06-22 22:51:07 +03:00
|
|
|
go fetcher.fetch()
|
|
|
|
}
|
2019-10-25 17:24:40 +03:00
|
|
|
fetcher.wg.wait()
|
2019-06-22 22:51:07 +03:00
|
|
|
}
|
|
|
|
|