-
Notifications
You must be signed in to change notification settings - Fork 39
allow client to reconnect if other side closes connection #478
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cybernetlab
wants to merge
13
commits into
pinterest:master
Choose a base branch
from
cybernetlab:allow-reconnect
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+117
−18
Open
Changes from 6 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
0dfad21
allow client to reconnect if other side closes connection
cybernetlab d3b0edc
format fixed, added cleaning sock in state before reconnect
cybernetlab ea2e246
added: resending data after succesfull reconnect
cybernetlab 6f97f56
connection is active now, tests added
cybernetlab a0be2d9
format fixed
cybernetlab 719ad3f
changed: separate handling of incoming messages for tcp and ssl
cybernetlab 29bca94
removed: resending data after reconnect
cybernetlab 3120978
removed: resending data after reconnect
cybernetlab 2db60eb
handle_info fixed
cybernetlab 0827acf
handle_info fixed
cybernetlab 64bd3ad
:reconnect option added to start_link/3 docs
cybernetlab 46ce001
:reconnect option doc typo fixed
cybernetlab a1c23e3
:reconnect option doc typo fixed
cybernetlab File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,7 +21,7 @@ defmodule Thrift.Binary.Framed.Client do | |
| alias Thrift.TApplicationException | ||
| alias Thrift.Transport.SSL | ||
|
|
||
| @immutable_tcp_opts [active: false, packet: 4, mode: :binary] | ||
| @immutable_tcp_opts [active: true, packet: 4, mode: :binary] | ||
|
|
||
| @type error :: {:error, atom} | {:error, {:exception, struct}} | ||
| @type success :: {:ok, binary} | ||
|
|
@@ -42,6 +42,7 @@ defmodule Thrift.Binary.Framed.Client do | |
| {:tcp_opts, [tcp_option]} | ||
| | {:ssl_opts, [SSL.option()]} | ||
| | {:gen_server_opts, [genserver_call_option]} | ||
| | {:reconnect, boolean} | ||
|
|
||
| @type options :: [option] | ||
|
|
||
|
|
@@ -55,7 +56,8 @@ defmodule Thrift.Binary.Framed.Client do | |
| ssl_opts: [SSL.option()], | ||
| timeout: integer, | ||
| sock: {:gen_tcp, :gen_tcp.socket()} | {:ssl, :ssl.sslsocket()}, | ||
| seq_id: integer | ||
| seq_id: integer, | ||
| reconnect: boolean | ||
| } | ||
| defstruct host: nil, | ||
| port: nil, | ||
|
|
@@ -64,7 +66,8 @@ defmodule Thrift.Binary.Framed.Client do | |
| ssl_opts: nil, | ||
| timeout: 5000, | ||
| sock: nil, | ||
| seq_id: 0 | ||
| seq_id: 0, | ||
| reconnect: false | ||
| end | ||
|
|
||
| require Logger | ||
|
|
@@ -74,6 +77,7 @@ defmodule Thrift.Binary.Framed.Client do | |
| def init({host, port, opts}) do | ||
| tcp_opts = Keyword.get(opts, :tcp_opts, []) | ||
| ssl_opts = Keyword.get(opts, :ssl_opts, []) | ||
| reconnect = Keyword.get(opts, :reconnect, false) | ||
|
|
||
| {timeout, tcp_opts} = Keyword.pop(tcp_opts, :timeout, 5000) | ||
|
|
||
|
|
@@ -82,7 +86,8 @@ defmodule Thrift.Binary.Framed.Client do | |
| port: port, | ||
| tcp_opts: tcp_opts, | ||
| ssl_opts: ssl_opts, | ||
| timeout: timeout | ||
| timeout: timeout, | ||
| reconnect: reconnect | ||
| } | ||
|
|
||
| {:connect, :init, s} | ||
|
|
@@ -137,15 +142,20 @@ defmodule Thrift.Binary.Framed.Client do | |
| def close(conn), do: Connection.call(conn, :close) | ||
|
|
||
| @impl Connection | ||
| def connect(_info, %{sock: nil, host: host, port: port, tcp_opts: opts, timeout: timeout} = s) do | ||
| def connect(info, %{sock: nil, host: host, port: port, tcp_opts: opts, timeout: timeout} = s) do | ||
| opts = | ||
| opts | ||
| |> Keyword.merge(@immutable_tcp_opts) | ||
| |> Keyword.put_new(:send_timeout, 1000) | ||
|
|
||
| # reset sequence id for newly created connection | ||
| s = %{s | seq_id: 0} | ||
|
|
||
| case :gen_tcp.connect(host, port, opts, timeout) do | ||
| {:ok, sock} -> | ||
| maybe_ssl_handshake(sock, host, port, s) | ||
| sock | ||
| |> maybe_ssl_handshake(host, port, s) | ||
| |> maybe_resend_data(info) | ||
|
|
||
| {:error, :timeout} = error -> | ||
| Logger.error("Failed to connect to #{host}:#{port} due to timeout after #{timeout}ms") | ||
|
|
@@ -158,10 +168,13 @@ defmodule Thrift.Binary.Framed.Client do | |
| end | ||
|
|
||
| @impl Connection | ||
| def disconnect(info, %{sock: {transport, sock}}) do | ||
| def disconnect(info, %{sock: {transport, sock}} = s) do | ||
| :ok = transport.close(sock) | ||
|
|
||
| case info do | ||
| {:reconnect, _} -> | ||
| {:connect, info, %{s | sock: nil}} | ||
|
|
||
| {:close, from} -> | ||
| Connection.reply(from, :ok) | ||
| {:stop, :normal, nil} | ||
|
|
@@ -244,19 +257,31 @@ defmodule Thrift.Binary.Framed.Client do | |
| end | ||
|
|
||
| def handle_call( | ||
| {:call, rpc_name, serialized_args, tcp_opts}, | ||
| _, | ||
| %{sock: {transport, sock}, seq_id: seq_id, timeout: default_timeout} = s | ||
| {:call, rpc_name, serialized_args, tcp_opts} = msg, | ||
| from, | ||
| %{ | ||
| sock: {transport, sock}, | ||
| seq_id: seq_id, | ||
| timeout: default_timeout, | ||
| reconnect: reconnect | ||
| } = s | ||
| ) do | ||
| s = %{s | seq_id: seq_id + 1} | ||
| message = Binary.serialize(:message_begin, {:call, seq_id, rpc_name}) | ||
| timeout = Keyword.get(tcp_opts, :timeout, default_timeout) | ||
|
|
||
| with :ok <- transport.send(sock, [message | serialized_args]), | ||
| {:ok, message} <- transport.recv(sock, 0, timeout) do | ||
| {:ok, message} <- receive_message(transport, sock, timeout) do | ||
| reply = deserialize_message_reply(message, rpc_name, seq_id) | ||
| {:reply, reply, s} | ||
| else | ||
| {:error, :closed} = error -> | ||
| if reconnect do | ||
| {:disconnect, {:reconnect, {:call, msg, from}}, s} | ||
| else | ||
| {:disconnect, error, error, s} | ||
| end | ||
|
|
||
| {:error, :timeout} = error -> | ||
| {:disconnect, {:error, :timeout, timeout}, error, s} | ||
|
|
||
|
|
@@ -276,8 +301,8 @@ defmodule Thrift.Binary.Framed.Client do | |
| end | ||
|
|
||
| def handle_cast( | ||
| {:oneway, rpc_name, serialized_args}, | ||
| %{sock: {transport, sock}, seq_id: seq_id} = s | ||
| {:oneway, rpc_name, serialized_args} = msg, | ||
| %{sock: {transport, sock}, seq_id: seq_id, reconnect: reconnect} = s | ||
| ) do | ||
| s = %{s | seq_id: seq_id + 1} | ||
| message = Binary.serialize(:message_begin, {:oneway, seq_id, rpc_name}) | ||
|
|
@@ -286,15 +311,51 @@ defmodule Thrift.Binary.Framed.Client do | |
| :ok -> | ||
| {:noreply, s} | ||
|
|
||
| {:error, :closed} = error -> | ||
| if reconnect do | ||
| {:disconnect, {:reconnect, {:cast, msg}}, s} | ||
| else | ||
| {:disconnect, error, s} | ||
| end | ||
|
|
||
| {:error, _} = error -> | ||
| {:disconnect, error, s} | ||
| end | ||
| end | ||
|
|
||
| @impl Connection | ||
| def handle_info({:tcp_closed, sock}, %{reconnect: true, sock: {_transport, sock}} = s) do | ||
| {:disconnect, {:reconnect, nil}, s} | ||
| end | ||
|
|
||
| def handle_info(_, s) do | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should handle the |
||
| {:noreply, s} | ||
| end | ||
|
|
||
| def deserialize_message_reply(message, rpc_name, seq_id) do | ||
| handle_message(Binary.deserialize(:message_begin, message), seq_id, rpc_name) | ||
| end | ||
|
|
||
| defp receive_message(:gen_tcp, sock, timeout) do | ||
| receive do | ||
| {:tcp, ^sock, data} -> {:ok, data} | ||
| {:tcp_closed, ^sock} -> {:error, :closed} | ||
| {:tcp_error, ^sock, error} -> {:error, error} | ||
| after | ||
| timeout -> {:error, :timeout} | ||
| end | ||
| end | ||
|
|
||
| defp receive_message(:ssl, sock, timeout) do | ||
| receive do | ||
| {:ssl, ^sock, data} -> {:ok, data} | ||
| {:ssl_closed, ^sock} -> {:error, :closed} | ||
| {:ssl_error, ^sock, error} -> {:error, error} | ||
| after | ||
| timeout -> {:error, :timeout} | ||
| end | ||
| end | ||
cybernetlab marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| defp handle_message({:ok, {:reply, seq_id, rpc_name, serialized_response}}, seq_id, rpc_name) do | ||
| {:ok, serialized_response} | ||
| end | ||
|
|
@@ -372,4 +433,34 @@ defmodule Thrift.Binary.Framed.Client do | |
| {:stop, error, s} | ||
| end | ||
| end | ||
|
|
||
| defp maybe_resend_data({:ok, s}, {:reconnect, {:call, msg, from}}) do | ||
| case handle_call(msg, from, s) do | ||
| {:reply, reply, s} -> | ||
| GenServer.reply(from, reply) | ||
| {:ok, s} | ||
|
|
||
| {:disconnect, info, error, s} -> | ||
| GenServer.reply(from, error) | ||
| disconnect(info, s) | ||
|
|
||
| _ -> | ||
cybernetlab marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| {:ok, s} | ||
| end | ||
| end | ||
|
|
||
| defp maybe_resend_data({:ok, s}, {:reconnect, {:cast, msg}}) do | ||
| case handle_cast(msg, s) do | ||
| {:noreply, s} -> | ||
| {:ok, s} | ||
|
|
||
| {:disconnect, info, s} -> | ||
| disconnect(info, s) | ||
|
|
||
| _ -> | ||
cybernetlab marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| {:ok, s} | ||
| end | ||
| end | ||
|
|
||
| defp maybe_resend_data(reply, _), do: reply | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.