Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 30 additions & 5 deletions src/thrift_reconnecting_client.erl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
get_stats/1,
get_and_reset_stats/1 ]).

-export([ start_link/6 ]).
-export([ start_link/6,
start_link/7,
start_link/8
]).

%% gen_server callbacks
-export([ init/1,
Expand Down Expand Up @@ -57,10 +60,26 @@
start_link( Host, Port,
ThriftSvc, ThriftOpts,
ReconnMin, ReconnMax ) ->
start_link( Host, Port,
ThriftSvc, ThriftOpts,
ReconnMin, ReconnMax, 0 ).

start_link( Host, Port,
ThriftSvc, ThriftOpts,
ReconnMin, ReconnMax, InitialWait ) ->
gen_server:start_link( ?MODULE,
[ Host, Port,
ThriftSvc, ThriftOpts,
ReconnMin, ReconnMax ],
ReconnMin, ReconnMax, InitialWait ],
[] ).

start_link( ServerName, Host, Port,
ThriftSvc, ThriftOpts,
ReconnMin, ReconnMax, InitialWait ) ->
gen_server:start_link( ServerName, ?MODULE,
[ Host, Port,
ThriftSvc, ThriftOpts,
ReconnMin, ReconnMax, InitialWait ],
[] ).

call( Pid, Op, Args ) ->
Expand All @@ -83,7 +102,7 @@ get_and_reset_stats( Pid ) ->
%% {stop, Reason}
%% Description: Start the server.
%%--------------------------------------------------------------------
init( [ Host, Port, TSvc, TOpts, ReconnMin, ReconnMax ] ) ->
init( [ Host, Port, TSvc, TOpts, ReconnMin, ReconnMax, InitialWait ] ) ->
process_flag( trap_exit, true ),

State = #state{ host = Host,
Expand All @@ -95,7 +114,7 @@ init( [ Host, Port, TSvc, TOpts, ReconnMin, ReconnMax ] ) ->
op_cnt_dict = dict:new(),
op_time_dict = dict:new() },

{ ok, try_connect( State ) }.
{ ok, State, InitialWait }.

%%--------------------------------------------------------------------
%% Function: %% handle_call(Request, From, State) -> {reply, Reply, State} |
Expand Down Expand Up @@ -156,6 +175,9 @@ handle_cast( _Msg, State ) ->
%% {stop, Reason, State}
%% Description: Handling all non call/cast messages
%%--------------------------------------------------------------------
handle_info( timeout, State ) ->
{ noreply, try_connect( State ) };

handle_info( _Info, State ) ->
{ noreply, State }.

Expand All @@ -167,7 +189,10 @@ handle_info( _Info, State ) ->
%% The return value is ignored.
%%--------------------------------------------------------------------
terminate( _Reason, #state{ client = Client } ) ->
thrift_client:close( Client ),
case Client of
nil -> ok;
_ -> ( catch thrift_client:close( Client ) )
end,
ok.

%%--------------------------------------------------------------------
Expand Down