File tree Expand file tree Collapse file tree 4 files changed +34
-21
lines changed
Expand file tree Collapse file tree 4 files changed +34
-21
lines changed Original file line number Diff line number Diff line change @@ -326,13 +326,25 @@ let accept_conn s latest_response_time =
326326 let now = Unix. gettimeofday () in
327327 let timeout = latest_response_time -. now in
328328 (* Await an incoming connection... *)
329- Fun. protect
330- ~finally: (fun () -> Unix. setsockopt_float s Unix. SO_RCVTIMEO 0. )
331- (fun () ->
332- Unix. setsockopt_float s Unix. SO_RCVTIMEO timeout ;
333- try fst (Unix. accept s)
334- with Unix. Unix_error (Unix. EAGAIN, _ , _ ) -> raise Unixext. Timeout
335- )
329+ let epoll = Polly. create () in
330+ Polly. add epoll s Polly.Events. inp ;
331+ let fds =
332+ Fun. protect
333+ ~finally: (fun () -> Polly. close epoll)
334+ (fun () ->
335+ Polly. wait_fold epoll 1
336+ (int_of_float (timeout *. 1000. ))
337+ []
338+ (fun _ fd _ fds -> fst (Unix. accept fd) :: fds)
339+ )
340+ in
341+ match fds with
342+ | [] ->
343+ (* We must have timed out *)
344+ raise Unixext. Timeout
345+ | fd :: _ ->
346+ (* There will only ever be a maximum of one fd *)
347+ fd
336348
337349(* Listen on a given socket. Accept a single connection and transfer all the data from it to dest_fd, or raise Timeout if target_response_time happens first. *)
338350(* Raises NotEnoughSpace if the next write would exceed the available_space. *)
Original file line number Diff line number Diff line change @@ -60,16 +60,13 @@ let timeout_read fd timeout =
6060 match Unix. read fd bytes 0 4096 with
6161 | 0 ->
6262 Buffer. contents buf (* EOF *)
63+ | n when n > max_bytes ->
64+ debug " exceeding maximum read limit %d, clear buffer" ! json_rpc_max_len ;
65+ Buffer. clear buf ;
66+ raise Read_error
6367 | n ->
64- if n > max_bytes then (
65- debug " exceeding maximum read limit %d, clear buffer"
66- ! json_rpc_max_len ;
67- Buffer. clear buf ;
68- raise Read_error
69- ) else (
70- Buffer. add_subbytes buf bytes 0 n ;
71- inner remain_time (max_bytes - n)
72- )
68+ Buffer. add_subbytes buf bytes 0 n ;
69+ inner remain_time (max_bytes - n)
7370 | exception
7471 Unix. Unix_error ((Unix. EAGAIN | Unix. EWOULDBLOCK | Unix. EINTR ), _ , _ ) ->
7572 inner remain_time max_bytes
Original file line number Diff line number Diff line change @@ -88,14 +88,15 @@ let proxy (a : Unix.file_descr) (b : Unix.file_descr) =
8888 (fun () ->
8989 ignore
9090 @@ Polly. wait epoll 4 (- 1 ) (fun _ fd event ->
91- if event = Polly.Events. inp then
91+ (* Note: only one fd is handled *)
92+ if event = Polly.Events. inp then (
9293 if a = fd then
9394 CBuf. read a' a
94- else
95+ else if b = fd then
9596 CBuf. read b' b
96- else if a = fd then
97+ ) else if a = fd then
9798 CBuf. write b' a
98- else
99+ else if b = fd then
99100 CBuf. write a' b
100101 ) ;
101102 (* If there's nothing else to read or write then signal the other end *)
@@ -176,6 +177,7 @@ let send proxy_socket =
176177 (fun () ->
177178 ignore
178179 @@ Polly. wait epoll 2 (- 1 ) (fun _ fd _ ->
180+ (* Note: only one fd is handled *)
179181 if s_unix = fd then (
180182 let fd, _peer = Unix. accept s_unix in
181183 to_close := fd :: ! to_close ;
@@ -198,7 +200,8 @@ let send proxy_socket =
198200 to_close := fd :: ! to_close ;
199201 proxy fd proxy_socket
200202 ) else
201- assert false (* can never happen *)
203+ Printf. fprintf stderr
204+ " Unexpected file descriptor returned by epoll"
202205 )
203206 )
204207 )
Original file line number Diff line number Diff line change @@ -68,6 +68,7 @@ let proxy (ain : Unix.file_descr) (aout : Unix.file_descr) (bin : Unixfd.t)
6868 (fun () ->
6969 ignore
7070 @@ Polly. wait epoll 4 (- 1 ) (fun _ fd _ ->
71+ (* Note: only one fd is handled *)
7172 if aout = fd then
7273 write_from b' a'
7374 else if bout = fd then
You can’t perform that action at this time.
0 commit comments