File tree Expand file tree Collapse file tree 4 files changed +31
-21
lines changed Expand file tree Collapse file tree 4 files changed +31
-21
lines changed Original file line number Diff line number Diff line change @@ -326,13 +326,22 @@ let accept_conn s latest_response_time =
326
326
let now = Unix. gettimeofday () in
327
327
let timeout = latest_response_time -. now in
328
328
(* 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
+ if fds = [] then (* We must have timed out *)
342
+ raise Unixext. Timeout
343
+ else (* There will only ever be a maximum of one fd *)
344
+ List. hd fds
336
345
337
346
(* 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. *)
338
347
(* 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 =
60
60
match Unix. read fd bytes 0 4096 with
61
61
| 0 ->
62
62
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
63
67
| 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)
73
70
| exception
74
71
Unix. Unix_error ((Unix. EAGAIN | Unix. EWOULDBLOCK | Unix. EINTR ), _ , _ ) ->
75
72
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) =
88
88
(fun () ->
89
89
ignore
90
90
@@ 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 (
92
93
if a = fd then
93
94
CBuf. read a' a
94
- else
95
+ else if b = fd then
95
96
CBuf. read b' b
96
- else if a = fd then
97
+ ) else if a = fd then
97
98
CBuf. write b' a
98
- else
99
+ else if b = fd then
99
100
CBuf. write a' b
100
101
) ;
101
102
(* If there's nothing else to read or write then signal the other end *)
@@ -176,6 +177,7 @@ let send proxy_socket =
176
177
(fun () ->
177
178
ignore
178
179
@@ Polly. wait epoll 2 (- 1 ) (fun _ fd _ ->
180
+ (* Note: only one fd is handled *)
179
181
if s_unix = fd then (
180
182
let fd, _peer = Unix. accept s_unix in
181
183
to_close := fd :: ! to_close ;
@@ -198,7 +200,8 @@ let send proxy_socket =
198
200
to_close := fd :: ! to_close ;
199
201
proxy fd proxy_socket
200
202
) else
201
- assert false (* can never happen *)
203
+ Printf. fprintf stderr
204
+ " Unexpected file descriptor returned by epoll"
202
205
)
203
206
)
204
207
)
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)
68
68
(fun () ->
69
69
ignore
70
70
@@ Polly. wait epoll 4 (- 1 ) (fun _ fd _ ->
71
+ (* Note: only one fd is handled *)
71
72
if aout = fd then
72
73
write_from b' a'
73
74
else if bout = fd then
You can’t perform that action at this time.
0 commit comments