File: client_timeout.ml

package info (click to toggle)
ocaml-cohttp 6.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,624 kB
  • sloc: ml: 13,107; makefile: 20; sh: 18; javascript: 18
file content (17 lines) | stat: -rw-r--r-- 611 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
open Cohttp_eio

let () =
  Eio_main.run @@ fun env ->
  let client = Client.make ~https:None env#net in
  (* Increment/decrement this value to see success/failure. *)
  let timeout_s = 0.01 in
  Eio.Time.with_timeout env#clock timeout_s (fun () ->
      Eio.Switch.run @@ fun sw ->
      let _, body =
        Client.get client ~sw (Uri.of_string "http://www.example.org")
      in
      Eio.Buf_read.(of_flow ~max_size:max_int body |> take_all) |> Result.ok)
  |> function
  | Ok s -> print_string s
  | Error (`Fatal e) -> Fmt.epr "fatal error: %s@." e
  | Error `Timeout -> Fmt.epr "Connection timed out@."