mirror of
https://github.com/c-cube/nanoev.git
synced 2025-12-06 03:05:32 -05:00
add posix backend to test and to echo.ml
This commit is contained in:
parent
adc468b59d
commit
bbd77c4730
5 changed files with 66 additions and 8 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
(executable
|
(executable
|
||||||
(name echo)
|
(name echo)
|
||||||
(libraries nanoev nanoev.unix moonpool moonpool.fib trace trace-tef
|
(libraries nanoev nanoev.unix nanoev-posix moonpool moonpool.fib trace
|
||||||
nanoev_tiny_httpd))
|
trace-tef nanoev_tiny_httpd))
|
||||||
|
|
|
||||||
|
|
@ -86,6 +86,13 @@ let () =
|
||||||
let port_ = ref 8080 in
|
let port_ = ref 8080 in
|
||||||
let max_conn = ref 1024 in
|
let max_conn = ref 1024 in
|
||||||
let j = ref 8 in
|
let j = ref 8 in
|
||||||
|
let backend = ref `Posix in
|
||||||
|
|
||||||
|
let set_backend = function
|
||||||
|
| "posix" | "poll" | "default" -> backend := `Posix
|
||||||
|
| "unix" | "select" -> backend := `Unix
|
||||||
|
| s -> failwith @@ Printf.sprintf "unknown backend %S" s
|
||||||
|
in
|
||||||
Arg.parse
|
Arg.parse
|
||||||
(Arg.align
|
(Arg.align
|
||||||
[
|
[
|
||||||
|
|
@ -94,15 +101,30 @@ let () =
|
||||||
"-j", Arg.Set_int j, " number of threads";
|
"-j", Arg.Set_int j, " number of threads";
|
||||||
"--debug", Arg.Unit setup_logging, " enable debug";
|
"--debug", Arg.Unit setup_logging, " enable debug";
|
||||||
"--max-conns", Arg.Set_int max_conn, " maximum concurrent connections";
|
"--max-conns", Arg.Set_int max_conn, " maximum concurrent connections";
|
||||||
|
( "--backend",
|
||||||
|
Arg.Symbol
|
||||||
|
([ "posix"; "default"; "unix"; "select"; "poll" ], set_backend),
|
||||||
|
" event loop backend" );
|
||||||
])
|
])
|
||||||
(fun _ -> raise (Arg.Bad ""))
|
(fun _ -> raise (Arg.Bad ""))
|
||||||
"echo [option]*";
|
"echo [option]*";
|
||||||
|
|
||||||
let@ pool = Moonpool.Ws_pool.with_ ~num_threads:!j () in
|
let@ pool =
|
||||||
let@ _runner = Moonpool_fib.main in
|
fun yield ->
|
||||||
|
if !j > 1 then
|
||||||
|
let@ pool = Moonpool.Ws_pool.with_ ~num_threads:!j () in
|
||||||
|
let@ _runner = Moonpool_fib.main in
|
||||||
|
yield pool
|
||||||
|
else
|
||||||
|
Moonpool_fib.main yield
|
||||||
|
in
|
||||||
|
|
||||||
let ev = Nanoev_unix.create () in
|
let ev =
|
||||||
Nanoev_picos.setup_bg_thread ev;
|
match !backend with
|
||||||
|
| `Posix -> Nanoev_posix.create ()
|
||||||
|
| `Unix -> Nanoev_unix.create ()
|
||||||
|
in
|
||||||
|
let@ () = Nanoev_picos.with_setup_bg_thread ev in
|
||||||
|
|
||||||
let server =
|
let server =
|
||||||
Nanoev_tiny_httpd.create ~new_thread:(Moonpool.run_async pool) ~port:!port_
|
Nanoev_tiny_httpd.create ~new_thread:(Moonpool.run_async pool) ~port:!port_
|
||||||
|
|
@ -273,8 +295,9 @@ let () =
|
||||||
let s = to_string_top h in
|
let s = to_string_top h in
|
||||||
Response.make_string ~headers:[ "content-type", "text/html" ] @@ Ok s);
|
Response.make_string ~headers:[ "content-type", "text/html" ] @@ Ok s);
|
||||||
|
|
||||||
Printf.printf "listening on http://%s:%d\n%!" (Server.addr server)
|
Printf.printf
|
||||||
(Server.port server);
|
"listening on http://%s:%d with %d threads, %d max connections\n%!"
|
||||||
|
(Server.addr server) (Server.port server) !j !max_conn;
|
||||||
match Server.run server with
|
match Server.run server with
|
||||||
| Ok () -> ()
|
| Ok () -> ()
|
||||||
| Error e -> raise e
|
| Error e -> raise e
|
||||||
|
|
|
||||||
3
tests/posix/dune
Normal file
3
tests/posix/dune
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
(tests
|
||||||
|
(names t1)
|
||||||
|
(libraries nanoev nanoev-posix threads))
|
||||||
3
tests/posix/t1.expected
Normal file
3
tests/posix/t1.expected
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
writing
|
||||||
|
can read
|
||||||
|
done writing
|
||||||
29
tests/posix/t1.ml
Normal file
29
tests/posix/t1.ml
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
module E = Nanoev_posix
|
||||||
|
|
||||||
|
let mkpipe () : Unix.file_descr * Unix.file_descr =
|
||||||
|
let f1, f2 = Unix.pipe () in
|
||||||
|
Unix.set_nonblock f1;
|
||||||
|
Unix.set_nonblock f2;
|
||||||
|
f1, f2
|
||||||
|
|
||||||
|
let loop (e : E.t) =
|
||||||
|
while true do
|
||||||
|
E.step e
|
||||||
|
done
|
||||||
|
|
||||||
|
let () =
|
||||||
|
let ev = E.create () in
|
||||||
|
ignore (Thread.create loop ev : Thread.t);
|
||||||
|
let rd, wr = mkpipe () in
|
||||||
|
E.on_readable ev rd () () (fun ~closed () () ->
|
||||||
|
if closed then
|
||||||
|
print_endline "closed!"
|
||||||
|
else
|
||||||
|
print_endline "can read");
|
||||||
|
Thread.delay 0.05;
|
||||||
|
print_endline "writing";
|
||||||
|
ignore
|
||||||
|
(Unix.write wr (Bytes.unsafe_of_string "hello") 0 (String.length "hello")
|
||||||
|
: int);
|
||||||
|
Thread.delay 0.1;
|
||||||
|
print_endline "done writing"
|
||||||
Loading…
Add table
Reference in a new issue