25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-12-16 17:15:55 +01:00

Remove useless loop and clean prepare_response (thanks to Christopher tofu Zorn)

1. Remove the silly loop that isnt needed,
   this speeds up all requests by 100 milliseconds.

2. 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:
Badlop 2010-05-28 00:40:07 +02:00
parent 3b963c4f18
commit bd69e3ad6c

View File

@ -783,7 +783,7 @@ handle_http_put(Sid, Rid, Attrs, Payload, PayloadSize, StreamStart, IP) ->
{buffered, _Sess} -> {buffered, _Sess} ->
{200, ?HEADER, "<body xmlns='"++?NS_HTTP_BIND_s++"'/>"}; {200, ?HEADER, "<body xmlns='"++?NS_HTTP_BIND_s++"'/>"};
{ok, Sess} -> {ok, Sess} ->
prepare_response(Sess, Rid, Attrs, StreamStart) prepare_response(Sess, Rid, [], StreamStart)
end. end.
http_put(Sid, Rid, Attrs, Payload, PayloadSize, StreamStart, IP) -> http_put(Sid, Rid, Attrs, Payload, PayloadSize, StreamStart, IP) ->
@ -912,9 +912,7 @@ update_shaper(ShaperState, PayloadSize) ->
{NewShaperState, undefined} {NewShaperState, undefined}
end. end.
prepare_response(#http_bind{id=Sid, wait=Wait, hold=Hold, to=To}=Sess, prepare_response(Sess, Rid, OutputEls, StreamStart) ->
Rid, _, StreamStart) ->
receive after 100 -> ok end, %% TODO: Why is this needed. Argh. Bad programming practice.
case catch http_get(Sess, Rid) of case catch http_get(Sess, Rid) of
{ok, cancel} -> {ok, cancel} ->
%% actually it would be better if we could completely %% actually it would be better if we could completely
@ -927,100 +925,118 @@ prepare_response(#http_bind{id=Sid, wait=Wait, hold=Hold, to=To}=Sess,
{200, ?HEADER, "<body type='terminate' xmlns='"++?NS_HTTP_BIND_s++"'/>"}; {200, ?HEADER, "<body type='terminate' xmlns='"++?NS_HTTP_BIND_s++"'/>"};
{ok, ROutPacket} -> {ok, ROutPacket} ->
OutPacket = lists:reverse(ROutPacket), OutPacket = lists:reverse(ROutPacket),
?DEBUG("OutPacket: ~p", [OutPacket]), ?DEBUG("OutPacket: ~p", [OutputEls++OutPacket]),
case StreamStart of prepare_outpacket_response(Sess, Rid, OutputEls++OutPacket, StreamStart);
false -> {'EXIT', {shutdown, _}} ->
case catch send_outpacket(Sess, OutPacket) of {200, ?HEADER, "<body type='terminate' condition='system-shutdown' xmlns='"++?NS_HTTP_BIND_s++"'/>"};
{'EXIT', _Reason} -> {'EXIT', _Reason} ->
{200, ?HEADER, {200, ?HEADER, "<body type='terminate' xmlns='"++?NS_HTTP_BIND_s++"'/>"}
"<body type='terminate' xmlns='"++ end.
?NS_HTTP_BIND_s++"'/>"};
SendRes -> %% Send output payloads on establised sessions
SendRes prepare_outpacket_response(Sess, _Rid, OutPacket, false) ->
end; case catch send_outpacket(Sess, OutPacket) of
true -> {'EXIT', _Reason} ->
case OutPacket of {200, ?HEADER,
[{xmlstreamstart, _, OutAttrs} | Els] -> "<body type='terminate' xmlns='"++
AuthID = exmpp_xml:get_attribute_from_list_as_list(OutAttrs, "id", ""), ?NS_HTTP_BIND_s++"'/>"};
FromB = exmpp_xml:get_attribute_from_list_as_binary(OutAttrs, "from", ""), SendRes ->
Version = exmpp_xml:get_attribute_from_list_as_list(OutAttrs, "version", ""), SendRes
OutEls = end;
case Els of %% Handle a new session along with its output payload
[] -> prepare_outpacket_response(#http_bind{id=Sid, wait=Wait,
[]; hold=Hold, to=To}=Sess,
[{xmlstreamelement, Rid, OutPacket, true) ->
#xmlel{name = 'features', case OutPacket of
declared_ns = [{undefined, ?NS_XMPP_pfx}], [{xmlstreamstart, _, OutAttrs} | Els] ->
attrs = StreamAttribs, AuthID = exmpp_xml:get_attribute_from_list_as_list(OutAttrs, "id", ""),
children = StreamEls}} FromB = exmpp_xml:get_attribute_from_list_as_binary(OutAttrs, "from", ""),
| StreamTail] -> Version = exmpp_xml:get_attribute_from_list_as_list(OutAttrs, "version", ""),
% {xmlelement, "stream:features", OutEls =
% StreamAttribs, StreamEls}} case Els of
% | StreamTail] -> [] ->
TypedTail = [];
[check_default_xmlns(OEl) || [{xmlstreamelement,
{xmlstreamelement, OEl} <- #xmlel{name = 'features',
StreamTail], declared_ns = [{undefined, ?NS_XMPP_pfx}],
[#xmlel{name = 'features', attrs = StreamAttribs,
declared_ns = [{?NS_XMPP_s, ?NS_XMPP_pfx}], children = StreamEls}}
attrs = StreamAttribs, | StreamTail] ->
children = StreamEls}] ++ %% {xmlelement, "stream:features",
% [{xmlelement, "stream:features", %% StreamAttribs, StreamEls}}
% [{"xmlns:stream", %% | StreamTail] ->
% ?NS_XMPP_s}] ++ TypedTail =
% StreamAttribs, StreamEls}] ++ [check_default_xmlns(OEl) ||
{xmlstreamelement, OEl} <-
StreamTail],
[#xmlel{name = 'features',
declared_ns = [{?NS_XMPP_s, ?NS_XMPP_pfx}],
attrs = StreamAttribs,
children = StreamEls}] ++
%% [{xmlelement, "stream:features",
%% [{"xmlns:stream",
%% ?NS_XMPP_s}] ++
%% StreamAttribs, StreamEls}] ++
TypedTail; TypedTail;
StreamTail -> StreamTail ->
[check_default_xmlns(OEl) || [check_default_xmlns(OEl) ||
{xmlstreamelement, OEl} <- {xmlstreamelement, OEl} <-
StreamTail] StreamTail]
end, end,
BOSH_attribs = case OutEls of
[#xmlattr{name = 'authid', value = list_to_binary(AuthID)}, [] ->
#xmlattr{name = 'xmlns:stream', value = ?NS_XMPP_b}] ++ prepare_response(Sess, Rid, OutPacket, true);
case OutEls of [#xmlel{name = 'stream:error'}] ->
[] -> {200, ?HEADER, "<body type='terminate' "
[]; "condition='host-unknown' "
_ -> "xmlns='"++?NS_HTTP_BIND_s++"'/>"};
[#xmlattr{name = 'version', ns = ?NS_BOSH_s, value = list_to_binary(Version)}] _ ->
end, BOSH_attribs =
MaxInactivity = get_max_inactivity(To, ?MAX_INACTIVITY), [#xmlattr{name = 'authid', value = list_to_binary(AuthID)},
MaxPause = get_max_pause(To), #xmlattr{name = 'xmlns:stream', value = ?NS_XMPP_b}] ++
{200, ?HEADER, case OutEls of
exmpp_xml:document_to_list( [] ->
#xmlel{name = 'body', prepare_response(Sess, Rid, OutPacket, true);
ns = ?NS_HTTP_BIND_s, _ ->
declared_ns = [{?NS_XBOSH_s, ?NS_XBOSH_pfx}], [#xmlattr{name = 'version', ns = ?NS_BOSH_s, value = list_to_binary(Version)}]
attrs = [ end,
#xmlattr{name = 'sid', MaxInactivity = get_max_inactivity(To, ?MAX_INACTIVITY),
value = list_to_binary(Sid) MaxPause = get_max_pause(To),
}, {200, ?HEADER,
#xmlattr{name = 'wait', exmpp_xml:document_to_list(
value = list_to_binary(integer_to_list(Wait)) #xmlel{name = 'body',
}, ns = ?NS_HTTP_BIND_s,
#xmlattr{name = 'requests', declared_ns = [{?NS_XBOSH_s, ?NS_XBOSH_pfx}],
value = list_to_binary(integer_to_list(Hold+1)) attrs = [
}, #xmlattr{name = 'sid',
#xmlattr{name = 'inactivity', value = list_to_binary(Sid)
value = list_to_binary(integer_to_list(trunc(MaxInactivity/1000))) },
}, #xmlattr{name = 'wait',
#xmlattr{name = 'maxpause', value = list_to_binary(integer_to_list(Wait))
value = list_to_binary(integer_to_list(MaxPause)) },
}, #xmlattr{name = 'requests',
#xmlattr{name = 'polling', value = list_to_binary(integer_to_list(Hold+1))
value = list_to_binary(integer_to_list(trunc(?MIN_POLLING/1000000))) },
}, #xmlattr{name = 'inactivity',
#xmlattr{name = 'ver', value = list_to_binary(integer_to_list(trunc(MaxInactivity/1000)))
value = ?BOSH_VERSION_b },
}, #xmlattr{name = 'maxpause',
#xmlattr{name = 'from', value = list_to_binary(integer_to_list(MaxPause))
value = FromB },
}, #xmlattr{name = 'polling',
#xmlattr{name = 'secure', value = list_to_binary(integer_to_list(trunc(?MIN_POLLING/1000000)))
value = <<"true">> },
} #xmlattr{name = 'ver',
] ++ BOSH_attribs, value = ?BOSH_VERSION_b
children = OutEls})}; },
#xmlattr{name = 'from',
value = FromB
},
#xmlattr{name = 'secure',
value = <<"true">>
}
] ++ BOSH_attribs,
children = OutEls})}
% {xmlelement,"body", % {xmlelement,"body",
% [{"xmlns", % [{"xmlns",
% ?NS_HTTP_BIND_s}, % ?NS_HTTP_BIND_s},
@ -1039,18 +1055,14 @@ prepare_response(#http_bind{id=Sid, wait=Wait, hold=Hold, to=To}=Sess,
% {"from", From}, % {"from", From},
% {"secure", "true"} %% we're always being secure % {"secure", "true"} %% we're always being secure
% ] ++ BOSH_attribs,OutEls})}; % ] ++ BOSH_attribs,OutEls})};
{error, _} -> end;
{200, ?HEADER, "<body type='terminate' " _ ->
"condition='host-unknown' " {200, ?HEADER, "<body type='terminate' "
"xmlns='"++?NS_HTTP_BIND_s++"'/>"} "condition='internal-server-error' "
end "xmlns='"++?NS_HTTP_BIND_s++"'/>"}
end;
{'EXIT', {shutdown, _}} ->
{200, ?HEADER, "<body type='terminate' condition='system-shutdown' xmlns='"++?NS_HTTP_BIND_s++"'/>"};
{'EXIT', _Reason} ->
{200, ?HEADER, "<body type='terminate' xmlns='"++?NS_HTTP_BIND_s++"'/>"}
end. end.
http_get(#http_bind{pid = FsmRef, wait = Wait, hold = Hold}, Rid) -> http_get(#http_bind{pid = FsmRef, wait = Wait, hold = Hold}, Rid) ->
gen_fsm:sync_send_all_state_event( gen_fsm:sync_send_all_state_event(
FsmRef, {http_get, Rid, Wait, Hold}, 2 * ?MAX_WAIT * 1000). FsmRef, {http_get, Rid, Wait, Hold}, 2 * ?MAX_WAIT * 1000).