1 | // Copyright (c) 2019-2023 Alexander Medvednikov. All rights reserved. |
2 | // Use of this source code is governed by an MIT license |
3 | // that can be found in the LICENSE file. |
4 | import net.http |
5 | import json |
6 | import sync.pool |
7 | |
8 | struct Story { |
9 | title string |
10 | url string |
11 | } |
12 | |
13 | fn worker_fetch(mut p pool.PoolProcessor, cursor int, worker_id int) voidptr { |
14 | id := p.get_item[int](cursor) |
15 | resp := http.get('https://hacker-news.firebaseio.com/v0/item/${id}.json') or { |
16 | println('failed to fetch data from /v0/item/${id}.json') |
17 | return pool.no_result |
18 | } |
19 | story := json.decode(Story, resp.body) or { |
20 | println('failed to decode a story') |
21 | return pool.no_result |
22 | } |
23 | println('# ${cursor}) ${story.title} | ${story.url}') |
24 | return pool.no_result |
25 | } |
26 | |
27 | // Fetches top HN stories in parallel, depending on how many cores you have |
28 | fn main() { |
29 | resp := http.get('https://hacker-news.firebaseio.com/v0/topstories.json') or { |
30 | println('failed to fetch data from /v0/topstories.json') |
31 | return |
32 | } |
33 | ids := json.decode([]int, resp.body) or { |
34 | println('failed to decode topstories.json') |
35 | return |
36 | }#[0..10] |
37 | mut fetcher_pool := pool.new_pool_processor( |
38 | callback: worker_fetch |
39 | ) |
40 | // Note: if you do not call set_max_jobs, the pool will try to use an optimal |
41 | // number of threads, one per each core in your system, which in most |
42 | // cases is what you want anyway... You can override the automatic choice |
43 | // by setting the VJOBS environment variable too. |
44 | // fetcher_pool.set_max_jobs( 4 ) |
45 | fetcher_pool.work_on_items(ids) |
46 | } |