@@ -28,91 +28,91 @@ let to_s s = (Int64.to_float s) *. 1e-9
2828
2929(* Read the entire contents of the fd, of unknown length *)
3030let timeout_read fd timeout =
31- let buf = Buffer. create ! json_rpc_max_len in
32- let read_start = Mtime_clock. counter () in
33- let get_total_used_time () = Mtime.Span. to_uint64_ns (Mtime_clock. count read_start) in
34- let rec inner max_time max_bytes =
35- let (ready_to_read, _, _) = try Unix. select [fd] [] [] (to_s max_time) with
36- (* in case the unix.select call fails in situation like interrupt *)
37- | Unix. Unix_error (Unix. EINTR,_ ,_ ) -> [] , [] , []
38- in
39- (* This is not accurate the calculate time just for the select part. However, we
40- * think the read time will be minor comparing to the scale of tens of seconds.
41- * the current style will be much concise in code. *)
42- let remain_time =
43- let used_time = get_total_used_time () in
44- Int64. sub timeout used_time
45- in
46- if remain_time < 0L then
47- begin
48- debug " Timeout after read %d" (Buffer. length buf);
49- raise Timeout
50- end ;
51- if List. mem fd ready_to_read then
52- begin
53- let bytes = Bytes. make 4096 '\000' in
54- match Unix. read fd bytes 0 4096 with
55- | 0 -> Buffer. contents buf (* EOF *)
56- | n ->
57- if n > max_bytes then
58- begin
59- debug " exceeding maximum read limit %d, clear buffer" ! json_rpc_max_len;
60- Buffer. clear buf;
61- raise Read_error
62- end
63- else
64- begin
65- Buffer. add_subbytes buf bytes 0 n;
66- inner remain_time (max_bytes - n)
67- end
68- | exception Unix. Unix_error ((Unix. EAGAIN | Unix. EWOULDBLOCK | Unix. EINTR ),_ ,_ ) ->
69- inner remain_time max_bytes
70- end
71- else inner remain_time max_bytes
72- in
73- inner timeout ! json_rpc_max_len
31+ let buf = Buffer. create ! json_rpc_max_len in
32+ let read_start = Mtime_clock. counter () in
33+ let get_total_used_time () = Mtime.Span. to_uint64_ns (Mtime_clock. count read_start) in
34+ let rec inner max_time max_bytes =
35+ let (ready_to_read, _, _) = try Unix. select [fd] [] [] (to_s max_time) with
36+ (* in case the unix.select call fails in situation like interrupt *)
37+ | Unix. Unix_error (Unix. EINTR,_ ,_ ) -> [] , [] , []
38+ in
39+ (* This is not accurate the calculate time just for the select part. However, we
40+ * think the read time will be minor comparing to the scale of tens of seconds.
41+ * the current style will be much concise in code. *)
42+ let remain_time =
43+ let used_time = get_total_used_time () in
44+ Int64. sub timeout used_time
45+ in
46+ if remain_time < 0L then
47+ begin
48+ debug " Timeout after read %d" (Buffer. length buf);
49+ raise Timeout
50+ end ;
51+ if List. mem fd ready_to_read then
52+ begin
53+ let bytes = Bytes. make 4096 '\000' in
54+ match Unix. read fd bytes 0 4096 with
55+ | 0 -> Buffer. contents buf (* EOF *)
56+ | n ->
57+ if n > max_bytes then
58+ begin
59+ debug " exceeding maximum read limit %d, clear buffer" ! json_rpc_max_len;
60+ Buffer. clear buf;
61+ raise Read_error
62+ end
63+ else
64+ begin
65+ Buffer. add_subbytes buf bytes 0 n;
66+ inner remain_time (max_bytes - n)
67+ end
68+ | exception Unix. Unix_error ((Unix. EAGAIN | Unix. EWOULDBLOCK | Unix. EINTR ),_ ,_ ) ->
69+ inner remain_time max_bytes
70+ end
71+ else inner remain_time max_bytes
72+ in
73+ inner timeout ! json_rpc_max_len
7474
7575(* Write as many bytes to a file descriptor as possible from data before a given clock time. *)
7676(* Raises Timeout exception if the number of bytes written is less than the specified length. *)
7777(* Writes into the file descriptor at the current cursor position. *)
7878let timeout_write filedesc total_length data response_time =
79- let write_start = Mtime_clock. counter () in
80- let get_total_used_time () = Mtime.Span. to_uint64_ns (Mtime_clock. count write_start) in
81- let rec inner_write offset max_time =
82- let (_, ready_to_write, _) = try Unix. select [] [filedesc] [] (to_s max_time) with
83- (* in case the unix.select call fails in situation like interrupt *)
84- | Unix. Unix_error (Unix. EINTR,_ ,_ ) -> [] , [] , []
85- in
86- let remain_time =
87- let used_time = get_total_used_time () in
88- Int64. sub response_time used_time
89- in
90- if remain_time < 0L then
91- begin
92- debug " Timeout to write %d at offset %d" total_length offset;
93- raise Timeout
94- end ;
95- if List. mem filedesc ready_to_write then
96- begin
97- let length = total_length - offset in
98- let bytes_written =
99- (try Unix. single_write filedesc data offset length with
100- | Unix. Unix_error ((Unix. EAGAIN | Unix. EWOULDBLOCK | Unix. EINTR ),_ ,_ ) -> 0 )
101- in
102- let new_offset = offset + bytes_written in
103- if length = bytes_written then ()
104- else inner_write new_offset remain_time
105- end
106- else inner_write offset remain_time
107- in
108- inner_write 0 response_time
79+ let write_start = Mtime_clock. counter () in
80+ let get_total_used_time () = Mtime.Span. to_uint64_ns (Mtime_clock. count write_start) in
81+ let rec inner_write offset max_time =
82+ let (_, ready_to_write, _) = try Unix. select [] [filedesc] [] (to_s max_time) with
83+ (* in case the unix.select call fails in situation like interrupt *)
84+ | Unix. Unix_error (Unix. EINTR,_ ,_ ) -> [] , [] , []
85+ in
86+ let remain_time =
87+ let used_time = get_total_used_time () in
88+ Int64. sub response_time used_time
89+ in
90+ if remain_time < 0L then
91+ begin
92+ debug " Timeout to write %d at offset %d" total_length offset;
93+ raise Timeout
94+ end ;
95+ if List. mem filedesc ready_to_write then
96+ begin
97+ let length = total_length - offset in
98+ let bytes_written =
99+ (try Unix. single_write filedesc data offset length with
100+ | Unix. Unix_error ((Unix. EAGAIN | Unix. EWOULDBLOCK | Unix. EINTR ),_ ,_ ) -> 0 )
101+ in
102+ let new_offset = offset + bytes_written in
103+ if length = bytes_written then ()
104+ else inner_write new_offset remain_time
105+ end
106+ else inner_write offset remain_time
107+ in
108+ inner_write 0 response_time
109109
110110let with_rpc ?(version =Jsonrpc. V2 ) ~path ~call () =
111- let uri = Uri. of_string (Printf. sprintf " file://%s" path) in
112- Open_uri. with_open_uri uri (fun s ->
113- Unix. set_nonblock s;
114- let req = Bytes. of_string (Jsonrpc. string_of_call ~version call) in
115- timeout_write s (Bytes. length req) req ! json_rpc_write_timeout;
116- let res = timeout_read s ! json_rpc_read_timeout in
117- debug " Response: %s" res;
118- Jsonrpc. response_of_string ~strict: false res)
111+ let uri = Uri. of_string (Printf. sprintf " file://%s" path) in
112+ Open_uri. with_open_uri uri (fun s ->
113+ Unix. set_nonblock s;
114+ let req = Bytes. of_string (Jsonrpc. string_of_call ~version call) in
115+ timeout_write s (Bytes. length req) req ! json_rpc_write_timeout;
116+ let res = timeout_read s ! json_rpc_read_timeout in
117+ debug " Response: %s" res;
118+ Jsonrpc. response_of_string ~strict: false res)
0 commit comments