26
1
mirror of https://github.com/processone/ejabberd.git synced 2024-12-30 17:43:57 +01:00

remove the silly loop that isnt needed, this speeds up all requests by 100 milliseconds. Clean up prepare response function and add two new functions to handle out going payloads based of whether its a new session or not.

This commit is contained in:
Christopher tofu Zorn 2010-05-05 16:45:46 -04:00 committed by Badlop
parent 9900be3d68
commit d4d45f3a50

View File

@ -102,7 +102,7 @@
-define(MIN_POLLING, 2000000). % don't poll faster than that or we will
% shoot you (time in microsec)
-define(MAX_WAIT, 3600). % max num of secs to keep a request on hold
-define(MAX_INACTIVITY, 120000). % msecs to wait before terminating
-define(MAX_INACTIVITY, 30000). % msecs to wait before terminating
% idle sessions
-define(MAX_PAUSE, 120). % may num of sec a client is allowed to pause
% the session
@ -781,7 +781,7 @@ handle_http_put(Sid, Rid, Attrs, Payload, PayloadSize, StreamStart, IP) ->
{buffered, _Sess} ->
{200, ?HEADER, "<body xmlns='"++?NS_HTTP_BIND++"'/>"};
{ok, Sess} ->
prepare_response(Sess, Rid, Attrs, StreamStart)
prepare_response(Sess, Rid, [], StreamStart)
end.
http_put(Sid, Rid, Attrs, Payload, PayloadSize, StreamStart, IP) ->
@ -883,9 +883,7 @@ update_shaper(ShaperState, PayloadSize) ->
{NewShaperState, undefined}
end.
prepare_response(#http_bind{id=Sid, wait=Wait, hold=Hold, to=To}=Sess,
Rid, _, StreamStart) ->
receive after 100 -> ok end, %% TODO: Why is this needed. Argh. Bad programming practice.
prepare_response(Sess, Rid, OutputEls, StreamStart) ->
case catch http_get(Sess, Rid) of
{ok, cancel} ->
%% actually it would be better if we could completely
@ -898,9 +896,16 @@ prepare_response(#http_bind{id=Sid, wait=Wait, hold=Hold, to=To}=Sess,
{200, ?HEADER, "<body type='terminate' xmlns='"++?NS_HTTP_BIND++"'/>"};
{ok, ROutPacket} ->
OutPacket = lists:reverse(ROutPacket),
?DEBUG("OutPacket: ~p", [OutPacket]),
case StreamStart of
false ->
?DEBUG("OutPacket: ~p", [OutputEls++OutPacket]),
prepare_outpacket_response(Sess, Rid, OutputEls++OutPacket, StreamStart);
{'EXIT', {shutdown, _}} ->
{200, ?HEADER, "<body type='terminate' condition='system-shutdown' xmlns='"++?NS_HTTP_BIND++"'/>"};
{'EXIT', _Reason} ->
{200, ?HEADER, "<body type='terminate' xmlns='"++?NS_HTTP_BIND++"'/>"}
end.
%% Send output payloads on establised sessions
prepare_outpacket_response(Sess, _Rid, OutPacket, false) ->
case catch send_outpacket(Sess, OutPacket) of
{'EXIT', _Reason} ->
{200, ?HEADER,
@ -909,7 +914,10 @@ prepare_response(#http_bind{id=Sid, wait=Wait, hold=Hold, to=To}=Sess,
SendRes ->
SendRes
end;
true ->
%% Handle a new session along with its output payload
prepare_outpacket_response(#http_bind{id=Sid, wait=Wait,
hold=Hold, to=To}=Sess,
Rid, OutPacket, true) ->
case OutPacket of
[{xmlstreamstart, _, OutAttrs} | Els] ->
AuthID = xml:get_attr_s("id", OutAttrs),
@ -937,6 +945,15 @@ prepare_response(#http_bind{id=Sid, wait=Wait, hold=Hold, to=To}=Sess,
{xmlstreamelement, OEl} <-
StreamTail]
end,
case OutEls of
[] ->
prepare_response(Sess, Rid, OutPacket, true);
[{xmlelement,
"stream:error",_,_}] ->
{200, ?HEADER, "<body type='terminate' "
"condition='host-unknown' "
"xmlns='"++?NS_HTTP_BIND++"'/>"};
_ ->
BOSH_attribs =
[{"authid", AuthID},
{"xmlns:xmpp", ?NS_BOSH},
@ -968,19 +985,15 @@ prepare_response(#http_bind{id=Sid, wait=Wait, hold=Hold, to=To}=Sess,
{"ver", ?BOSH_VERSION},
{"from", From},
{"secure", "true"} %% we're always being secure
] ++ BOSH_attribs,OutEls})};
{error, _} ->
{200, ?HEADER, "<body type='terminate' "
"condition='host-unknown' "
"xmlns='"++?NS_HTTP_BIND++"'/>"}
end
] ++ BOSH_attribs,OutEls})}
end;
{'EXIT', {shutdown, _}} ->
{200, ?HEADER, "<body type='terminate' condition='system-shutdown' xmlns='"++?NS_HTTP_BIND++"'/>"};
{'EXIT', _Reason} ->
{200, ?HEADER, "<body type='terminate' xmlns='"++?NS_HTTP_BIND++"'/>"}
_ ->
{200, ?HEADER, "<body type='terminate' "
"condition='internal-server-error' "
"xmlns='"++?NS_HTTP_BIND++"'/>"}
end.
http_get(#http_bind{pid = FsmRef, wait = Wait, hold = Hold}, Rid) ->
gen_fsm:sync_send_all_state_event(
FsmRef, {http_get, Rid, Wait, Hold}, 2 * ?MAX_WAIT * 1000).