summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Reiss <dreiss@apache.org>2008-06-11 01:12:20 +0000
committerDavid Reiss <dreiss@apache.org>2008-06-11 01:12:20 +0000
commit65cf720b19f2dc2fc6a9cd18ee274a34f87c96b5 (patch)
tree8a70557e02451bd628f1e929a59f178cc606ea90
parent6d477592fa05ec88d8cbcb75d62a65b98a87cf13 (diff)
downloadthrift-65cf720b19f2dc2fc6a9cd18ee274a34f87c96b5.tar.gz
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
-rw-r--r--lib/alterl/src/thrift_client.erl64
-rw-r--r--test/erl/src/test_disklog.erl5
2 files changed, 49 insertions, 20 deletions
diff --git a/lib/alterl/src/thrift_client.erl b/lib/alterl/src/thrift_client.erl
index 5099829bb..d6508bb21 100644
--- a/lib/alterl/src/thrift_client.erl
+++ b/lib/alterl/src/thrift_client.erl
@@ -10,7 +10,7 @@
-behaviour(gen_server).
%% 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
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
@@ -93,6 +93,14 @@ call(Client, Function, Args)
{exception, Exception} -> throw(Exception)
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) ->
gen_server:cast(Client, close).
@@ -129,27 +137,43 @@ handle_call({connect, ProtocolFactory}, _From,
{stop, normal, Error, State}
end;
-handle_call({call, Function, Args}, _From, State = #state{service = Service,
- protocol = Protocol,
- seqid = SeqId}) ->
- Result =
- try
- ok = send_function_call(State, Function, Args),
- receive_function_result(State, Function)
- catch
- throw:{return, Return} ->
- Return;
- error:function_clause ->
- ST = erlang:get_stacktrace(),
- case hd(ST) of
- {Service, function_info, [Function, _]} ->
- {error, {no_function, Function}};
- _ -> throw({error, {function_clause, ST}})
- end
- end,
-
+handle_call({call, Function, Args}, _From, State = #state{service = Service}) ->
+ Result = catch_function_exceptions(
+ fun() ->
+ ok = send_function_call(State, Function, Args),
+ 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
+ throw:{return, Return} ->
+ Return;
+ error:function_clause ->
+ ST = erlang:get_stacktrace(),
+ case hd(ST) of
+ {Service, function_info, [Function, _]} ->
+ {error, {no_function, Function}};
+ _ -> throw({error, {function_clause, ST}})
+ end
+ end.
+
+
%%--------------------------------------------------------------------
%% Function: handle_cast(Msg, State) -> {noreply, State} |
%% {noreply, State, Timeout} |
diff --git a/test/erl/src/test_disklog.erl b/test/erl/src/test_disklog.erl
index 0044b832a..78b792c4d 100644
--- a/test/erl/src/test_disklog.erl
+++ b/test/erl/src/test_disklog.erl
@@ -13,11 +13,16 @@ t() ->
{ok, Client} = thrift_client:start_link(ProtocolFactory, thriftTest_thrift),
io:format("Client started~n"),
+
% We have to make async calls into this client only since otherwise it will try
% to read from the disklog and go boom.
{ok, ok} = thrift_client:call(Client, testAsync, [16#deadbeef]),
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),
io:format("Client closed~n"),