claudiajs / claudia

Deploy Node.js projects to AWS Lambda and API Gateway easily
https://claudiajs.com
MIT License
3.8k stars 276 forks source link

serverless-proxy: init server once per context #147

Closed LoneRifle closed 6 years ago

LoneRifle commented 6 years ago

The current template implementation of serverless proxy appears to derive from the example from aws-serverless-express. That example (and hence this template) will create one http.Server per lambda invocation. This introduces significant delay, and for spiky traffic, would result in http.Server objects being thrown away when the excess lambda instances are removed.

This change attempts to improve things by storing an http.Server wrapped in a Promise in the execution context provided by AWS Lambda. The first lambda invocation will create the serverPromise and save it in the context. Subsequent invocations can then use this.

Given node assumes there is only a single thread of execution, it is obvious that there would be a race condition between two lambda invocations in creating and saving the serverPromise. This should only be a minor problem since the server itself is stateless and can be easily replaced without consequence.

LoneRifle commented 6 years ago

Further comments - serverPromise is removed from the context before the event is proxied to the server, in order to prevent it from being serialized to string (and hence triggering an error relating to JSON circular structures)

LoneRifle commented 6 years ago

Closing on realising that I have to pass the original context, not a copy. Further work will be done so that passing the original does not result in the problems stated earlier