From a36bf4db9b45fcb5ad8686f32a37ec5a8eeb6eba Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Mon, 17 May 2010 11:42:53 +0100 Subject: mod_bosh: Remove requests from the session table using table.remove(), prevents the possibility of holes in the array. --- plugins/mod_bosh.lua | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/plugins/mod_bosh.lua b/plugins/mod_bosh.lua index 5c77ad46..150d1fd0 100644 --- a/plugins/mod_bosh.lua +++ b/plugins/mod_bosh.lua @@ -47,8 +47,11 @@ function on_destroy_request(request) local session = sessions[request.sid]; if session then local requests = session.requests; - for i,r in pairs(requests) do - if r == request then requests[i] = nil; break; end + for i,r in ipairs(requests) do + if r == request then + t_remove(requests, i); + break; + end end -- If this session now has no requests open, mark it as inactive -- cgit v1.2.3 From 91492c1a60ae52094dc4ee49b440ea5decefbad1 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Mon, 17 May 2010 11:45:11 +0100 Subject: mod_bosh: Remove some hacky code for pruning dead requests that hopefully never gets executed - if it does there's a bug anyway. --- plugins/mod_bosh.lua | 5 ----- 1 file changed, 5 deletions(-) diff --git a/plugins/mod_bosh.lua b/plugins/mod_bosh.lua index 150d1fd0..56e62680 100644 --- a/plugins/mod_bosh.lua +++ b/plugins/mod_bosh.lua @@ -157,11 +157,6 @@ function stream_callbacks.streamopened(request, attr) function session.send(s) --log("debug", "Sending BOSH data: %s", tostring(s)); local oldest_request = r[1]; - while oldest_request and oldest_request.destroyed do - t_remove(r, 1); - waiting_requests[oldest_request] = nil; - oldest_request = r[1]; - end if oldest_request then log("debug", "We have an open request, so sending on that"); response.body = t_concat{"", tostring(s), "" }; -- cgit v1.2.3 From 2eb914de98a14c237f09e69b21d63d7f636f3def Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Mon, 17 May 2010 11:46:54 +0100 Subject: mod_bosh: Fix handling of rids by not dropping requests with repeated rids (assign them their sid instead), and always starting a session with first_rid-1. --- plugins/mod_bosh.lua | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/plugins/mod_bosh.lua b/plugins/mod_bosh.lua index 56e62680..ecce93cd 100644 --- a/plugins/mod_bosh.lua +++ b/plugins/mod_bosh.lua @@ -145,7 +145,7 @@ function stream_callbacks.streamopened(request, attr) -- New session sid = new_uuid(); - local session = { type = "c2s_unauthed", conn = {}, sid = sid, rid = tonumber(attr.rid), host = attr.to, bosh_version = attr.ver, bosh_wait = attr.wait, streamid = sid, + local session = { type = "c2s_unauthed", conn = {}, sid = sid, rid = tonumber(attr.rid)-1, host = attr.to, bosh_version = attr.ver, bosh_wait = attr.wait, streamid = sid, bosh_hold = BOSH_DEFAULT_HOLD, bosh_max_inactive = BOSH_DEFAULT_INACTIVITY, requests = { }, send_buffer = {}, reset_stream = bosh_reset_stream, close = bosh_close_stream, dispatch_stanza = core_process_stanza, log = logger.init("bosh"..sid), secure = request.secure }; @@ -212,8 +212,9 @@ function stream_callbacks.streamopened(request, attr) session.log("warn", "rid too large (means a request was lost). Last rid: %d New rid: %s", session.rid, attr.rid); elseif diff <= 0 then -- Repeated, ignore - session.log("debug", "rid repeated (on request %s), ignoring: %d", request.id, session.rid); + session.log("debug", "rid repeated (on request %s), ignoring: %s (diff %d)", request.id, session.rid, diff); request.notopen = nil; + request.sid = sid; t_insert(session.requests, request); return; end -- cgit v1.2.3 From 95e01d796f994eb9ffcb902b38dd1c17ca7c4c57 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Mon, 17 May 2010 11:49:24 +0100 Subject: mod_bosh: Always give requests a destroy handler, so that the management of each session's request array and the inactive_sessions logic can happen in one place. Simplifies everything and concludes this series of BOSH fixes. --- plugins/mod_bosh.lua | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/plugins/mod_bosh.lua b/plugins/mod_bosh.lua index ecce93cd..04887c29 100644 --- a/plugins/mod_bosh.lua +++ b/plugins/mod_bosh.lua @@ -73,6 +73,8 @@ function handle_request(method, body, request) --log("debug", "Handling new request %s: %s\n----------", request.id, tostring(body)); request.notopen = true; request.log = log; + request.on_destroy = on_destroy_request; + local parser = lxp.new(init_xmlhandlers(request, stream_callbacks), "\1"); parser:parse(body); @@ -101,14 +103,21 @@ function handle_request(method, body, request) session.send(resp); end - if not request.destroyed and session.bosh_wait then - request.reply_before = os_time() + session.bosh_wait; - request.on_destroy = on_destroy_request; - waiting_requests[request] = true; + if not request.destroyed then + -- We're keeping this request open, to respond later + log("debug", "Have nothing to say, so leaving request unanswered for now"); + if session.bosh_wait then + request.reply_before = os_time() + session.bosh_wait; + waiting_requests[request] = true; + end + if inactive_sessions[session] then + -- Session was marked as inactive, since we have + -- a request open now, unmark it + inactive_sessions[session] = nil; + end end - log("debug", "Have nothing to say, so leaving request unanswered for now"); - return true; + return true; -- Inform httpserver we shall reply later end end @@ -171,7 +180,6 @@ function stream_callbacks.streamopened(request, attr) else log("debug", "Destroying the request now..."); oldest_request:destroy(); - t_remove(r, 1); end elseif s ~= "" then log("debug", "Saved to send buffer because there are %d open requests", #r); @@ -228,12 +236,6 @@ function stream_callbacks.streamopened(request, attr) return; end - -- If session was inactive, make sure it is now marked as not - if #session.requests == 0 then - (session.log or log)("debug", "BOSH client now active again at %d", os_time()); - inactive_sessions[session] = nil; - end - if session.notopen then local features = st.stanza("stream:features"); fire_event("stream-features", session, features); -- cgit v1.2.3