Skip to content
Snippets Groups Projects
Commit 12f02be6 authored by Loïc Correnson's avatar Loïc Correnson Committed by David Bühler
Browse files

[server] maximal socket buffer size

parent 44f0e1f1
No related branches found
No related tags found
No related merge requests found
...@@ -41,6 +41,15 @@ module Socket = Senv.String ...@@ -41,6 +41,15 @@ module Socket = Senv.String
Finally, the server is executed until shutdown." Finally, the server is executed until shutdown."
end) end)
let () = Parameter_customize.set_group socket_group
module SocketSize = Senv.Int
(struct
let option_name = "-server-socket-size"
let arg_name = "n"
let default = 8192
let help = "Set the maximal size of socket buffers (default: 8192)"
end)
let _ = Server_doc.protocole let _ = Server_doc.protocole
~title:"Unix Socket Protocol" ~title:"Unix Socket Protocol"
~readme:"server_socket.md" ~readme:"server_socket.md"
...@@ -237,9 +246,11 @@ let channel (s: socket) = ...@@ -237,9 +246,11 @@ let channel (s: socket) =
| None -> | None ->
try try
let sock,_ = Unix.accept ~cloexec:true s.socket in let sock,_ = Unix.accept ~cloexec:true s.socket in
let snd = Unix.getsockopt_int sock SO_SNDBUF in Unix.set_nonblock sock ;
let rcv = Unix.getsockopt_int sock SO_RCVBUF in let size = max 256 (SocketSize.get ()) in
Senv.debug "Client connected" ; let rcv = min size @@ Unix.getsockopt_int sock SO_RCVBUF in
let snd = min size @@ Unix.getsockopt_int sock SO_SNDBUF in
Senv.debug "Client connected (in: %d) (out: %d)" rcv snd ;
let ch = Some { let ch = Some {
sock ; sock ;
snd = Bytes.create snd ; snd = Bytes.create snd ;
...@@ -262,12 +273,10 @@ let fetch (s:socket) () = ...@@ -262,12 +273,10 @@ let fetch (s:socket) () =
Senv.warning "Socket: exn %s" (Printexc.to_string exn) ; Senv.warning "Socket: exn %s" (Printexc.to_string exn) ;
close s ; None close s ; None
let bind fd = let establish_server fd =
let socket = { socket = fd ; channel = None } in let socket = { socket = fd ; channel = None } in
try try
Unix.set_nonblock fd ;
Unix.listen fd 1 ; Unix.listen fd 1 ;
Unix.set_nonblock fd ;
ignore (Sys.signal Sys.sigpipe Signal_ignore) ; ignore (Sys.signal Sys.sigpipe Signal_ignore) ;
let pretty = Format.pp_print_string in let pretty = Format.pp_print_string in
let server = Main.create ~pretty ~fetch:(fetch socket) () in let server = Main.create ~pretty ~fetch:(fetch socket) () in
...@@ -293,17 +302,16 @@ let bind fd = ...@@ -293,17 +302,16 @@ let bind fd =
let server = ref None let server = ref None
let cmdline () = let cmdline () =
let addr = Socket.get () in let option = match Socket.get () with "" -> None | a -> Some a in
match !server with match !server, option with
| Some addr0 -> | _ , None -> ()
if Senv.debug_atleast 1 && addr <> addr0 then | Some addr0, Some addr ->
if addr0 <> addr then
Senv.warning "Socket server already running on [%s]." addr0 Senv.warning "Socket server already running on [%s]." addr0
else | None, Some addr ->
Senv.feedback "Socket server already running." begin
| None ->
if addr <> "" then
try try
server := Some addr ; server := option ;
if Sys.file_exists addr then Unix.unlink addr ; if Sys.file_exists addr then Unix.unlink addr ;
let fd = Unix.socket PF_UNIX SOCK_STREAM 0 in let fd = Unix.socket PF_UNIX SOCK_STREAM 0 in
Unix.bind fd (ADDR_UNIX addr) ; Unix.bind fd (ADDR_UNIX addr) ;
...@@ -311,10 +319,11 @@ let cmdline () = ...@@ -311,10 +319,11 @@ let cmdline () =
Senv.feedback "Socket server running on [%s]." addr Senv.feedback "Socket server running on [%s]." addr
else else
Senv.feedback "Socket server running." ; Senv.feedback "Socket server running." ;
bind fd establish_server fd ;
with exn -> with exn ->
Senv.fatal "Server socket failed.@\nError: %s@" Senv.fatal "Server socket failed.@\nError: %s@"
(Printexc.to_string exn) (Printexc.to_string exn)
end
let () = Db.Main.extend cmdline let () = Db.Main.extend cmdline
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment