From c2d81c59b60cd8f0f6abbc5a6560c273b9b5f9dd Mon Sep 17 00:00:00 2001 From: Alexey Shchepin Date: Sun, 23 Nov 2008 09:43:52 +0000 Subject: [PATCH] * src/ejabberd_receiver.erl: Hibernate after timeout * src/ejabberd_frontend_socket.erl: Likewise SVN Revision: 1680 --- ChangeLog | 5 ++++ src/ejabberd_frontend_socket.erl | 39 ++++++++++++++++++++------------ src/ejabberd_receiver.erl | 34 +++++++++++++++++----------- 3 files changed, 50 insertions(+), 28 deletions(-) diff --git a/ChangeLog b/ChangeLog index c1d633da9..a87b971d0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2008-11-23 Alexey Shchepin + + * src/ejabberd_receiver.erl: Hibernate after timeout + * src/ejabberd_frontend_socket.erl: Likewise + 2008-11-12 Badlop * src/web/ejabberd_http.erl: Include recognized headers in diff --git a/src/ejabberd_frontend_socket.erl b/src/ejabberd_frontend_socket.erl index e2d244800..df604ccc3 100644 --- a/src/ejabberd_frontend_socket.erl +++ b/src/ejabberd_frontend_socket.erl @@ -53,6 +53,8 @@ -record(state, {sockmod, socket, receiver}). +-define(HIBERNATE_TIMEOUT, 90000). + %%==================================================================== %% API %%==================================================================== @@ -175,7 +177,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), @@ -183,7 +186,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( @@ -191,7 +195,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( @@ -201,35 +206,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), @@ -245,7 +251,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, @@ -256,11 +262,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} | @@ -269,7 +275,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} | @@ -277,8 +283,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() diff --git a/src/ejabberd_receiver.erl b/src/ejabberd_receiver.erl index 17b884481..9ab43f5a0 100644 --- a/src/ejabberd_receiver.erl +++ b/src/ejabberd_receiver.erl @@ -54,6 +54,8 @@ xml_stream_state, timeout}). +-define(HIBERNATE_TIMEOUT, 90000). + %%==================================================================== %% API %%==================================================================== @@ -141,7 +143,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; @@ -156,7 +158,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; @@ -167,17 +169,18 @@ handle_call(reset_stream, _From, close_stream(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} | @@ -187,11 +190,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} | @@ -207,19 +210,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) -> @@ -228,15 +233,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()