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

Remove Socket argument from ranch_protocol:start_link

Loïc Hoguin 6 лет назад
Родитель
Сommit
3ea575d868

+ 4 - 4
doc/src/guide/protocols.asciidoc

@@ -38,10 +38,10 @@ in `examples/tcp_echo/`.
 -module(echo_protocol).
 -behaviour(ranch_protocol).
 
--export([start_link/4]).
+-export([start_link/3]).
 -export([init/3]).
 
-start_link(Ref, _Socket, Transport, Opts) ->
+start_link(Ref, Transport, Opts) ->
 	Pid = spawn_link(?MODULE, init, [Ref, Transport, Opts]),
 	{ok, Pid}.
 
@@ -80,11 +80,11 @@ the normal `gen_statem` execution loop.
 -behaviour(gen_statem).
 -behaviour(ranch_protocol).
 
--export([start_link/4]).
+-export([start_link/3]).
 -export([init/1]).
 %% Exports of other gen_statem callbacks here.
 
-start_link(Ref, _Socket, Transport, Opts) ->
+start_link(Ref, Transport, Opts) ->
 	{ok, proc_lib:spawn_link(?MODULE, init, [{Ref, Transport, Opts}])}.
 
 init({Ref, Transport, _Opts = []}) ->

+ 0 - 5
doc/src/manual/ranch.handshake.asciidoc

@@ -24,11 +24,6 @@ handshake necessary to give control of the socket to this
 process and also does the transport handshake, for example
 setting up the TLS connection.
 
-Currently the socket can be obtained from a
-`Protocol:start_link/4` argument and as a return value
-from `ranch:handshake/1,2`. In Ranch 2.0 the socket will
-only be available from `ranch:handshake/1,2`.
-
 == Arguments
 
 Ref::

+ 4 - 4
doc/src/manual/ranch_protocol.asciidoc

@@ -16,7 +16,6 @@ Ranch protocols implement the following interface:
 [source,erlang]
 ----
 start_link(Ref       :: ranch:ref(),
-           _,
            Transport :: module(),
            ProtoOpts :: any())
     -> {ok, ConnPid :: pid()}
@@ -46,9 +45,10 @@ processes and degrade performance severely.
 
 == Changelog
 
-* *1.6*: The second argument `Socket` was deprecated and will
-         be removed in Ranch 2.0. The socket should be obtained
-         by calling link:man:ranch:handshake(3)[ranch:handshake(3)].
+* *2.0*: The second argument `Socket` was removed.
+* *1.6*: The second argument `Socket` was deprecated. Call
+         link:man:ranch:handshake(3)[ranch:handshake(3)]
+         to obtain the socket.
 
 == See also
 

+ 2 - 2
examples/tcp_echo/src/echo_protocol.erl

@@ -3,10 +3,10 @@
 -module(echo_protocol).
 -behaviour(ranch_protocol).
 
--export([start_link/4]).
+-export([start_link/3]).
 -export([init/3]).
 
-start_link(Ref, _Socket, Transport, Opts) ->
+start_link(Ref, Transport, Opts) ->
 	Pid = spawn_link(?MODULE, init, [Ref, Transport, Opts]),
 	{ok, Pid}.
 

+ 2 - 2
examples/tcp_reverse/src/reverse_protocol.erl

@@ -5,7 +5,7 @@
 -behaviour(ranch_protocol).
 
 %% API.
--export([start_link/4]).
+-export([start_link/3]).
 
 %% gen_statem.
 -export([callback_mode/0]).
@@ -20,7 +20,7 @@
 
 %% API.
 
-start_link(Ref, _Socket, Transport, Opts) ->
+start_link(Ref, Transport, Opts) ->
 	{ok, proc_lib:spawn_link(?MODULE, init, [{Ref, Transport, Opts}])}.
 
 %% gen_statem.

+ 1 - 1
src/ranch_conns_sup.erl

@@ -121,7 +121,7 @@ loop(State=#state{parent=Parent, ref=Ref, conn_type=ConnType,
 		max_conns=MaxConns, logger=Logger}, CurConns, NbChildren, Sleepers) ->
 	receive
 		{?MODULE, start_protocol, To, Socket} ->
-			try Protocol:start_link(Ref, Socket, Transport, Opts) of
+			try Protocol:start_link(Ref, Transport, Opts) of
 				{ok, Pid} ->
 					handshake(State, CurConns, NbChildren, Sleepers, To, Socket, Pid, Pid);
 				{ok, SupPid, ProtocolPid} when ConnType =:= supervisor ->

+ 0 - 1
src/ranch_protocol.erl

@@ -17,7 +17,6 @@
 %% Start a new connection process for the given socket.
 -callback start_link(
 		Ref::ranch:ref(),
-		Socket::any(),
 		Transport::module(),
 		ProtocolOptions::any())
 	-> {ok, ConnectionPid::pid()}

+ 2 - 2
test/active_echo_protocol.erl

@@ -1,10 +1,10 @@
 -module(active_echo_protocol).
 -behaviour(ranch_protocol).
 
--export([start_link/4]).
+-export([start_link/3]).
 -export([init/3]).
 
-start_link(Ref, _Socket, Transport, Opts) ->
+start_link(Ref, Transport, Opts) ->
 	Pid = spawn_link(?MODULE, init, [Ref, Transport, Opts]),
 	{ok, Pid}.
 

+ 7 - 6
test/check_tcp_options.erl

@@ -1,15 +1,16 @@
 -module(check_tcp_options).
 -behaviour(ranch_protocol).
 
--export([start_link/4]).
+-export([start_link/3]).
 -export([init/3]).
 
-start_link(_, Socket, _, [{pid, TestPid}|TcpOptions]) ->
-	{ok, RealTcpOptions} =
-		inet:getopts(Socket, [Key || {Key, _} <- TcpOptions]),
-	Pid = spawn_link(?MODULE, init, [TestPid, RealTcpOptions, TcpOptions]),
+start_link(Ref, _, [{pid, TestPid}|TcpOptions]) ->
+	Pid = spawn_link(?MODULE, init, [Ref, TestPid, TcpOptions]),
 	{ok, Pid}.
 
-init(Pid, TcpOptions, TcpOptions) ->
+init(Ref, Pid, TcpOptions) ->
+	{ok, Socket} = ranch:handshake(Ref),
+	{ok, RealTcpOptions} = inet:getopts(Socket, [Key || {Key, _} <- TcpOptions]),
+	true = TcpOptions =:= RealTcpOptions,
 	Pid ! checked,
 	receive after 2500 -> ok end.

+ 2 - 2
test/echo_protocol.erl

@@ -1,10 +1,10 @@
 -module(echo_protocol).
 -behaviour(ranch_protocol).
 
--export([start_link/4]).
+-export([start_link/3]).
 -export([init/3]).
 
-start_link(Ref, _Socket, Transport, Opts) ->
+start_link(Ref, Transport, Opts) ->
 	Pid = spawn_link(?MODULE, init, [Ref, Transport, Opts]),
 	{ok, Pid}.
 

+ 2 - 2
test/notify_and_wait_protocol.erl

@@ -1,10 +1,10 @@
 -module(notify_and_wait_protocol).
 -behaviour(ranch_protocol).
 
--export([start_link/4]).
+-export([start_link/3]).
 -export([init/2]).
 
-start_link(_, _, _, [{msg, Msg}, {pid, TestPid}]) ->
+start_link(_, _, [{msg, Msg}, {pid, TestPid}]) ->
 	Pid = spawn_link(?MODULE, init, [Msg, TestPid]),
 	{ok, Pid}.
 

+ 2 - 2
test/proxy_protocol.erl

@@ -1,10 +1,10 @@
 -module(proxy_protocol).
 -behaviour(ranch_protocol).
 
--export([start_link/4]).
+-export([start_link/3]).
 -export([init/3]).
 
-start_link(Ref, _Socket, Transport, Opts) ->
+start_link(Ref, Transport, Opts) ->
 	Pid = spawn_link(?MODULE, init, [Ref, Transport, Opts]),
 	{ok, Pid}.
 

+ 2 - 2
test/remove_conn_and_wait_protocol.erl

@@ -1,10 +1,10 @@
 -module(remove_conn_and_wait_protocol).
 -behaviour(ranch_protocol).
 
--export([start_link/4]).
+-export([start_link/3]).
 -export([init/3]).
 
-start_link(Ref, _, _, [{remove, MaybeRemove, Timeout}]) ->
+start_link(Ref, _, [{remove, MaybeRemove, Timeout}]) ->
 	Pid = spawn_link(?MODULE, init, [Ref, MaybeRemove, Timeout]),
 	{ok, Pid}.
 

+ 2 - 2
test/ssl_upgrade_protocol.erl

@@ -1,10 +1,10 @@
 -module(ssl_upgrade_protocol).
 -behaviour(ranch_protocol).
 
--export([start_link/4]).
+-export([start_link/3]).
 -export([init/3]).
 
-start_link(Ref, _Socket, Transport, Opts) ->
+start_link(Ref, Transport, Opts) ->
 	Pid = spawn_link(?MODULE, init, [Ref, Transport, Opts]),
 	{ok, Pid}.
 

+ 3 - 3
test/supervisor_separate.erl

@@ -2,13 +2,13 @@
 -behavior(supervisor).
 -behavior(ranch_protocol).
 
--export([start_link/4]).
+-export([start_link/3]).
 -export([init/1]).
 
-start_link(Ref, Socket, Transport, Opts) ->
+start_link(Ref, Transport, Opts) ->
 	{ok, SupPid} = supervisor:start_link(?MODULE, []),
 	{ok, ConnPid} = supervisor:start_child(SupPid,
-		{echo_protocol, {echo_protocol, start_link, [Ref, Socket, Transport, Opts]},
+		{echo_protocol, {echo_protocol, start_link, [Ref, Transport, Opts]},
 			temporary, 5000, worker, [echo_protocol]}),
 	{ok, SupPid, ConnPid}.
 

+ 2 - 2
test/transport_capabilities_protocol.erl

@@ -1,10 +1,10 @@
 -module(transport_capabilities_protocol).
 -behaviour(ranch_protocol).
 
--export([start_link/4]).
+-export([start_link/3]).
 -export([init/3]).
 
-start_link(Ref, _Socket, Transport, Opts) ->
+start_link(Ref, Transport, Opts) ->
 	Pid = spawn_link(?MODULE, init, [Ref, Transport, Opts]),
 	{ok, Pid}.
 

+ 2 - 2
test/trap_exit_protocol.erl

@@ -1,10 +1,10 @@
 -module(trap_exit_protocol).
 -behaviour(ranch_protocol).
 
--export([start_link/4]).
+-export([start_link/3]).
 -export([init/3]).
 
-start_link(Ref, _Socket, Transport, Opts) ->
+start_link(Ref, Transport, Opts) ->
 	Pid = spawn_link(?MODULE, init, [Ref, Transport, Opts]),
 	{ok, Pid}.