Skip to content

Commit

Permalink
CP-32622: Replace select with polly in xsh
Browse files Browse the repository at this point in the history
  • Loading branch information
snwoods committed Jan 5, 2023
1 parent 344bd04 commit 4a79673
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
1 change: 1 addition & 0 deletions ocaml/xsh/dune
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
(package xapi)
(libraries
dune-build-info
polly
stunnel
safe-resources
xapi-consts
Expand Down
24 changes: 18 additions & 6 deletions ocaml/xsh/xsh.ml
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,24 @@ let proxy (ain : Unix.file_descr) (aout : Unix.file_descr) (bin : Unixfd.t)
(if can_write a' then [bout] else [])
@ if can_write b' then [aout] else []
in
let r, w, _ = Unix.select r w [] (-1.0) in
(* Do the writing before the reading *)
List.iter
(fun fd -> if aout = fd then write_from b' a' else write_from a' b')
w ;
List.iter (fun fd -> if ain = fd then read_into a' else read_into b') r
let epoll = Polly.create () in
List.iter (fun fd -> Polly.add epoll fd Polly.Events.inp) r ;
List.iter (fun fd -> Polly.add epoll fd Polly.Events.out) w ;
Fun.protect
~finally:(fun () -> Polly.close epoll)
(fun () ->
ignore
@@ Polly.wait epoll 4 (-1) (fun _ fd _ ->
if aout = fd then
write_from b' a'
else if bout = fd then
write_from a' b'
else if ain = fd then
read_into a'
else
read_into b'
)
)
done
with _ -> (
(try Unix.clear_nonblock ain with _ -> ()) ;
Expand Down

0 comments on commit 4a79673

Please sign in to comment.