Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move 'doNotWaitForEmptyEventLoop' middleware from 'after' step to 'before'? #93

Closed
vladholubiev opened this issue Jan 18, 2018 · 4 comments

Comments

@vladholubiev
Copy link
Contributor

vladholubiev commented Jan 18, 2018

Today I added this middleware ⬇️ and I discovered my Lambdas were timing out when they didn't pass validation

module.exports = () => ({
after: (handler, next) => {
handler.context.callbackWaitsForEmptyEventLoop = false

It sets false in after step, meaning after handler execution. But if validation fails, handler never runs, and I get a timeout.

I changed after to before and it works as expected now.

Any risk making the same change in the framework itself? Was there a reason to do so specifically?

I mean it's a tiny change, it's faster to submit a PR then writing down this 😅 but who knows, was it by design?

@lmammino
Copy link
Member

Thanks @vladgolubev, that's probably a good idea. I think we have to make this middleware slightly smarter to handle this case (and more).

The original idea of having it in the after phase was to make sure that the user does not override it by mistake, but we didn't think it might have affected error cases.

One idea could be to set the callbackWaitsForEmptyEventLoop to false in both phases (before and after) and probably we should do it also in the onError phase.

Also, we could assume the before phase is mandatory and the after and onError phases could be switched off through config.

Using these ideas, this can be a first draft of the new version:

module.exports = (opts) => {
  const defaults = {
    runOnAfter: true,
    runOnError: true
  }

  const options = Object.assign({}, defaults, opts)

  const setFlag = (handler, next) => {
    handler.context.callbackWaitsForEmptyEventLoop = false
    next()
  }

  return ({
    before: setFlag,
    after: options.runOnAfter ? setFlag : undefined,
    onError: options.runOnError ? setFlag : undefined
  })
}

@dkatavic, what do you think?

@dkatavic
Copy link
Contributor

Good catch @vladgolubev . I like the draft @lmammino , just I would like to make the middleware as configurable as possible, so can we add runOnBefore option too?

@lmammino
Copy link
Member

That makes totally sense @dkatavic. Thanks!

Probably we should also set more efficient defaults like:

const defaults = {
    runOnBefore: true,
    runOnAfter: false,  // user code (or other middlewares) might reset the flag, but no extra function is executed
    runOnError: false  // user code (or other middlewares) might reset the flag, but no extra function is executed
  }

@vladgolubev, do you want to take care of submitting a PR with this? 😉

@vladholubiev
Copy link
Contributor Author

@lmammino 🤔 🤝

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants