Skip to content

Commit

Permalink
Handle "start-delay" support at coldstart (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
Serkan ÖZAL authored May 7, 2022
1 parent e350d09 commit 9341013
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 26 deletions.
18 changes: 11 additions & 7 deletions src/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -71,7 +71,7 @@ const _destroyStartDelayHandler = () => {
}
}

const _startProfiler = async (opts) => {
const _doStartProfiler = async (opts) => {
const _samplingInterval =
samplingInterval ||
(opts && opts.samplingInterval) ||
Expand All @@ -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)
Expand All @@ -94,11 +102,6 @@ const _beforeInvocation = async (opts, event, context) => {
return
}

const _startDelayed = _setupStartDelayHandler(opts, event, context)
if (_startDelayed) {
return
}

await _startProfiler(opts)
}

Expand Down Expand Up @@ -144,4 +147,5 @@ const _afterInvocation = async (
module.exports = {
beforeInvocation: _beforeInvocation,
afterInvocation: _afterInvocation,
startProfiler: _startProfiler,
}
14 changes: 3 additions & 11 deletions src/handler_cjs.js
Original file line number Diff line number Diff line change
@@ -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)
}
Expand Down
10 changes: 2 additions & 8 deletions src/handler_es.mjs
Original file line number Diff line number Diff line change
@@ -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)
}
Expand Down

0 comments on commit 9341013

Please sign in to comment.