Skip to content

Commit

Permalink
eio(client): Add timeout example
Browse files Browse the repository at this point in the history
  • Loading branch information
bikallem committed Aug 25, 2022
1 parent 09c2d01 commit af9e2b5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
19 changes: 19 additions & 0 deletions cohttp-eio/examples/client_timeout.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
open Eio
open Cohttp_eio

let () =
Eio_main.run @@ fun env ->
Switch.run @@ fun sw ->
(* Increment/decrement this value to see success/failure. *)
let timeout_s = 0.01 in
Eio.Time.with_timeout env#clock timeout_s (fun () ->
let hostname, port = ("www.example.org", 80) in
let he = Unix.gethostbyname hostname in
let addr = `Tcp (Eio_unix.Ipaddr.of_unix he.h_addr_list.(0), port) in
let conn = Net.connect ~sw env#net addr in
let host = (hostname, Some port) in
let res = Client.get ~conn host "/" in
Client.read_fixed res |> Result.ok)
|> function
| Ok s -> print_string s
| Error `Timeout -> print_string "Connection timed out"
2 changes: 1 addition & 1 deletion cohttp-eio/examples/dune
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(executables
(names server1 client1 docker_client)
(names server1 client1 docker_client client_timeout)
(libraries cohttp-eio eio_main eio.unix unix))

(alias
Expand Down

0 comments on commit af9e2b5

Please sign in to comment.