example(echo): add --pool option to use a thread pool

This commit is contained in:
Simon Cruanes 2022-01-01 21:50:55 -05:00
parent d68142a161
commit 700f42a4d1
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
2 changed files with 11 additions and 2 deletions

View file

@ -13,7 +13,7 @@
(name echo) (name echo)
(flags :standard -warn-error -a+8) (flags :standard -warn-error -a+8)
(modules echo vfs) (modules echo vfs)
(libraries tiny_httpd tiny_httpd_camlzip)) (libraries tiny_httpd tiny_httpd.pool tiny_httpd_camlzip))
(rule (rule
(targets test_output.txt) (targets test_output.txt)

View file

@ -36,15 +36,24 @@ let middleware_stat () : S.Middleware.t * (unit -> string) =
let () = let () =
let port_ = ref 8080 in let port_ = ref 8080 in
let pool = ref false in
let j = ref 32 in let j = ref 32 in
Arg.parse (Arg.align [ Arg.parse (Arg.align [
"--port", Arg.Set_int port_, " set port"; "--port", Arg.Set_int port_, " set port";
"-p", Arg.Set_int port_, " set port"; "-p", Arg.Set_int port_, " set port";
"--debug", Arg.Unit (fun () -> S._enable_debug true), " enable debug"; "--debug", Arg.Unit (fun () -> S._enable_debug true), " enable debug";
"--pool", Arg.Set pool, " use a thread pool";
"-j", Arg.Set_int j, " maximum number of connections"; "-j", Arg.Set_int j, " maximum number of connections";
]) (fun _ -> raise (Arg.Bad "")) "echo [option]*"; ]) (fun _ -> raise (Arg.Bad "")) "echo [option]*";
let server = S.create ~port:!port_ ~max_connections:!j () in let new_thread =
if !pool then (
let j = if !j > 64 then Some (!j/4) else None in
let pool = Tiny_httpd_pool.create ?j () in
Some (fun f -> Tiny_httpd_pool.run pool f)
) else None
in
let server = S.create ?new_thread ~port:!port_ ~max_connections:!j () in
Tiny_httpd_camlzip.setup ~compress_above:1024 ~buf_size:(16*1024) server; Tiny_httpd_camlzip.setup ~compress_above:1024 ~buf_size:(16*1024) server;
let m_stats, get_stats = middleware_stat () in let m_stats, get_stats = middleware_stat () in