comparison mod_pubsub_hub/mod_pubsub_hub.lua @ 766:1184fe8ebb21

mod_pubsub_hub: Try to choose time until the next periodic check in a sane way
author Kim Alvefur <zash@zash.se>
date Thu, 02 Aug 2012 00:25:24 +0200
parents d11d91ee81ed
children e5667f1da6bf
comparison
equal deleted inserted replaced
765:82d292e4f30f 766:1184fe8ebb21
142 end 142 end
143 143
144 local function periodic() 144 local function periodic()
145 local now = time(); 145 local now = time();
146 local next_check = now + max_lease; 146 local next_check = now + max_lease;
147 local purge = false 147 local purge = false;
148 for topic, callbacks in pairs(subs_by_topic) do 148 for topic, callbacks in pairs(subs_by_topic) do
149 for callback, subscription in pairs(callbacks) do 149 for callback, subscription in pairs(callbacks) do
150 if subscription.mode == "subscribed" then 150 if subscription.mode == "subscribed" then
151 if subscription.expires < now then 151 if subscription.expires < now then
152 -- Subscription has expired, drop it. 152 -- Subscription has expired, drop it.
153 purge = true 153 purge = true;
154 end 154 elseif subscription.expires < now + min_lease then
155 if subscription.expires < now + min_lease then
156 -- Subscription set to expire soon, re-confirm it. 155 -- Subscription set to expire soon, re-confirm it.
157 local challenge = uuid(); 156 local challenge = uuid();
158 local callback_url = callback .. (callback:match("%?") and "&" or "?") .. formencode{ 157 local callback_url = callback .. (callback:match("%?") and "&" or "?") .. formencode{
159 ["hub.mode"] = subscription.state, 158 ["hub.mode"] = subscription.state,
160 ["hub.topic"] = topic, 159 ["hub.topic"] = topic,
166 if body == challenge and code > 199 and code < 300 then 165 if body == challenge and code > 199 and code < 300 then
167 subscription.expires = now + subscription.lease_seconds; 166 subscription.expires = now + subscription.lease_seconds;
168 end 167 end
169 end); 168 end);
170 else 169 else
171 next_check = m_min(next_check, now - subscription.expires) 170 next_check = m_min(next_check, subscription.expires);
172 end 171 end
173 end 172 end
174 end 173 end
175 if purge then 174 if purge then
176 local new_callbacks = {}; 175 local new_callbacks = {};
178 if (subscription.state == "subscribed" and subscription.expires < now) 177 if (subscription.state == "subscribed" and subscription.expires < now)
179 and subscription.want_state ~= "remove" then 178 and subscription.want_state ~= "remove" then
180 new_callbacks[callback] = subscription; 179 new_callbacks[callback] = subscription;
181 end 180 end
182 end 181 end
183 subs_by_topic[topic] = new_callbacks 182 subs_by_topic[topic] = new_callbacks;
184 end 183 purge = false;
185 end 184 end
186 return m_max(next_check - min_lease, min_lease); 185 end
186 return now - m_max(next_check - min_lease, min_lease);
187 end 187 end
188 188
189 local function on_notify(subscription, content) 189 local function on_notify(subscription, content)
190 local body = tostring(content); 190 local body = tostring(content);
191 local headers = { 191 local headers = {