You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by dr...@apache.org on 2008/06/11 03:01:21 UTC

svn commit: r666444 - in /incubator/thrift/trunk/lib/alterl/src: thrift_buffered_transport.erl thrift_client.erl

Author: dreiss
Date: Tue Jun 10 18:01:21 2008
New Revision: 666444

URL: http://svn.apache.org/viewvc?rev=666444&view=rev
Log:
small buffered_transport and client improvements

moved close of wrapped transport into terminate/2
made thrift_client:close into a cast rather than call

Modified:
    incubator/thrift/trunk/lib/alterl/src/thrift_buffered_transport.erl
    incubator/thrift/trunk/lib/alterl/src/thrift_client.erl

Modified: incubator/thrift/trunk/lib/alterl/src/thrift_buffered_transport.erl
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/alterl/src/thrift_buffered_transport.erl?rev=666444&r1=666443&r2=666444&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/alterl/src/thrift_buffered_transport.erl (original)
+++ incubator/thrift/trunk/lib/alterl/src/thrift_buffered_transport.erl Tue Jun 10 18:01:21 2008
@@ -20,16 +20,13 @@
 %% thrift_transport callbacks
 -export([write/2, read/2, flush/1, close/1]).
 
--record(state, {
-          % The wrapped transport
-          wrapped,
-
-          % a list of binaries which will be concatenated and sent during
-          % a flush.
-          %
-          % *** THIS LIST IS STORED IN REVERSE ORDER!!! ***
-          %
-          buffer}).
+-record(buffered_transport, {wrapped, % a thrift_transport
+                             buffer
+                             %% a list of binaries which will be concatenated and sent during
+                             %% a flush.
+                             %%
+                             %% *** THIS LIST IS STORED IN REVERSE ORDER!!! ***
+                            }).
 
 %%====================================================================
 %% API
@@ -70,7 +67,7 @@
 %% Description: Closes the transport and the wrapped transport
 %%--------------------------------------------------------------------
 close(Transport) ->
-    gen_server:call(Transport, close).
+    gen_server:cast(Transport, close).
 
 %%--------------------------------------------------------------------
 %% Function: Read(Transport, Len) -> {ok, Data}
@@ -96,8 +93,8 @@
 init([Wrapped]) ->
     %% TODO(cpiro): need to trap exits here so when transport exits
     %% normally from under our feet we exit normally
-    {ok, #state{wrapped = Wrapped,
-                buffer = []}}.
+    {ok, #buffered_transport{wrapped = Wrapped,
+                             buffer = []}}.
 
 %%--------------------------------------------------------------------
 %% Function: %% handle_call(Request, From, State) -> {reply, Reply, State} |
@@ -108,25 +105,19 @@
 %%                                      {stop, Reason, State}
 %% Description: Handling call messages
 %%--------------------------------------------------------------------
-handle_call({write, Data}, _From, State = #state{buffer = Buffer}) ->
-    {reply, ok, State#state{buffer = [Data | Buffer]}};
+handle_call({write, Data}, _From, State = #buffered_transport{buffer = Buffer}) ->
+    {reply, ok, State#buffered_transport{buffer = [Data | Buffer]}};
 
-handle_call({read, Len}, _From, State = #state{wrapped = Wrapped}) ->
+handle_call({read, Len}, _From, State = #buffered_transport{wrapped = Wrapped}) ->
     Response = thrift_transport:read(Wrapped, Len),
     {reply, Response, State};
 
-handle_call(flush, _From, State = #state{buffer = Buffer,
-                                         wrapped = Wrapped}) ->
+handle_call(flush, _From, State = #buffered_transport{buffer = Buffer,
+                                                      wrapped = Wrapped}) ->
     Concat   = concat_binary(lists:reverse(Buffer)),
     Response = thrift_transport:write(Wrapped, Concat),
     thrift_transport:flush(Wrapped),
-    {reply, Response, State#state{buffer = []}};
-
-handle_call(close, _From, State = #state{buffer  = Buffer,
-                                         wrapped = Wrapped}) ->
-    thrift_transport:write(Wrapped, concat_binary(lists:reverse(Buffer))),
-    Close=thrift_transport:close(Wrapped),
-    {stop, shutdown, Close, State}.
+    {reply, Response, State#buffered_transport{buffer = []}}.
 
 %%--------------------------------------------------------------------
 %% Function: handle_cast(Msg, State) -> {noreply, State} |
@@ -134,7 +125,13 @@
 %%                                      {stop, Reason, State}
 %% Description: Handling cast messages
 %%--------------------------------------------------------------------
-handle_cast(Msg, State=#state{}) ->
+handle_cast(close, State = #buffered_transport{buffer = Buffer,
+                                               wrapped = Wrapped}) ->
+    thrift_transport:write(Wrapped, concat_binary(lists:reverse(Buffer))),
+    %% Wrapped is closed by terminate/2
+    %%  error_logger:info_msg("thrift_buffered_transport ~p: closing", [self()]),
+    {stop, normal, State};
+handle_cast(Msg, State=#buffered_transport{}) ->
     {noreply, State}.
 
 %%--------------------------------------------------------------------
@@ -153,7 +150,8 @@
 %% cleaning up. When it returns, the gen_server terminates with Reason.
 %% The return value is ignored.
 %%--------------------------------------------------------------------
-terminate(_Reason, _State) ->
+terminate(_Reason, State = #buffered_transport{wrapped=Wrapped}) ->
+    thrift_transport:close(Wrapped),
     ok.
 
 %%--------------------------------------------------------------------

Modified: incubator/thrift/trunk/lib/alterl/src/thrift_client.erl
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/alterl/src/thrift_client.erl?rev=666444&r1=666443&r2=666444&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/alterl/src/thrift_client.erl (original)
+++ incubator/thrift/trunk/lib/alterl/src/thrift_client.erl Tue Jun 10 18:01:21 2008
@@ -44,7 +44,7 @@
     end.
 
 close(Client) when is_pid(Client) ->
-    gen_server:call(Client, close).
+    gen_server:cast(Client, close).
 
 %%====================================================================
 %% gen_server callbacks
@@ -104,10 +104,7 @@
                 end
         end,
 
-    {reply, Result, State};
-
-handle_call(close, _From, State = #state{protocol = Protocol}) ->
-    {stop, shutdown, ok, State}.
+    {reply, Result, State}.
 
 %%--------------------------------------------------------------------
 %% Function: handle_cast(Msg, State) -> {noreply, State} |
@@ -115,6 +112,9 @@
 %%                                      {stop, Reason, State}
 %% Description: Handling cast messages
 %%--------------------------------------------------------------------
+handle_cast(close, State=#state{protocol = Protocol}) ->
+%%     error_logger:info_msg("thrift_client ~p received close", [self()]),
+    {stop,normal,State};
 handle_cast(_Msg, State) ->
     {noreply, State}.
 
@@ -134,7 +134,8 @@
 %% cleaning up. When it returns, the gen_server terminates with Reason.
 %% The return value is ignored.
 %%--------------------------------------------------------------------
-terminate(_Reason, State = #state{protocol = Protocol}) ->
+terminate(Reason, State = #state{protocol = Protocol}) ->
+%%     error_logger:info_msg("thrift_client ~p terminating due to ~p", [self(), Reason]),
     thrift_protocol:close_transport(Protocol),
     ok.