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

HapiJS 17 and Opossum #219

Closed
gperreymond opened this issue Aug 1, 2018 · 1 comment
Closed

HapiJS 17 and Opossum #219

gperreymond opened this issue Aug 1, 2018 · 1 comment

Comments

@gperreymond
Copy link

Hello,
I have a problem with the timeout on Hapi v17.

handler.js

function sleep (delay) {
  var start = new Date().getTime()
  while (new Date().getTime() < start + delay);
}

module.exports = async (request, h) => {
  try {
    const failure = Math.random()
    const exectime = Math.floor(Math.random() * (2000 - 100 + 1)) + 100
    if (failure <= 0.1) { throw new Error('random failure') }
    sleep(exectime)
    return { world: exectime }
  } catch (e) {
    return Promise.reject(e)
  }
}

The circuit breaker added to the route handler

const handler = require('./handler')
const circuit = new CircuitBreaker(handler, {timeout: 350})
const Hapi = require('hapi')
const instance = Hapi.server()

await instance.route({
  method: 'GET',
  path: '/hello',
  handler: async (request, h) => {
    return circuit.fire(request, h)
  }
})

When I go on /hello after the server started, random failure occured correctly.
But I never go on timeout event.

I dunno why, I check your code to see how you implements the timeout and you did something with setTimeout.
I already noticed some problems on my libraries with async/await functions and setTimeout.
I had to replace that by a Promise.race

Or perhaps I miss something when I param Opossum.

@gperreymond
Copy link
Author

I just tryed to use your timedFunction in your tests, to replace my sleep.
And timeout i working, so I think the problem came from me :)

Gilles

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

No branches or pull requests

1 participant