feat(webhooks): add polling optimization and fallback when webhooks are active (Phase 5)
All checks were successful
All checks were successful
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user