From 93410136693735381d2e1051225e07fed5547d66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Serkan=20=C3=96ZAL?= Date: Sat, 7 May 2022 14:25:06 +0300 Subject: [PATCH] Handle "start-delay" support at coldstart (#9) --- src/controller.js | 18 +++++++++++------- src/handler_cjs.js | 14 +++----------- src/handler_es.mjs | 10 ++-------- 3 files changed, 16 insertions(+), 26 deletions(-) diff --git a/src/controller.js b/src/controller.js index 4a325de..0f33a6d 100644 --- a/src/controller.js +++ b/src/controller.js @@ -56,7 +56,7 @@ const _setupStartDelayHandler = (opts) => { const _startDelay = startDelay || (opts && opts.startDelay) if (startDelay) { startDelayHandler = setTimeout(async () => { - await _startProfiler(opts) + await _doStartProfiler(opts) }, _startDelay) startDelayHandler.unref() return true @@ -71,7 +71,7 @@ const _destroyStartDelayHandler = () => { } } -const _startProfiler = async (opts) => { +const _doStartProfiler = async (opts) => { const _samplingInterval = samplingInterval || (opts && opts.samplingInterval) || @@ -85,6 +85,14 @@ const _startProfiler = async (opts) => { } } +const _startProfiler = async (opts) => { + const _startDelayed = _setupStartDelayHandler(opts) + if (_startDelayed) { + return + } + await _doStartProfiler(opts) +} + const _beforeInvocation = async (opts, event, context) => { _destroyTimeoutHandler() _setupTimeoutHandler(opts, event, context) @@ -94,11 +102,6 @@ const _beforeInvocation = async (opts, event, context) => { return } - const _startDelayed = _setupStartDelayHandler(opts, event, context) - if (_startDelayed) { - return - } - await _startProfiler(opts) } @@ -144,4 +147,5 @@ const _afterInvocation = async ( module.exports = { beforeInvocation: _beforeInvocation, afterInvocation: _afterInvocation, + startProfiler: _startProfiler, } diff --git a/src/handler_cjs.js b/src/handler_cjs.js index 3c00f26..6823557 100644 --- a/src/handler_cjs.js +++ b/src/handler_cjs.js @@ -1,22 +1,14 @@ const loader = require('./loader.js') -const { startProfiler } = require('./profiler.js') -const { - MIDDY_PROFILER_SAMPLING_INTERVAL_ENV_VAR_NAME, - MIDDY_PROFILER_HANDLER_ENV_VAR_NAME, - MIDDY_PROFILER_SAMPLING_INTERVAL_DEFAULT_VALUE, -} = require('./constants.js') +const { startProfiler } = require('./controller.js') +const { MIDDY_PROFILER_HANDLER_ENV_VAR_NAME } = require('./constants.js') const { beforeInvocation, afterInvocation } = require('./hooks.js') const logger = require('./logger.js') -const samplingInterval = - parseInt(process.env[MIDDY_PROFILER_SAMPLING_INTERVAL_ENV_VAR_NAME]) || - MIDDY_PROFILER_SAMPLING_INTERVAL_DEFAULT_VALUE - let profilerPromise = null let userHandler = null try { - profilerPromise = startProfiler(samplingInterval) + profilerPromise = startProfiler() } catch (e) { logger.error('Unable to start profiler:', e) } diff --git a/src/handler_es.mjs b/src/handler_es.mjs index d1d9b60..0ea71d7 100644 --- a/src/handler_es.mjs +++ b/src/handler_es.mjs @@ -1,20 +1,14 @@ import loader from './loader.js' -import { startProfiler } from './profiler.js' +import { startProfiler } from './controller.js' import { - MIDDY_PROFILER_SAMPLING_INTERVAL_ENV_VAR_NAME, MIDDY_PROFILER_HANDLER_ENV_VAR_NAME, - MIDDY_PROFILER_SAMPLING_INTERVAL_DEFAULT_VALUE, } from './constants.js' import { beforeInvocation, afterInvocation } from './hooks.js' import logger from './logger.js' -const samplingInterval = - parseInt(process.env[MIDDY_PROFILER_SAMPLING_INTERVAL_ENV_VAR_NAME]) || - MIDDY_PROFILER_SAMPLING_INTERVAL_DEFAULT_VALUE - try { // Start profiler and wait for it - await startProfiler(samplingInterval) + await startProfiler() } catch (e) { logger.error('Unable to start profiler:', e) }