From fa3e35d2e925533ef0ce6a9d2272eaffec4f43d4 Mon Sep 17 00:00:00 2001 From: Steven Woods Date: Wed, 4 Jan 2023 11:20:14 +0000 Subject: [PATCH] CP-32622: Replace select with polly in xn Signed-off-by: Steven Woods --- ocaml/xenopsd/cli/dune | 1 + ocaml/xenopsd/cli/xn.ml | 53 +++++++++++++++++++++++++---------------- 2 files changed, 33 insertions(+), 21 deletions(-) diff --git a/ocaml/xenopsd/cli/dune b/ocaml/xenopsd/cli/dune index 220d5aae2e2..1ca310dd079 100644 --- a/ocaml/xenopsd/cli/dune +++ b/ocaml/xenopsd/cli/dune @@ -9,6 +9,7 @@ astring cmdliner dune-build-info + polly re result rpclib.core diff --git a/ocaml/xenopsd/cli/xn.ml b/ocaml/xenopsd/cli/xn.ml index 079bea26db9..86d286969b8 100644 --- a/ocaml/xenopsd/cli/xn.ml +++ b/ocaml/xenopsd/cli/xn.ml @@ -1176,27 +1176,38 @@ let raw_console_proxy sockaddr = ) else if !final then finished := true else - let r, _, _ = Unix.select [Unix.stdin; fd] [] [] (-1.) in - if List.mem Unix.stdin r then ( - let b = - Unix.read Unix.stdin buf_remote !buf_remote_end - (block - !buf_remote_end) - in - let i = ref !buf_remote_end in - while - !i < !buf_remote_end + b - && Char.code (Bytes.get buf_remote !i) <> 0x1d - do - incr i - done ; - if !i < !buf_remote_end + b then final := true ; - buf_remote_end := !i - ) ; - if List.mem fd r then - let b = - Unix.read fd buf_local !buf_local_end (block - !buf_local_end) - in - buf_local_end := !buf_local_end + b + let epoll = Polly.create () in + List.iter + (fun fd -> Polly.add epoll fd Polly.Events.inp) + [Unix.stdin; fd] ; + Fun.protect + ~finally:(fun () -> Polly.close epoll) + (fun () -> + ignore + @@ Polly.wait epoll 2 (-1) (fun _ file_desc _ -> + if Unix.stdin = file_desc then ( + let b = + Unix.read Unix.stdin buf_remote !buf_remote_end + (block - !buf_remote_end) + in + let i = ref !buf_remote_end in + while + !i < !buf_remote_end + b + && Char.code (Bytes.get buf_remote !i) <> 0x1d + do + incr i + done ; + if !i < !buf_remote_end + b then final := true ; + buf_remote_end := !i + ) ; + if fd = file_desc then + let b = + Unix.read fd buf_local !buf_local_end + (block - !buf_local_end) + in + buf_local_end := !buf_local_end + b + ) + ) done in let delay = ref 0.1 in