25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-24 16:23:40 +01:00

* src/ejabberd_receiver.erl: Hibernate after timeout

* src/ejabberd_frontend_socket.erl: Likewise

SVN Revision: 1680
This commit is contained in:
Alexey Shchepin 2008-11-23 09:43:52 +00:00
parent 91b328b7d5
commit c2d81c59b6
3 changed files with 50 additions and 28 deletions

View File

@ -1,3 +1,8 @@
2008-11-23 Alexey Shchepin <alexey@process-one.net>
* src/ejabberd_receiver.erl: Hibernate after timeout
* src/ejabberd_frontend_socket.erl: Likewise
2008-11-12 Badlop <badlop@process-one.net> 2008-11-12 Badlop <badlop@process-one.net>
* src/web/ejabberd_http.erl: Include recognized headers in * src/web/ejabberd_http.erl: Include recognized headers in

View File

@ -53,6 +53,8 @@
-record(state, {sockmod, socket, receiver}). -record(state, {sockmod, socket, receiver}).
-define(HIBERNATE_TIMEOUT, 90000).
%%==================================================================== %%====================================================================
%% API %% API
%%==================================================================== %%====================================================================
@ -175,7 +177,8 @@ handle_call({starttls, TLSOpts}, _From, State) ->
{ok, TLSSocket} = tls:tcp_to_tls(State#state.socket, TLSOpts), {ok, TLSSocket} = tls:tcp_to_tls(State#state.socket, TLSOpts),
ejabberd_receiver:starttls(State#state.receiver, TLSSocket), ejabberd_receiver:starttls(State#state.receiver, TLSSocket),
Reply = ok, 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) -> handle_call({starttls, TLSOpts, Data}, _From, State) ->
{ok, TLSSocket} = tls:tcp_to_tls(State#state.socket, TLSOpts), {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( catch (State#state.sockmod):send(
State#state.socket, Data), State#state.socket, Data),
Reply = ok, 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) -> handle_call(compress, _From, State) ->
{ok, ZlibSocket} = ejabberd_zlib:enable_zlib( {ok, ZlibSocket} = ejabberd_zlib:enable_zlib(
@ -191,7 +195,8 @@ handle_call(compress, _From, State) ->
State#state.socket), State#state.socket),
ejabberd_receiver:compress(State#state.receiver, ZlibSocket), ejabberd_receiver:compress(State#state.receiver, ZlibSocket),
Reply = ok, 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) -> handle_call({compress, Data}, _From, State) ->
{ok, ZlibSocket} = ejabberd_zlib:enable_zlib( {ok, ZlibSocket} = ejabberd_zlib:enable_zlib(
@ -201,35 +206,36 @@ handle_call({compress, Data}, _From, State) ->
catch (State#state.sockmod):send( catch (State#state.sockmod):send(
State#state.socket, Data), State#state.socket, Data),
Reply = ok, 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) -> handle_call(reset_stream, _From, State) ->
ejabberd_receiver:reset_stream(State#state.receiver), ejabberd_receiver:reset_stream(State#state.receiver),
Reply = ok, Reply = ok,
{reply, Reply, State}; {reply, Reply, State, ?HIBERNATE_TIMEOUT};
handle_call({send, Data}, _From, State) -> handle_call({send, Data}, _From, State) ->
catch (State#state.sockmod):send( catch (State#state.sockmod):send(
State#state.socket, Data), State#state.socket, Data),
Reply = ok, Reply = ok,
{reply, Reply, State}; {reply, Reply, State, ?HIBERNATE_TIMEOUT};
handle_call({change_shaper, Shaper}, _From, State) -> handle_call({change_shaper, Shaper}, _From, State) ->
ejabberd_receiver:change_shaper(State#state.receiver, Shaper), ejabberd_receiver:change_shaper(State#state.receiver, Shaper),
Reply = ok, Reply = ok,
{reply, Reply, State}; {reply, Reply, State, ?HIBERNATE_TIMEOUT};
handle_call(get_sockmod, _From, State) -> handle_call(get_sockmod, _From, State) ->
Reply = State#state.sockmod, Reply = State#state.sockmod,
{reply, Reply, State}; {reply, Reply, State, ?HIBERNATE_TIMEOUT};
handle_call(get_peer_certificate, _From, State) -> handle_call(get_peer_certificate, _From, State) ->
Reply = tls:get_peer_certificate(State#state.socket), Reply = tls:get_peer_certificate(State#state.socket),
{reply, Reply, State}; {reply, Reply, State, ?HIBERNATE_TIMEOUT};
handle_call(get_verify_result, _From, State) -> handle_call(get_verify_result, _From, State) ->
Reply = tls:get_verify_result(State#state.socket), Reply = tls:get_verify_result(State#state.socket),
{reply, Reply, State}; {reply, Reply, State, ?HIBERNATE_TIMEOUT};
handle_call(close, _From, State) -> handle_call(close, _From, State) ->
ejabberd_receiver:close(State#state.receiver), ejabberd_receiver:close(State#state.receiver),
@ -245,7 +251,7 @@ handle_call(sockname, _From, State) ->
_ -> _ ->
SockMod:sockname(Socket) SockMod:sockname(Socket)
end, end,
{reply, Reply, State}; {reply, Reply, State, ?HIBERNATE_TIMEOUT};
handle_call(peername, _From, State) -> handle_call(peername, _From, State) ->
#state{sockmod = SockMod, socket = Socket} = State, #state{sockmod = SockMod, socket = Socket} = State,
@ -256,11 +262,11 @@ handle_call(peername, _From, State) ->
_ -> _ ->
SockMod:peername(Socket) SockMod:peername(Socket)
end, end,
{reply, Reply, State}; {reply, Reply, State, ?HIBERNATE_TIMEOUT};
handle_call(_Request, _From, State) -> handle_call(_Request, _From, State) ->
Reply = ok, Reply = ok,
{reply, Reply, State}. {reply, Reply, State, ?HIBERNATE_TIMEOUT}.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Function: handle_cast(Msg, State) -> {noreply, State} | %% Function: handle_cast(Msg, State) -> {noreply, State} |
@ -269,7 +275,7 @@ handle_call(_Request, _From, State) ->
%% Description: Handling cast messages %% Description: Handling cast messages
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
handle_cast(_Msg, State) -> handle_cast(_Msg, State) ->
{noreply, State}. {noreply, State, ?HIBERNATE_TIMEOUT}.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Function: handle_info(Info, State) -> {noreply, State} | %% Function: handle_info(Info, State) -> {noreply, State} |
@ -277,8 +283,11 @@ handle_cast(_Msg, State) ->
%% {stop, Reason, State} %% {stop, Reason, State}
%% Description: Handling all non call/cast messages %% 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) -> handle_info(_Info, State) ->
{noreply, State}. {noreply, State, ?HIBERNATE_TIMEOUT}.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Function: terminate(Reason, State) -> void() %% Function: terminate(Reason, State) -> void()

View File

@ -54,6 +54,8 @@
xml_stream_state, xml_stream_state,
timeout}). timeout}).
-define(HIBERNATE_TIMEOUT, 90000).
%%==================================================================== %%====================================================================
%% API %% API
%%==================================================================== %%====================================================================
@ -141,7 +143,7 @@ handle_call({starttls, TLSSocket}, _From,
xml_stream_state = NewXMLStreamState}, xml_stream_state = NewXMLStreamState},
case tls:recv_data(TLSSocket, "") of case tls:recv_data(TLSSocket, "") of
{ok, TLSData} -> {ok, TLSData} ->
{reply, ok, process_data(TLSData, NewState)}; {reply, ok, process_data(TLSData, NewState), ?HIBERNATE_TIMEOUT};
{error, _Reason} -> {error, _Reason} ->
{stop, normal, ok, NewState} {stop, normal, ok, NewState}
end; end;
@ -156,7 +158,7 @@ handle_call({compress, ZlibSocket}, _From,
xml_stream_state = NewXMLStreamState}, xml_stream_state = NewXMLStreamState},
case ejabberd_zlib:recv_data(ZlibSocket, "") of case ejabberd_zlib:recv_data(ZlibSocket, "") of
{ok, ZlibData} -> {ok, ZlibData} ->
{reply, ok, process_data(ZlibData, NewState)}; {reply, ok, process_data(ZlibData, NewState), ?HIBERNATE_TIMEOUT};
{error, _Reason} -> {error, _Reason} ->
{stop, normal, ok, NewState} {stop, normal, ok, NewState}
end; end;
@ -167,17 +169,18 @@ handle_call(reset_stream, _From,
close_stream(XMLStreamState), close_stream(XMLStreamState),
NewXMLStreamState = xml_stream:new(C2SPid, MaxStanzaSize), NewXMLStreamState = xml_stream:new(C2SPid, MaxStanzaSize),
Reply = ok, 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) -> handle_call({become_controller, C2SPid}, _From, State) ->
XMLStreamState = xml_stream:new(C2SPid, State#state.max_stanza_size), XMLStreamState = xml_stream:new(C2SPid, State#state.max_stanza_size),
NewState = State#state{c2s_pid = C2SPid, NewState = State#state{c2s_pid = C2SPid,
xml_stream_state = XMLStreamState}, xml_stream_state = XMLStreamState},
activate_socket(NewState), activate_socket(NewState),
Reply = ok, Reply = ok,
{reply, Reply, NewState}; {reply, Reply, NewState, ?HIBERNATE_TIMEOUT};
handle_call(_Request, _From, State) -> handle_call(_Request, _From, State) ->
Reply = ok, Reply = ok,
{reply, Reply, State}. {reply, Reply, State, ?HIBERNATE_TIMEOUT}.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Function: handle_cast(Msg, State) -> {noreply, State} | %% Function: handle_cast(Msg, State) -> {noreply, State} |
@ -187,11 +190,11 @@ handle_call(_Request, _From, State) ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
handle_cast({change_shaper, Shaper}, State) -> handle_cast({change_shaper, Shaper}, State) ->
NewShaperState = shaper:new(Shaper), NewShaperState = shaper:new(Shaper),
{noreply, State#state{shaper_state = NewShaperState}}; {noreply, State#state{shaper_state = NewShaperState}, ?HIBERNATE_TIMEOUT};
handle_cast(close, State) -> handle_cast(close, State) ->
{stop, normal, State}; {stop, normal, State};
handle_cast(_Msg, State) -> handle_cast(_Msg, State) ->
{noreply, State}. {noreply, State, ?HIBERNATE_TIMEOUT}.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Function: handle_info(Info, State) -> {noreply, State} | %% Function: handle_info(Info, State) -> {noreply, State} |
@ -207,19 +210,21 @@ handle_info({Tag, _TCPSocket, Data},
tls -> tls ->
case tls:recv_data(Socket, Data) of case tls:recv_data(Socket, Data) of
{ok, TLSData} -> {ok, TLSData} ->
{noreply, process_data(TLSData, State)}; {noreply, process_data(TLSData, State),
?HIBERNATE_TIMEOUT};
{error, _Reason} -> {error, _Reason} ->
{stop, normal, State} {stop, normal, State}
end; end;
ejabberd_zlib -> ejabberd_zlib ->
case ejabberd_zlib:recv_data(Socket, Data) of case ejabberd_zlib:recv_data(Socket, Data) of
{ok, ZlibData} -> {ok, ZlibData} ->
{noreply, process_data(ZlibData, State)}; {noreply, process_data(ZlibData, State),
?HIBERNATE_TIMEOUT};
{error, _Reason} -> {error, _Reason} ->
{stop, normal, State} {stop, normal, State}
end; end;
_ -> _ ->
{noreply, process_data(Data, State)} {noreply, process_data(Data, State), ?HIBERNATE_TIMEOUT}
end; end;
handle_info({Tag, _TCPSocket}, State) handle_info({Tag, _TCPSocket}, State)
when (Tag == tcp_closed) or (Tag == ssl_closed) -> 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) -> when (Tag == tcp_error) or (Tag == ssl_error) ->
case Reason of case Reason of
timeout -> timeout ->
{noreply, State}; {noreply, State, ?HIBERNATE_TIMEOUT};
_ -> _ ->
{stop, normal, State} {stop, normal, State}
end; end;
handle_info({timeout, _Ref, activate}, State) -> handle_info({timeout, _Ref, activate}, State) ->
activate_socket(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) -> handle_info(_Info, State) ->
{noreply, State}. {noreply, State, ?HIBERNATE_TIMEOUT}.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Function: terminate(Reason, State) -> void() %% Function: terminate(Reason, State) -> void()