| 1 | // Copyright (c) 2019-2024 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 x.json2 |
| 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 := json2.decode[Story](resp.body) or { |
| 20 | println('failed to decode a story') |
| 21 | return pool.no_result |
| 22 | } |
| 23 | println('# ${cursor + 1}) ${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 | |
| 34 | ids := json2.decode[[]int](resp.body) or { |
| 35 | println('failed to decode topstories.json ${err}') |
| 36 | return |
| 37 | }#[0..30] |
| 38 | |
| 39 | mut fetcher_pool := pool.new_pool_processor( |
| 40 | callback: worker_fetch |
| 41 | ) |
| 42 | // Note: if you do not call set_max_jobs, the pool will try to use an optimal |
| 43 | // number of threads, one per each core in your system, which in most |
| 44 | // cases is what you want anyway... You can override the automatic choice |
| 45 | // by setting the VJOBS environment variable too. |
| 46 | // fetcher_pool.set_max_jobs( 4 ) |
| 47 | fetcher_pool.work_on_items(ids) |
| 48 | } |
| 49 | |