]> granicus.if.org Git - ejabberd/commitdiff
Hibernate ejabberd_receiver and ejabberd_frontend_socket processes after timeout
authorChristophe Romain <christophe.romain@process-one.net>
Mon, 1 Dec 2008 15:16:40 +0000 (15:16 +0000)
committerChristophe Romain <christophe.romain@process-one.net>
Mon, 1 Dec 2008 15:16:40 +0000 (15:16 +0000)
SVN Revision: 1693

ChangeLog
src/ejabberd_frontend_socket.erl
src/ejabberd_receiver.erl

index b0c2c10125ec30f7eaf9b6730fbabeb028a6c25f..9a68913fcf44890e1e6957a5d4d93103a1c26d65 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2008-10-17  Christophe Romain <christophe.romain@process-one.net>
+
+       * src/ejabberd_frontend_socket.erl: Hibernate ejabberd_receiver and
+       ejabberd_frontend_socket processes after timeout. Backported from
+       Aleksey's patch (r1680)
+       * src/ejabberd_frontend_socket.erl: Likewise
+
 2008-11-26  Badlop  <badlop@process-one.net>
 
        * src/mod_irc/mod_irc.erl: Announce disco#info (thanks to Spike)
index 320968f70fa8c85500f39499c7592e215975fb9b..7ea0b5d3532cbd4061e082da2804cd3fd8e5f907 100644 (file)
@@ -53,6 +53,8 @@
 
 -record(state, {sockmod, socket, receiver}).
 
+-define(HIBERNATE_TIMEOUT, 90000).
+
 %%====================================================================
 %% API
 %%====================================================================
@@ -173,7 +175,8 @@ handle_call({starttls, TLSOpts}, _From, State) ->
     {ok, TLSSocket} = tls:tcp_to_tls(State#state.socket, TLSOpts),
     ejabberd_receiver:starttls(State#state.receiver, TLSSocket),
     Reply = ok,
-    {reply, Reply, State#state{socket = TLSSocket, sockmod = tls}};
+    {reply, Reply, State#state{socket = TLSSocket, sockmod = tls},
+     ?HIBERNATE_TIMEOUT};
 
 handle_call({starttls, TLSOpts, Data}, _From, State) ->
     {ok, TLSSocket} = tls:tcp_to_tls(State#state.socket, TLSOpts),
@@ -181,7 +184,8 @@ handle_call({starttls, TLSOpts, Data}, _From, State) ->
     catch (State#state.sockmod):send(
            State#state.socket, Data),
     Reply = ok,
-    {reply, Reply, State#state{socket = TLSSocket, sockmod = tls}};
+    {reply, Reply, State#state{socket = TLSSocket, sockmod = tls},
+     ?HIBERNATE_TIMEOUT};
 
 handle_call(compress, _From, State) ->
     {ok, ZlibSocket} = ejabberd_zlib:enable_zlib(
@@ -189,7 +193,8 @@ handle_call(compress, _From, State) ->
                         State#state.socket),
     ejabberd_receiver:compress(State#state.receiver, ZlibSocket),
     Reply = ok,
-    {reply, Reply, State#state{socket = ZlibSocket, sockmod = ejabberd_zlib}};
+    {reply, Reply, State#state{socket = ZlibSocket, sockmod = ejabberd_zlib},
+     ?HIBERNATE_TIMEOUT};
 
 handle_call({compress, Data}, _From, State) ->
     {ok, ZlibSocket} = ejabberd_zlib:enable_zlib(
@@ -199,35 +204,36 @@ handle_call({compress, Data}, _From, State) ->
     catch (State#state.sockmod):send(
            State#state.socket, Data),
     Reply = ok,
-    {reply, Reply, State#state{socket = ZlibSocket, sockmod = ejabberd_zlib}};
+    {reply, Reply, State#state{socket = ZlibSocket, sockmod = ejabberd_zlib},
+     ?HIBERNATE_TIMEOUT};
 
 handle_call(reset_stream, _From, State) ->
     ejabberd_receiver:reset_stream(State#state.receiver),
     Reply = ok,
-    {reply, Reply, State};
+    {reply, Reply, State, ?HIBERNATE_TIMEOUT};
 
 handle_call({send, Data}, _From, State) ->
     catch (State#state.sockmod):send(
            State#state.socket, Data),
     Reply = ok,
-    {reply, Reply, State};
+    {reply, Reply, State, ?HIBERNATE_TIMEOUT};
 
 handle_call({change_shaper, Shaper}, _From, State) ->
     ejabberd_receiver:change_shaper(State#state.receiver, Shaper),
     Reply = ok,
-    {reply, Reply, State};
+    {reply, Reply, State, ?HIBERNATE_TIMEOUT};
 
 handle_call(get_sockmod, _From, State) ->
     Reply = State#state.sockmod,
-    {reply, Reply, State};
+    {reply, Reply, State, ?HIBERNATE_TIMEOUT};
 
 handle_call(get_peer_certificate, _From, State) ->
     Reply = tls:get_peer_certificate(State#state.socket),
-    {reply, Reply, State};
+    {reply, Reply, State, ?HIBERNATE_TIMEOUT};
 
 handle_call(get_verify_result, _From, State) ->
     Reply = tls:get_verify_result(State#state.socket),
-    {reply, Reply, State};
+    {reply, Reply, State, ?HIBERNATE_TIMEOUT};
 
 handle_call(close, _From, State) ->
     ejabberd_receiver:close(State#state.receiver),
@@ -243,7 +249,7 @@ handle_call(sockname, _From, State) ->
            _ ->
                SockMod:sockname(Socket)
        end,
-    {reply, Reply, State};
+    {reply, Reply, State, ?HIBERNATE_TIMEOUT};
 
 handle_call(peername, _From, State) ->
     #state{sockmod = SockMod, socket = Socket} = State,
@@ -254,11 +260,11 @@ handle_call(peername, _From, State) ->
            _ ->
                SockMod:peername(Socket)
        end,
-    {reply, Reply, State};
+    {reply, Reply, State, ?HIBERNATE_TIMEOUT};
 
 handle_call(_Request, _From, State) ->
     Reply = ok,
-    {reply, Reply, State}.
+    {reply, Reply, State, ?HIBERNATE_TIMEOUT}.
 
 %%--------------------------------------------------------------------
 %% Function: handle_cast(Msg, State) -> {noreply, State} |
@@ -267,7 +273,7 @@ handle_call(_Request, _From, State) ->
 %% Description: Handling cast messages
 %%--------------------------------------------------------------------
 handle_cast(_Msg, State) ->
-    {noreply, State}.
+    {noreply, State, ?HIBERNATE_TIMEOUT}.
 
 %%--------------------------------------------------------------------
 %% Function: handle_info(Info, State) -> {noreply, State} |
@@ -275,8 +281,11 @@ handle_cast(_Msg, State) ->
 %%                                       {stop, Reason, State}
 %% Description: Handling all non call/cast messages
 %%--------------------------------------------------------------------
+handle_info(timeout, State) ->
+    proc_lib:hibernate(gen_server, enter_loop, [?MODULE, [], State]),
+    {noreply, State, ?HIBERNATE_TIMEOUT};
 handle_info(_Info, State) ->
-    {noreply, State}.
+    {noreply, State, ?HIBERNATE_TIMEOUT}.
 
 %%--------------------------------------------------------------------
 %% Function: terminate(Reason, State) -> void()
index a2437a9e198254a42c618d8232a7b58032ee6c5e..95d6ca30052079801d9ca0df56394a7f36bc08e1 100644 (file)
@@ -54,6 +54,8 @@
                xml_stream_state,
                timeout}).
 
+-define(HIBERNATE_TIMEOUT, 90000).
+
 %%====================================================================
 %% API
 %%====================================================================
@@ -146,7 +148,7 @@ handle_call({starttls, TLSSocket}, _From,
                           xml_stream_state = NewXMLStreamState},
     case tls:recv_data(TLSSocket, "") of
        {ok, TLSData} ->
-           {reply, ok, process_data(TLSData, NewState)};
+           {reply, ok, process_data(TLSData, NewState), ?HIBERNATE_TIMEOUT};
        {error, _Reason} ->
            {stop, normal, ok, NewState}
     end;
@@ -161,7 +163,7 @@ handle_call({compress, ZlibSocket}, _From,
                           xml_stream_state = NewXMLStreamState},
     case ejabberd_zlib:recv_data(ZlibSocket, "") of
        {ok, ZlibData} ->
-           {reply, ok, process_data(ZlibData, NewState)};
+           {reply, ok, process_data(ZlibData, NewState), ?HIBERNATE_TIMEOUT};
        {error, _Reason} ->
            {stop, normal, ok, NewState}
     end;
@@ -172,17 +174,18 @@ handle_call(reset_stream, _From,
     xml_stream:close(XMLStreamState),
     NewXMLStreamState = xml_stream:new(C2SPid, MaxStanzaSize),
     Reply = ok,
-    {reply, Reply, State#state{xml_stream_state = NewXMLStreamState}};
+    {reply, Reply, State#state{xml_stream_state = NewXMLStreamState},
+     ?HIBERNATE_TIMEOUT};
 handle_call({become_controller, C2SPid}, _From, State) ->
     XMLStreamState = xml_stream:new(C2SPid, State#state.max_stanza_size),
     NewState = State#state{c2s_pid = C2SPid,
                           xml_stream_state = XMLStreamState},
     activate_socket(NewState),
     Reply = ok,
-    {reply, Reply, NewState};
+    {reply, Reply, NewState, ?HIBERNATE_TIMEOUT};
 handle_call(_Request, _From, State) ->
     Reply = ok,
-    {reply, Reply, State}.
+    {reply, Reply, State, ?HIBERNATE_TIMEOUT}.
 
 %%--------------------------------------------------------------------
 %% Function: handle_cast(Msg, State) -> {noreply, State} |
@@ -192,11 +195,11 @@ handle_call(_Request, _From, State) ->
 %%--------------------------------------------------------------------
 handle_cast({change_shaper, Shaper}, State) ->
     NewShaperState = shaper:new(Shaper),
-    {noreply, State#state{shaper_state = NewShaperState}};
+    {noreply, State#state{shaper_state = NewShaperState}, ?HIBERNATE_TIMEOUT};
 handle_cast(close, State) ->
     {stop, normal, State};
 handle_cast(_Msg, State) ->
-    {noreply, State}.
+    {noreply, State, ?HIBERNATE_TIMEOUT}.
 
 %%--------------------------------------------------------------------
 %% Function: handle_info(Info, State) -> {noreply, State} |
@@ -212,19 +215,21 @@ handle_info({Tag, _TCPSocket, Data},
        tls ->
            case tls:recv_data(Socket, Data) of
                {ok, TLSData} ->
-                   {noreply, process_data(TLSData, State)};
+                   {noreply, process_data(TLSData, State),
+                    ?HIBERNATE_TIMEOUT};
                {error, _Reason} ->
                    {stop, normal, State}
            end;
        ejabberd_zlib ->
            case ejabberd_zlib:recv_data(Socket, Data) of
                {ok, ZlibData} ->
-                   {noreply, process_data(ZlibData, State)};
+                   {noreply, process_data(ZlibData, State),
+                    ?HIBERNATE_TIMEOUT};
                {error, _Reason} ->
                    {stop, normal, State}
            end;
        _ ->
-           {noreply, process_data(Data, State)}
+           {noreply, process_data(Data, State), ?HIBERNATE_TIMEOUT}
     end;
 handle_info({Tag, _TCPSocket}, State)
   when (Tag == tcp_closed) or (Tag == ssl_closed) ->
@@ -233,15 +238,18 @@ handle_info({Tag, _TCPSocket, Reason}, State)
   when (Tag == tcp_error) or (Tag == ssl_error) ->
     case Reason of
        timeout ->
-           {noreply, State};
+           {noreply, State, ?HIBERNATE_TIMEOUT};
        _ ->
            {stop, normal, State}
     end;
 handle_info({timeout, _Ref, activate}, State) ->
     activate_socket(State),
-    {noreply, State};
+    {noreply, State, ?HIBERNATE_TIMEOUT};
+handle_info(timeout, State) ->
+    proc_lib:hibernate(gen_server, enter_loop, [?MODULE, [], State]),
+    {noreply, State, ?HIBERNATE_TIMEOUT};
 handle_info(_Info, State) ->
-    {noreply, State}.
+    {noreply, State, ?HIBERNATE_TIMEOUT}.
 
 %%--------------------------------------------------------------------
 %% Function: terminate(Reason, State) -> void()