-
Notifications
You must be signed in to change notification settings - Fork 44
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
allow client to reconnect if other side closes connection #478
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.
Open
Changes from all 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 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} | ||
|
@@ -124,6 +129,9 @@ defmodule Thrift.Binary.Framed.Client do | |
Additionally, the options `:name`, `:debug`, and `:spawn_opt`, if specified, | ||
will be passed to the underlying `GenServer`. See `GenServer.start_link/3` | ||
for details on these options. | ||
|
||
The `:reconnect` option if set to `true` forces client to reopen TCP connection whenever | ||
it closed. | ||
""" | ||
@spec start_link(String.t(), 0..65_535, options) :: GenServer.on_start() | ||
def start_link(host, port, opts) do | ||
|
@@ -143,6 +151,9 @@ defmodule Thrift.Binary.Framed.Client do | |
|> 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) | ||
|
@@ -158,10 +169,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} | ||
|
@@ -245,15 +259,15 @@ defmodule Thrift.Binary.Framed.Client do | |
|
||
def handle_call( | ||
{:call, rpc_name, serialized_args, tcp_opts}, | ||
_, | ||
_from, | ||
%{sock: {transport, sock}, seq_id: seq_id, timeout: default_timeout} = 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 | ||
|
@@ -291,10 +305,59 @@ defmodule Thrift.Binary.Framed.Client do | |
end | ||
end | ||
|
||
@impl Connection | ||
def handle_info({:tcp, sock, _data}, %{reconnect: true, sock: {:gen_tcp, sock}} = s) do | ||
{:disconnect, :reconnect, s} | ||
end | ||
|
||
def handle_info({:tcp_closed, sock}, %{reconnect: true, sock: {:gen_tcp, sock}} = s) do | ||
{:disconnect, :reconnect, s} | ||
end | ||
|
||
def handle_info({:tcp_error, sock, _error}, %{reconnect: true, sock: {:gen_tcp, sock}} = s) do | ||
{:disconnect, :reconnect, s} | ||
end | ||
|
||
def handle_info({:ssl, sock, _data}, %{reconnect: true, sock: {:ssl, sock}} = s) do | ||
{:disconnect, :reconnect, s} | ||
end | ||
|
||
def handle_info({:ssl_closed, sock}, %{reconnect: true, sock: {:ssl, sock}} = s) do | ||
{:disconnect, :reconnect, s} | ||
end | ||
|
||
def handle_info({:ssl_error, sock, _error}, %{reconnect: true, sock: {:ssl, sock}} = s) do | ||
{:disconnect, :reconnect, s} | ||
end | ||
|
||
def handle_info(_, s) do | ||
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 | ||
|
This file contains 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
This file contains 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
This file contains 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should always disconnect and make the decision on reconnect there. If
reconnect: false
we want to disconnect too.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case many tests fails. If I disconnect on any TCP or SSL message with
reconnect: false
following output produced bymix test
:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure - should I correct these test cases?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, any comments about my last question?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to think about we handle these cases, and handle them.
For example we could do
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My opinion is not to handle these cases and let users of library to handle it. But in this case some tests should be rewritten