mirror of
https://github.com/valitydev/thrift.git
synced 2024-11-07 10:48:51 +00:00
Add thrift_client:send_call which sends a function call but doesn't read a response.
Summary: This is for logging applications with thrift_disk_log_transport, so the function calls logged don't necessarily have to be async void Test plan: Added to test_disklog.erl git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@666465 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
6d477592fa
commit
65cf720b19
@ -10,7 +10,7 @@
|
|||||||
-behaviour(gen_server).
|
-behaviour(gen_server).
|
||||||
|
|
||||||
%% API
|
%% API
|
||||||
-export([start_link/2, start_link/3, start_link/4, call/3, close/1]).
|
-export([start_link/2, start_link/3, start_link/4, call/3, send_call/3, close/1]).
|
||||||
|
|
||||||
%% gen_server callbacks
|
%% gen_server callbacks
|
||||||
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
|
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
|
||||||
@ -93,6 +93,14 @@ call(Client, Function, Args)
|
|||||||
{exception, Exception} -> throw(Exception)
|
{exception, Exception} -> throw(Exception)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
%% Sends a function call but does not read the result. This is useful
|
||||||
|
%% if you're trying to log non-async function calls to write-only
|
||||||
|
%% transports like thrift_disk_log_transport.
|
||||||
|
send_call(Client, Function, Args)
|
||||||
|
when is_pid(Client), is_atom(Function), is_list(Args) ->
|
||||||
|
gen_server:call(Client, {send_call, Function, Args}).
|
||||||
|
|
||||||
|
|
||||||
close(Client) when is_pid(Client) ->
|
close(Client) when is_pid(Client) ->
|
||||||
gen_server:cast(Client, close).
|
gen_server:cast(Client, close).
|
||||||
|
|
||||||
@ -129,13 +137,30 @@ handle_call({connect, ProtocolFactory}, _From,
|
|||||||
{stop, normal, Error, State}
|
{stop, normal, Error, State}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
handle_call({call, Function, Args}, _From, State = #state{service = Service,
|
handle_call({call, Function, Args}, _From, State = #state{service = Service}) ->
|
||||||
protocol = Protocol,
|
Result = catch_function_exceptions(
|
||||||
seqid = SeqId}) ->
|
fun() ->
|
||||||
Result =
|
|
||||||
try
|
|
||||||
ok = send_function_call(State, Function, Args),
|
ok = send_function_call(State, Function, Args),
|
||||||
receive_function_result(State, Function)
|
receive_function_result(State, Function)
|
||||||
|
end,
|
||||||
|
Service),
|
||||||
|
{reply, Result, State};
|
||||||
|
|
||||||
|
|
||||||
|
handle_call({send_call, Function, Args}, _From, State = #state{service = Service}) ->
|
||||||
|
Result = catch_function_exceptions(
|
||||||
|
fun() ->
|
||||||
|
send_function_call(State, Function, Args)
|
||||||
|
end,
|
||||||
|
Service),
|
||||||
|
{reply, Result, State}.
|
||||||
|
|
||||||
|
|
||||||
|
%% Helper function that catches exceptions thrown by sending or receiving
|
||||||
|
%% a function and returns the correct response for call or send_only above.
|
||||||
|
catch_function_exceptions(Fun, Service) ->
|
||||||
|
try
|
||||||
|
Fun()
|
||||||
catch
|
catch
|
||||||
throw:{return, Return} ->
|
throw:{return, Return} ->
|
||||||
Return;
|
Return;
|
||||||
@ -146,9 +171,8 @@ handle_call({call, Function, Args}, _From, State = #state{service = Service,
|
|||||||
{error, {no_function, Function}};
|
{error, {no_function, Function}};
|
||||||
_ -> throw({error, {function_clause, ST}})
|
_ -> throw({error, {function_clause, ST}})
|
||||||
end
|
end
|
||||||
end,
|
end.
|
||||||
|
|
||||||
{reply, Result, State}.
|
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Function: handle_cast(Msg, State) -> {noreply, State} |
|
%% Function: handle_cast(Msg, State) -> {noreply, State} |
|
||||||
|
@ -13,11 +13,16 @@ t() ->
|
|||||||
{ok, Client} = thrift_client:start_link(ProtocolFactory, thriftTest_thrift),
|
{ok, Client} = thrift_client:start_link(ProtocolFactory, thriftTest_thrift),
|
||||||
|
|
||||||
io:format("Client started~n"),
|
io:format("Client started~n"),
|
||||||
|
|
||||||
% We have to make async calls into this client only since otherwise it will try
|
% We have to make async calls into this client only since otherwise it will try
|
||||||
% to read from the disklog and go boom.
|
% to read from the disklog and go boom.
|
||||||
{ok, ok} = thrift_client:call(Client, testAsync, [16#deadbeef]),
|
{ok, ok} = thrift_client:call(Client, testAsync, [16#deadbeef]),
|
||||||
io:format("Call written~n"),
|
io:format("Call written~n"),
|
||||||
|
|
||||||
|
% Use the send_call method to write a non-async call into the log
|
||||||
|
ok = thrift_client:send_call(Client, testString, [<<"hello world">>]),
|
||||||
|
io:format("Non-async call sent~n"),
|
||||||
|
|
||||||
ok = thrift_client:close(Client),
|
ok = thrift_client:close(Client),
|
||||||
io:format("Client closed~n"),
|
io:format("Client closed~n"),
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user