Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connection timeouts #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lwt/dune
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(library
(name gluten_lwt)
(public_name gluten-lwt)
(libraries lwt gluten))
(libraries lwt gluten lwt.unix))
24 changes: 21 additions & 3 deletions lwt/gluten_lwt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ end

include Gluten_lwt_intf

let change t =
Lwt_timeout.change t 3

module IO_loop = struct
let start
: type t fd.
Expand All @@ -99,11 +102,23 @@ module IO_loop = struct
fun (module Io) (module Runtime) t ~read_buffer_size socket ->
let read_buffer = Buffer.create read_buffer_size in
let read_loop_exited, notify_read_loop_exited = Lwt.wait () in
let write_loop_exited, notify_write_loop_exited = Lwt.wait () in
let timeout =
Lwt_timeout.create 3 (fun () ->
(try Lwt.wakeup_later notify_read_loop_exited () with
| Invalid_argument _ ->
());
try Lwt.wakeup_later notify_write_loop_exited () with
| Invalid_argument _ ->
())
in
let rec read_loop () =
let rec read_loop_step () =
match Runtime.next_read_operation t with
| `Read ->
Buffer.put ~f:(Io.read socket) read_buffer >>= ( function
Buffer.put ~f:(Io.read socket) read_buffer >>= fun ret ->
change timeout;
(match ret with
| `Eof ->
Buffer.get read_buffer ~f:(fun bigstring ~off ~len ->
Runtime.read_eof t bigstring ~off ~len)
Expand All @@ -113,9 +128,10 @@ module IO_loop = struct
Buffer.get read_buffer ~f:(fun bigstring ~off ~len ->
Runtime.read t bigstring ~off ~len)
|> ignore;
read_loop_step () )
read_loop_step ())
| `Yield ->
Runtime.yield_reader t read_loop;
change timeout;
Lwt.return_unit
| `Close ->
Lwt.wakeup_later notify_read_loop_exited ();
Expand All @@ -128,16 +144,17 @@ module IO_loop = struct
Lwt.return_unit))
in
let writev = Io.writev socket in
let write_loop_exited, notify_write_loop_exited = Lwt.wait () in
let rec write_loop () =
let rec write_loop_step () =
match Runtime.next_write_operation t with
| `Write io_vectors ->
change timeout;
writev io_vectors >>= fun result ->
Runtime.report_write_result t result;
write_loop_step ()
| `Yield ->
Runtime.yield_writer t write_loop;
change timeout;
Lwt.return_unit
| `Close _ ->
Lwt.wakeup_later notify_write_loop_exited ();
Expand All @@ -150,6 +167,7 @@ module IO_loop = struct
in
read_loop ();
write_loop ();
Lwt_timeout.start timeout;
Lwt.join [ read_loop_exited; write_loop_exited ] >>= fun () ->
Io.close socket
end
Expand Down
2 changes: 1 addition & 1 deletion nix/sources.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
let
overlays =
builtins.fetchTarball
https://github.com/anmonteiro/nix-overlays/archive/77b3707.tar.gz;
https://github.com/anmonteiro/nix-overlays/archive/1718c8f.tar.gz;

in

Expand Down