Просмотр исходного кода

Add transport options linger, send_timeout, send_timeout_close

Loïc Hoguin 11 лет назад
Родитель
Сommit
3634e392a8
4 измененных файлов с 38 добавлено и 8 удалено
  1. 9 0
      manual/ranch_ssl.md
  2. 10 1
      manual/ranch_tcp.md
  3. 10 5
      src/ranch_ssl.erl
  4. 9 2
      src/ranch_tcp.erl

+ 9 - 0
manual/ranch_ssl.md

@@ -17,6 +17,7 @@ Types
 	| {ip, inet:ip_address()}
 	| {key, Der::binary()}
 	| {keyfile, string()}
+	| {linger, {boolean(), non_neg_integer()}}
 	| {next_protocols_advertised, [binary()]}
 	| {nodelay, boolean()}
 	| {password, string()}
@@ -25,6 +26,8 @@ Types
 	| {reuse_session, fun()}
 	| {reuse_sessions, boolean()}
 	| {secure_renegotiate, boolean()}
+	| {send_timeout, timeout()}
+	| {send_timeout_close, boolean()}
 	| {verify, ssl:verify_type()}
 	| {verify_fun, {fun(), InitialUserState::term()}}]
 
@@ -64,6 +67,8 @@ The default value is given next to the option name.
    -  DER encoded user private key.
  -  keyfile
    -  Path to the PEM encoded private key file, if different than the certfile.
+ -  linger ({false, 0})
+   -  Whether to wait and how long to flush data sent before closing the socket.
  -  next_protocols_advertised
    -  List of protocols to send to the client if it supports the Next Protocol extension.
  -  nodelay (true)
@@ -78,6 +83,10 @@ The default value is given next to the option name.
    -  Whether to allow session reuse.
  -  secure_renegotiate (false)
    -  Whether to reject renegotiation attempts that do not conform to RFC5746.
+ -  send_timeout (30000)
+   -  How long the send call may wait for confirmation before returning.
+ -  send_timeout_close (true)
+   -  Whether to close the socket when the confirmation wasn't received.
  -  verify (verify_none)
    -  Use `verify_peer` to request a certificate from the client.
  -  verify_fun

+ 10 - 1
manual/ranch_tcp.md

@@ -13,9 +13,12 @@ Types
 
 ### opts() = [{backlog, non_neg_integer()}
 	| {ip, inet:ip_address()}
+	| {linger, {boolean(), non_neg_integer()}}
 	| {nodelay, boolean()}
 	| {port, inet:port_number()}
-	| {raw, non_neg_integer(), non_neg_integer(), non_neg_integer() | binary()}]
+	| {raw, non_neg_integer(), non_neg_integer(), non_neg_integer() | binary()}
+	| {send_timeout, timeout()}
+	| {send_timeout_close, boolean()}]
 
 > Listen options.
 >
@@ -34,10 +37,16 @@ The default value is given next to the option name.
    -  Max length of the queue of pending connections.
  -  ip
    -  Interface to listen on. Listen on all interfaces by default.
+ -  linger ({false, 0})
+   -  Whether to wait and how long to flush data sent before closing the socket.
  -  nodelay (true)
    -  Whether to enable TCP_NODELAY.
  -  port (0)
    -  TCP port number to listen on. 0 means a random port will be used.
+ -  send_timeout (30000)
+   -  How long the send call may wait for confirmation before returning.
+ -  send_timeout_close (true)
+   -  Whether to close the socket when the confirmation wasn't received.
 
 The `raw` option is unsupported.
 

+ 10 - 5
src/ranch_ssl.erl

@@ -55,6 +55,7 @@
 	| {ip, inet:ip_address()}
 	| {key, Der::binary()}
 	| {keyfile, string()}
+	| {linger, {boolean(), non_neg_integer()}}
 	| {next_protocols_advertised, [binary()]}
 	| {nodelay, boolean()}
 	| {password, string()}
@@ -64,6 +65,8 @@
 	| {reuse_session, fun()}
 	| {reuse_sessions, boolean()}
 	| {secure_renegotiate, boolean()}
+	| {send_timeout, timeout()}
+	| {send_timeout_close, boolean()}
 	| {verify, ssl:verify_type()}
 	| {verify_fun, {fun(), InitialUserState::term()}}].
 -export_type([opts/0]).
@@ -145,16 +148,18 @@ listen(Opts) ->
 	true = lists:keymember(cert, 1, Opts)
 		orelse lists:keymember(certfile, 1, Opts),
 	Opts2 = ranch:set_option_default(Opts, backlog, 1024),
-	Opts3 = ranch:set_option_default(Opts2, ciphers, unbroken_cipher_suites()),
+	Opts3 = ranch:set_option_default(Opts2, send_timeout, 30000),
+	Opts4 = ranch:set_option_default(Opts3, send_timeout_close, true),
+	Opts5 = ranch:set_option_default(Opts4, ciphers, unbroken_cipher_suites()),
 	%% We set the port to 0 because it is given in the Opts directly.
 	%% The port in the options takes precedence over the one in the
 	%% first argument.
-	ssl:listen(0, ranch:filter_options(Opts3,
+	ssl:listen(0, ranch:filter_options(Opts5,
 		[backlog, cacertfile, cacerts, cert, certfile, ciphers,
 			fail_if_no_peer_cert, hibernate_after, ip, key, keyfile,
-			next_protocols_advertised, nodelay, password, port, raw,
-			reuse_session, reuse_sessions,
-			secure_renegotiate, verify, verify_fun],
+			linger, next_protocols_advertised, nodelay, password, port, raw,
+			reuse_session, reuse_sessions, secure_renegotiate,
+			send_timeout, send_timeout_close, verify, verify_fun],
 		[binary, {active, false}, {packet, raw},
 			{reuseaddr, true}, {nodelay, true}])).
 

+ 9 - 2
src/ranch_tcp.erl

@@ -40,10 +40,13 @@
 
 -type opts() :: [{backlog, non_neg_integer()}
 	| {ip, inet:ip_address()}
+	| {linger, {boolean(), non_neg_integer()}}
 	| {nodelay, boolean()}
 	| {port, inet:port_number()}
 	| {raw, non_neg_integer(), non_neg_integer(),
-		non_neg_integer() | binary()}].
+		non_neg_integer() | binary()}
+	| {send_timeout, timeout()}
+	| {send_timeout_close, boolean()}].
 -export_type([opts/0]).
 
 %% @doc Name of this transport, <em>tcp</em>.
@@ -77,10 +80,14 @@ messages() -> {tcp, tcp_closed, tcp_error}.
 -spec listen(opts()) -> {ok, inet:socket()} | {error, atom()}.
 listen(Opts) ->
 	Opts2 = ranch:set_option_default(Opts, backlog, 1024),
+	Opts3 = ranch:set_option_default(Opts2, send_timeout, 30000),
+	Opts4 = ranch:set_option_default(Opts3, send_timeout_close, true),
 	%% We set the port to 0 because it is given in the Opts directly.
 	%% The port in the options takes precedence over the one in the
 	%% first argument.
-	gen_tcp:listen(0, ranch:filter_options(Opts2, [backlog, ip, nodelay, port, raw],
+	gen_tcp:listen(0, ranch:filter_options(Opts4,
+		[backlog, ip, linger, nodelay, port, raw,
+			send_timeout, send_timeout_close],
 		[binary, {active, false}, {packet, raw},
 			{reuseaddr, true}, {nodelay, true}])).