Skip to content

Commit

Permalink
bench + exception managment + various
Browse files Browse the repository at this point in the history
  • Loading branch information
craff committed Jul 6, 2023
1 parent 9e1c352 commit c7dde27
Show file tree
Hide file tree
Showing 9 changed files with 4,038 additions and 4,048 deletions.
13 changes: 1 addition & 12 deletions src/Async.ml
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,10 @@ let pp_sock_info = function
| Pipe -> "Pipe"
| Lock -> "Lock"

exception SockError of socket_type * exn

let printexn e =
match e with
| SockError (_, e) -> Printf.sprintf "SockError(%s)" (Printexc.to_string e)
| e -> (Printexc.to_string e)

let clientError exn = raise (SockError(Client,exn))
let ioError exn = raise(SockError(Io,exn))

type pending_status =
NoEvent | Wait : 'a pending -> pending_status | TooSoon of bool

Expand Down Expand Up @@ -359,7 +353,6 @@ let rec read c s o l =
with Unix.(Unix_error((EAGAIN|EWOULDBLOCK),_,_))
| Ssl.(Read_error(Error_want_read|Error_want_write
|Error_want_connect|Error_want_accept|Error_zero_return)) -> perform_read c s o l
| exn -> clientError exn

and perform_read c s o l =
perform (Io {sock = c.sock; fn = (fun () -> read c s o l) })
Expand All @@ -371,7 +364,6 @@ let rec write c s o l =
| Ssl.(Write_error(Error_want_read|Error_want_write
|Error_want_connect|Error_want_accept|Error_zero_return)) ->
perform_write c s o l
| exn -> clientError exn

and perform_write c s o l =
perform (Io {sock = c.sock; fn = (fun () -> write c s o l) })
Expand All @@ -391,7 +383,6 @@ let rec sendfile c fd o l =
| Ssl.(Write_error(Error_want_read|Error_want_write
|Error_want_connect|Error_want_accept|Error_zero_return)) ->
perform_sendfile c fd o l
| exn -> clientError exn

and perform_sendfile c fd o l =
perform (Io {sock = c.sock; fn = (fun () -> sendfile c fd o l) })
Expand Down Expand Up @@ -444,15 +435,13 @@ module Io = struct
with Unix.(Unix_error((EAGAIN|EWOULDBLOCK),_,_)) ->
register io;
schedule_io io.sock (fun () -> read io s o l)
| exn -> ioError exn

let rec write (io:t) s o l =
try
Util.single_write io.sock s o l
with Unix.(Unix_error((EAGAIN|EWOULDBLOCK),_,_)) ->
register io;
schedule_io io.sock (fun () -> write io s o l)
| exn -> ioError exn

end

Expand Down Expand Up @@ -857,7 +846,7 @@ let accept_loop status listens pipes maxc =
with
| Unix.Unix_error((EAGAIN|EWOULDBLOCK),_,_) -> ()
| exn ->
(** normal if client close connection brutally? *)
(* normal if client close connection brutally? *)
Log.f (Exc 1) (fun k -> k "Exception during epoll_wait: %s" (printexn exn))
done

Expand Down
1 change: 0 additions & 1 deletion src/Async.mli
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ type socket_type =

exception NoRead
exception NoWrite
exception SockError of socket_type * exn
exception ClosedByHandler
exception TimeOut

Expand Down
12 changes: 7 additions & 5 deletions src/Server.ml
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,6 @@ let handle_client_ (self:t) (client:Async.client) : unit =
log (Req 0) (fun k -> k "response sent after %fms" (1e3 *. (Unix.gettimeofday () -. req.start_time)));
if !cont then Async.yield ()
with
| Sys_error _ | Unix.Unix_error _ | Async.ClosedByHandler | Async.TimeOut as e ->
log (Exc 1) (fun k -> k "broken connection (%s)"
(Async.printexn e));
cont := false; (* connection broken somehow *)

| Headers.Bad_req (c,s,headers,cookies) ->
log (Req 0) (fun k -> k "not 200 status: %d (%s)" (c :> int) s);
let res = Response.make_raw ~headers ~cookies ~code:c s in
Expand All @@ -247,6 +242,13 @@ let handle_client_ (self:t) (client:Async.client) : unit =
end;
if not ((c :> int) < 500) then cont := false else Async.yield ()

| Sys_error _ | Unix.Unix_error _
| Ssl.Write_error _ | Ssl.Read_error _
| Async.ClosedByHandler | Async.TimeOut as e ->
log (Exc 1) (fun k -> k "probably broken connection (%s)"
(Async.printexn e));
cont := false; (* connection broken somehow *)

| e ->
log (Exc 0) (fun k -> k "internal server error (%s)"
(Async.printexn e));
Expand Down
2 changes: 1 addition & 1 deletion src/Session.mli
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
type t
type session_data = Async.session_data = ..
type session_data = ..
type session_data += NoData

(** Managment of sessions using cookies *)
Expand Down
14 changes: 7 additions & 7 deletions tests/bench.ml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ let measure server cmd values =
let s = match server with
| Some server ->
let args = Array.of_list (String.split_on_char ' ' server) in
let chs = Unix.open_process_args args.(0) args in
let pid = Unix.process_pid chs in
let chs = Unix.open_process_args_out args.(0) args in
let pid = Unix.process_out_pid chs in
Unix.sleep 1;
Some(chs,pid)
| None -> None
Expand All @@ -48,9 +48,9 @@ let measure server cmd values =
in
let res = List.map fn values in
(match s with
| Some((ch,ch'),pid) ->
| Some(ch,pid) ->
Unix.kill pid Sys.sigkill;
close_in ch; close_out ch'
close_out ch
| None -> ());
res

Expand Down Expand Up @@ -103,7 +103,7 @@ let _ = csv := add_column !csv "nbc" string_of_int (List.map snd values)
let data =
Printf.printf "serve_files\n%!";
measure
(Some "../_build/default/tests/serve_files.exe --log-requests 0 --dir /var/www/nginx -c 2100 --timeout 10")
(Some "../_build/default/tests/serve_files.exe --log-requests 0 --dir /var/www/nginx -c 2100 --timeout 10")
(fun ((file, d), c) ->
Printf.sprintf "wrk -t5 -c%d --timeout 10 -d%d http://localhost:9080/%s"
c d file)
Expand All @@ -114,7 +114,7 @@ let _ = csv := add_column !csv "sh\\\\_cc" string_of_float data
let data =
Printf.printf "measure http_of_dir\n%!";
measure
(Some "../_build/default/src/bin/http_of_dir.exe --log-requests 0 -c 2100 --port=8080 --timeout 10 /var/www/nginx")
(Some "../_build/default/src/bin/http_of_dir.exe --log-requests 0 -c 2100 --port=8080 --timeout 10 /var/www/nginx")
(fun ((file, d), c) ->
Printf.sprintf "wrk -t5 -c%d --timeout 10 -d%d http://localhost:8080/%s"
c d file)
Expand All @@ -125,7 +125,7 @@ let _ = csv := add_column !csv "sh\\\\_dir" string_of_float data
let data =
Printf.printf "measure http_of_dir\n%!";
measure
(Some "../_build/default/src/bin/http_of_dir.exe --log-requests 0 --ssl ../_build/default/tests/domain.crt ../_build/default/tests/domain.key -c 2100 --port=8443 --timeout 10 /var/www/nginx")
(Some "../_build/default/src/bin/http_of_dir.exe --log-requests 0 --ssl ../_build/default/tests/domain.crt ../_build/default/tests/domain.key -c 2100 --port=8443 --timeout 10 /var/www/nginx")
(fun ((file, d), c) ->
Printf.sprintf "wrk -t5 -c%d --timeout 10 -d%d https://localhost:8443/%s"
c d file)
Expand Down
Loading

0 comments on commit c7dde27

Please sign in to comment.