Skip to content

Commit 62bff57

Browse files
committed
CP-32622: replace select in proxy with polly
Signed-off-by: Edwin Török <[email protected]>
1 parent 0f41cc9 commit 62bff57

File tree

1 file changed

+28
-14
lines changed
  • ocaml/libs/xapi-stdext/lib/xapi-stdext-unix

1 file changed

+28
-14
lines changed

ocaml/libs/xapi-stdext/lib/xapi-stdext-unix/unixext.ml

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -434,24 +434,38 @@ let proxy (a : Unix.file_descr) (b : Unix.file_descr) =
434434
let a' = CBuf.empty size and b' = CBuf.empty size in
435435
Unix.set_nonblock a ;
436436
Unix.set_nonblock b ;
437+
with_polly @@ fun polly ->
438+
Polly.add polly a Polly.Events.empty ;
439+
Polly.add polly b Polly.Events.empty ;
437440
try
438441
while true do
439-
let r =
440-
(if CBuf.should_read a' then [a] else [])
441-
@ if CBuf.should_read b' then [b] else []
442-
in
443-
let w =
444-
(if CBuf.should_write a' then [b] else [])
445-
@ if CBuf.should_write b' then [a] else []
442+
(* use oneshot notification so that we can use Polly.mod as needed to reenable,
443+
but it will disable itself each turn *)
444+
let a_events =
445+
Polly.Events.(
446+
(if CBuf.should_read a' then inp lor oneshot else empty)
447+
lor if CBuf.should_write b' then out lor oneshot else empty
448+
)
449+
and b_events =
450+
Polly.Events.(
451+
(if CBuf.should_read b' then inp lor oneshot else empty)
452+
lor if CBuf.should_write a' then out lor oneshot else empty
453+
)
446454
in
447455
(* If we can't make any progress (because fds have been closed), then stop *)
448-
if r = [] && w = [] then raise End_of_file ;
449-
let r, w, _ = Unix.select r w [] (-1.0) in
450-
(* Do the writing before the reading *)
451-
List.iter
452-
(fun fd -> if a = fd then CBuf.write b' a else CBuf.write a' b)
453-
w ;
454-
List.iter (fun fd -> if a = fd then CBuf.read a' a else CBuf.read b' b) r ;
456+
if Polly.Events.(a_events lor b_events = empty) then raise End_of_file ;
457+
458+
if Polly.Events.(a_events <> empty) then
459+
Polly.upd polly a a_events ;
460+
if Polly.Events.(b_events <> empty) then
461+
Polly.upd polly b b_events ;
462+
Polly.wait_fold polly 4 (-1) () (fun _polly fd events () ->
463+
(* Do the writing before the reading *)
464+
if Polly.Events.(test out events) then
465+
if a = fd then CBuf.write b' a else CBuf.write a' b ;
466+
if Polly.Events.(test inp events) then
467+
if a = fd then CBuf.read a' a else CBuf.read b' b
468+
) ;
455469
(* If there's nothing else to read or write then signal the other end *)
456470
List.iter
457471
(fun (buf, fd) ->

0 commit comments

Comments
 (0)