feat(webhooks): add polling optimization and fallback when webhooks are active (Phase 5)
All checks were successful
Build and Push Docker Image / build (push) Successful in 38s
Licence Check / Licence compatibility and copyright header verification (push) Successful in 56s
CI / Security audit (push) Successful in 1m18s
CI / Tests & coverage (push) Successful in 1m32s

This commit is contained in:
2026-05-19 16:10:45 +01:00
parent 80e8b72878
commit fcb0cd8e4a
2 changed files with 184 additions and 47 deletions

View File

@@ -72,4 +72,64 @@ class MemoryCache {
const cache = new MemoryCache();
// Webhook metrics for polling optimization
// These are stored separately from regular cache entries
const webhookMetrics = {
// Per-instance metrics: key = instance URL, value = { lastWebhookTimestamp, eventsReceived, pollsSkipped }
instances: new Map(),
// Global metrics
lastGlobalWebhookTimestamp: null,
totalWebhookEventsReceived: 0
};
function getWebhookMetrics(instanceUrl) {
if (!instanceUrl) return null;
return webhookMetrics.instances.get(instanceUrl) || {
lastWebhookTimestamp: null,
eventsReceived: 0,
pollsSkipped: 0
};
}
function updateWebhookMetrics(instanceUrl) {
const now = Date.now();
webhookMetrics.lastGlobalWebhookTimestamp = now;
webhookMetrics.totalWebhookEventsReceived++;
if (instanceUrl) {
const metrics = webhookMetrics.instances.get(instanceUrl) || {
lastWebhookTimestamp: null,
eventsReceived: 0,
pollsSkipped: 0
};
metrics.lastWebhookTimestamp = now;
metrics.eventsReceived++;
webhookMetrics.instances.set(instanceUrl, metrics);
}
}
function incrementPollsSkipped(instanceUrl) {
if (instanceUrl) {
const metrics = webhookMetrics.instances.get(instanceUrl) || {
lastWebhookTimestamp: null,
eventsReceived: 0,
pollsSkipped: 0
};
metrics.pollsSkipped++;
webhookMetrics.instances.set(instanceUrl, metrics);
}
}
function getGlobalWebhookMetrics() {
return {
lastGlobalWebhookTimestamp: webhookMetrics.lastGlobalWebhookTimestamp,
totalWebhookEventsReceived: webhookMetrics.totalWebhookEventsReceived,
instances: Object.fromEntries(webhookMetrics.instances)
};
}
module.exports = cache;
module.exports.getWebhookMetrics = getWebhookMetrics;
module.exports.updateWebhookMetrics = updateWebhookMetrics;
module.exports.incrementPollsSkipped = incrementPollsSkipped;
module.exports.getGlobalWebhookMetrics = getGlobalWebhookMetrics;