| 1 | import context |
| 2 | import net.http |
| 3 | import x.async as xasync |
| 4 | |
| 5 | fn synthetic_http_handler(req http.Request) !http.Response { |
| 6 | path := if req.url.contains('?') { req.url.all_before('?') } else { req.url } |
| 7 | match path { |
| 8 | '/health' { |
| 9 | return http.new_response(status: .ok, body: 'healthy') |
| 10 | } |
| 11 | '/ready' { |
| 12 | return http.new_response(status: .ok, body: 'ready') |
| 13 | } |
| 14 | else { |
| 15 | return http.new_response(status: .not_found, body: 'missing') |
| 16 | } |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | fn main() { |
| 21 | requests := [ |
| 22 | http.new_request(.get, '/health', ''), |
| 23 | http.new_request(.get, '/ready', ''), |
| 24 | http.new_request(.get, '/missing', ''), |
| 25 | ] |
| 26 | responses := chan string{cap: requests.len} |
| 27 | mut pool := xasync.new_pool(workers: 2, queue_size: requests.len)! |
| 28 | |
| 29 | for req in requests { |
| 30 | pool.try_submit(fn [req, responses] (mut ctx context.Context) ! { |
| 31 | done := ctx.done() |
| 32 | select { |
| 33 | _ := <-done { |
| 34 | return ctx.err() |
| 35 | } |
| 36 | else {} |
| 37 | } |
| 38 | resp := synthetic_http_handler(req)! |
| 39 | responses <- '${req.url} -> ${resp.status_code} ${resp.body}' |
| 40 | })! |
| 41 | } |
| 42 | |
| 43 | pool.close()! |
| 44 | for _ in 0 .. requests.len { |
| 45 | println(<-responses) |
| 46 | } |
| 47 | } |
| 48 | |