Comparison

mod_pubsub_serverinfo/mod_pubsub_serverinfo.lua @ 5835:cf5f77491323

mod_pubsub_serverinfo: Refresh cache entries if they will expire before next run
author Matthew Wild <mwild1@gmail.com>
date Mon, 08 Jan 2024 15:38:18 +0000
parent 5833:76b57bcfe1b2
child 5836:d38772479891
comparison
equal deleted inserted replaced
5834:ce8f0e458ffa 5835:cf5f77491323
225 225
226 function does_opt_in(remoteDomain) 226 function does_opt_in(remoteDomain)
227 227
228 -- try to read answer from cache. 228 -- try to read answer from cache.
229 local cached_value = opt_in_cache[remoteDomain] 229 local cached_value = opt_in_cache[remoteDomain]
230 if cached_value ~= nil and os.difftime(cached_value.expires, os.time()) > 0 then 230 local ttl = cached_value and os.difftime(cached_value.expires, os.time());
231 if cached_value and ttl > (publication_interval + 60) then
231 module:log("debug", "Opt-in status (from cache) for '%s': %s", remoteDomain, cached_value.opt_in) 232 module:log("debug", "Opt-in status (from cache) for '%s': %s", remoteDomain, cached_value.opt_in)
232 return cached_value.opt_in; 233 return cached_value.opt_in;
233 end 234 end
234 235
236 -- We don't have a cached value, or it is nearing expiration - refresh it now
235 -- TODO worry about not having multiple requests in flight to the same domain.cached_value 237 -- TODO worry about not having multiple requests in flight to the same domain.cached_value
236 238
237 -- Cache could not provide an answer. Perform service discovery.
238 module:log("debug", "No cached opt-in status for '%s': performing disco/info to determine opt-in.", remoteDomain) 239 module:log("debug", "No cached opt-in status for '%s': performing disco/info to determine opt-in.", remoteDomain)
239 local discoRequest = st.iq({ type = "get", to = remoteDomain, from = actor, id = new_id() }) 240 local discoRequest = st.iq({ type = "get", to = remoteDomain, from = actor, id = new_id() })
240 :tag("query", { xmlns = "http://jabber.org/protocol/disco#info" }) 241 :tag("query", { xmlns = "http://jabber.org/protocol/disco#info" })
241 242
242 module:send_iq(discoRequest):next( 243 module:send_iq(discoRequest):next(
270 expires = os.time() + cache_ttl; 271 expires = os.time() + cache_ttl;
271 } 272 }
272 end 273 end
273 ); 274 );
274 275
275 -- return 'false' for now. Better luck next time... 276 if ttl and ttl <= 0 then
276 return false; 277 -- Cache entry expired, remove it and assume not opted in
277 278 opt_in_cache[remoteDomain] = nil;
278 end 279 return false;
280 end
281
282 return cached_value and cached_value.opt_in;
283 end