From 512feb0268256b5ddcb9b8ad47b7d297827cd103 Mon Sep 17 00:00:00 2001 From: Martin Dimitrov Date: Tue, 2 Sep 2025 20:10:49 -0700 Subject: [PATCH] increase global timeout to 9s to prevent timeouts and graceful fallback sooner at 7.25s --- .../doorman-api/src/common/DoormanHandler.ts | 12 +++---- .../src/functions/buzzer-activated.ts | 36 +++++++++---------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/packages/doorman-api/src/common/DoormanHandler.ts b/packages/doorman-api/src/common/DoormanHandler.ts index 4fc23ba..7190b29 100644 --- a/packages/doorman-api/src/common/DoormanHandler.ts +++ b/packages/doorman-api/src/common/DoormanHandler.ts @@ -51,7 +51,7 @@ export function gracefullyEndLogger(logger: Logger): Promise { logger.on('finish', () => { fulfill(); }); - + logger.end(); }); } @@ -64,7 +64,7 @@ const FALLBACK_CALLBACK: CallbackResult = [null, REJECT_RESPONSE]; const MINIMUM_MS_TO_SEND_RESPONSE: number = 250; const FUNCTION_MAXIMUM_DURATION_MS: number = 10_000; -const INNER_HANDLER_MAXIMUM_DURATION_MS: number = 8_500; +const INNER_HANDLER_MAXIMUM_DURATION_MS: number = 9_000; /** * A decorator for twilio handlers. It provides a metrics registry and @@ -79,7 +79,7 @@ export function withMetrics console.log("[CommonHandler] created loki logger"); console.log("[CommonHandler] clearing old metrics"); register.clear(); - + console.log("[CommonHandler] creating metrics registry"); const metricsRegistry = new Registry(); const requestOptions: RequestOptions = { @@ -103,7 +103,7 @@ export function withMetrics metricsRegistry.registerMetric(new Counter({ name: CommonMetrics.HTTP_CLIENT_ERROR, help: "Client side HTTP error", - labelNames: [ "ErrorCode" ], + labelNames: ["ErrorCode"], })); metricsRegistry.registerMetric(new Counter({ @@ -231,7 +231,7 @@ export function withMetrics // wait for response for up to x ms, otherwise we call the failFastCallbackMethod try { await pTimeout( - () => handler(context, event, tempCallback, metricsRegistry, logger, failFastCallbackProvider), + () => handler(context, event, tempCallback, metricsRegistry, logger, failFastCallbackProvider), INNER_HANDLER_MAXIMUM_DURATION_MS )(); } catch (e) { @@ -254,7 +254,7 @@ export function withMetrics setResponseJson(response, 400, { msg: fromError(e).toString(), }); - + callbackResult = [null, response]; } } diff --git a/packages/doorman-client/src/functions/buzzer-activated.ts b/packages/doorman-client/src/functions/buzzer-activated.ts index 70fe6a9..19f255c 100644 --- a/packages/doorman-client/src/functions/buzzer-activated.ts +++ b/packages/doorman-client/src/functions/buzzer-activated.ts @@ -19,10 +19,10 @@ import { getMetricFromRegistry, withMetrics } from '../../../doorman-api/src/com import { Counter, Summary } from 'prom-client'; import { BuzzerActivatedMetrics, registerMetrics } from '../metrics/BuzzerActivatedMetrics'; -export const handler: ServerlessFunctionSignature = withMetrics('buzzer-activated', async function(context, event, callback, metricsRegistry, logger, failFastCallback) { +export const handler: ServerlessFunctionSignature = withMetrics('buzzer-activated', async function (context, event, callback, metricsRegistry, logger, failFastCallback) { // metrics registerMetrics(metricsRegistry); - + let invokeId = `[${randomUUID()}]`; let configString = event.config; let config: InfoResponseClient | undefined; @@ -34,7 +34,7 @@ export const handler: ServerlessFunctionSignature + let msgs = config.discordUsers.map((u) => msg + u + ')' ); @@ -69,7 +69,7 @@ export const handler: ServerlessFunctionSignature(metricsRegistry, BuzzerActivatedMetrics.POLL_LATENCY) .startTimer({ door: config.door }); - + fetch(context.DOORMAN_URL + `/api/door/status?door=${config.door}`) .then(res => res.json()) .then(async (rawBody) => { @@ -87,7 +87,7 @@ export const handler: ServerlessFunctionSignature(metricsRegistry, BuzzerActivatedMetrics.API_UNLOCK) .inc({ door: config.door }, 1); await notifyAllDiscord(context, config, `🔓 Doorman buzzed someone up @ Door "${config.door}"`, metricsRegistry, JSON.stringify(body.fingerprint)); - + resolve(twiml); } else { console.log( @@ -112,8 +112,8 @@ export const handler: ServerlessFunctionSignature(metricsRegistry, BuzzerActivatedMetrics.RESULT_NOTIFICATION_FATE_UNKNOWN) - .inc({ door: config.door }, 1); - - getMetricFromRegistry(metricsRegistry, BuzzerActivatedMetrics.DIAL_THROUGH) - .inc({ door: config.door }, 1); + .inc({ door: config.door }, 1); - const twiml = dialFallbackTwiml(config); - console.log( - invokeId + " UngracefulFallback: Cutting it too close to timeout! Skipping notifying users and responding fallback" - ); - return [null, twiml]; + getMetricFromRegistry(metricsRegistry, BuzzerActivatedMetrics.DIAL_THROUGH) + .inc({ door: config.door }, 1); + + const twiml = dialFallbackTwiml(config); + console.log( + invokeId + " UngracefulFallback: Cutting it too close to timeout! Skipping notifying users and responding fallback" + ); + return [null, twiml]; }); const twiml = await Promise.race([unlockPromise, gracefulFallbackPromise]);