diff mod_pubsub_serverinfo/mod_pubsub_serverinfo.lua @ 5812: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
parents 76b57bcfe1b2
children d38772479891
line wrap: on
line diff
--- a/mod_pubsub_serverinfo/mod_pubsub_serverinfo.lua	Thu Jan 04 16:21:05 2024 +0100
+++ b/mod_pubsub_serverinfo/mod_pubsub_serverinfo.lua	Mon Jan 08 15:38:18 2024 +0000
@@ -227,14 +227,15 @@
 
 	-- try to read answer from cache.
 	local cached_value = opt_in_cache[remoteDomain]
-	if cached_value ~= nil and os.difftime(cached_value.expires, os.time()) > 0 then
+	local ttl = cached_value and os.difftime(cached_value.expires, os.time());
+	if cached_value and ttl > (publication_interval + 60) then
 		module:log("debug", "Opt-in status (from cache) for '%s': %s", remoteDomain, cached_value.opt_in)
 		return cached_value.opt_in;
 	end
 
+	-- We don't have a cached value, or it is nearing expiration - refresh it now
 	-- TODO worry about not having multiple requests in flight to the same domain.cached_value
 
-	-- Cache could not provide an answer. Perform service discovery.
 	module:log("debug", "No cached opt-in status for '%s': performing disco/info to determine opt-in.", remoteDomain)
 	local discoRequest = st.iq({ type = "get", to = remoteDomain, from = actor, id = new_id() })
 		:tag("query", { xmlns = "http://jabber.org/protocol/disco#info" })
@@ -272,7 +273,11 @@
 		end
 	);
 
-	-- return 'false' for now. Better luck next time...
-	return false;
+	if ttl and ttl <= 0 then
+		-- Cache entry expired, remove it and assume not opted in
+		opt_in_cache[remoteDomain] = nil;
+		return false;
+	end
 
+	return cached_value and cached_value.opt_in;
 end