Skip to content

Commit

Permalink
refactor: use worker threads by default, remove flag, introduce in-pr…
Browse files Browse the repository at this point in the history
…ocess flag
  • Loading branch information
dnalborczyk committed Jun 8, 2022
1 parent a4b4cf8 commit 80091fb
Show file tree
Hide file tree
Showing 9 changed files with 578 additions and 481 deletions.
5 changes: 3 additions & 2 deletions src/config/commandOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,10 @@ export default {
type: 'boolean',
usage: 'Uses docker for node/python/ruby/provided',
},
useWorkerThreads: {
useInProcess: {
type: 'boolean',
usage: "Use 'worker threads' to run handlers.",
usage:
"Run handlers in the same process as 'serverless-offline'. NOTE: This can cause memory leaks and is not recommended. This option will likely be removed in future versions.",
},
webSocketHardTimeout: {
type: 'string',
Expand Down
2 changes: 1 addition & 1 deletion src/config/defaultOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default {
resourceRoutes: false,
useChildProcesses: false,
useDocker: false,
useWorkerThreads: false,
useInProcess: false,
webSocketHardTimeout: 7200,
webSocketIdleTimeout: 600,
websocketPort: 3001,
Expand Down
30 changes: 15 additions & 15 deletions src/lambda/handler-runner/HandlerRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default class HandlerRunner {
}

async #loadRunner() {
const { useDocker, useChildProcesses, useWorkerThreads, allowCache } =
const { allowCache, useChildProcesses, useDocker, useInProcess } =
this.#options

const { functionKey, handlerName, handlerPath, runtime, timeout } =
Expand Down Expand Up @@ -67,26 +67,26 @@ export default class HandlerRunner {
return new ChildProcessRunner(this.#funOptions, this.#env, allowCache)
}

if (useWorkerThreads) {
const { default: WorkerThreadRunner } = await import(
'./worker-thread-runner/index.js'
if (useInProcess) {
const { default: InProcessRunner } = await import(
'./in-process-runner/index.js'
)

return new WorkerThreadRunner(this.#funOptions, this.#env, allowCache)
return new InProcessRunner(
functionKey,
handlerPath,
handlerName,
this.#env,
timeout,
allowCache,
)
}

const { default: InProcessRunner } = await import(
'./in-process-runner/index.js'
const { default: WorkerThreadRunner } = await import(
'./worker-thread-runner/index.js'
)

return new InProcessRunner(
functionKey,
handlerPath,
handlerName,
this.#env,
timeout,
allowCache,
)
return new WorkerThreadRunner(this.#funOptions, this.#env, allowCache)
}

if (supportedGo.has(runtime)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default class InProcessRunner {
if (this.#memoryLeakWarning) {
log.warning()
log.warning(
`Running 'serverless-offline' and the handlers side-by-side in the same process with reloading enabled will cause memory leaks!`,
`Running the handlers in-process with 'serverless-offline' with reloading enabled is not recommended as it causes memory leaks!`,
)
log.warning()
this.#memoryLeakWarning = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,14 @@ export default class WorkerThreadRunner {
const { port1, port2 } = new MessageChannel()

port1
.on('message', res)
.on('message', (value) => {
if (value instanceof Error) {
rej(value)
return
}

res(value)
})
// emitted if the worker thread throws an uncaught exception.
// In that case, the worker will be terminated.
.on('error', rej)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ parentPort.on('message', async (messageData) => {
allowCache,
)

const result = await inProcessRunner.run(event, context)
let result

try {
result = await inProcessRunner.run(event, context)
} catch (err) {
port.postMessage(err)
}

// TODO check serializeability (contains function, symbol etc)
port.postMessage(result)
Expand Down
Loading

0 comments on commit 80091fb

Please sign in to comment.