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

Added pressure handler to options #82

Merged
merged 10 commits into from
Feb 4, 2021
Merged

Added pressure handler to options #82

merged 10 commits into from
Feb 4, 2021

Conversation

Jelenkee
Copy link
Contributor

Heyho,
I used this plugin in one of my projects and I don't know why there is no way to just add a handler to the options to handle the 'pressure'. There was no way to determine what kind of pressure happend (eventLoopDelay, heapBytes, ...) and you could not handle the pressure and proceed the request the usual way.
So I added a way to add a pressure handler to the options.

Checklist

@mcollina
Copy link
Member

The feature is good but CI is failing badly :(

index.js Outdated
@@ -229,3 +245,5 @@ module.exports = fp(underPressure, {
fastify: '3.x',
name: 'under-pressure'
})

Object.assign(module.exports, { TYPE_EVENT_LOOP_DELAY, TYPE_EVENT_LOOP_UTILIZATION, TYPE_HEALTH_CHECK, TYPE_HEAP_USED_BYTES, TYPE_RSS_BYTES })
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use explicit module.exports.SOMETHING = statements, so that they can be used with native esm.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

index.js Outdated Show resolved Hide resolved
test.js Outdated

fastify.get('/', (req, rep) => rep.send('A'))

fastify.inject().get('/').end().then(res => t.equal(res.body, 'A'))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use async/await instead

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

test.js Outdated
setTimeout(() => {
reject(new Error(errorMessage))
}, 250)
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use async/await and

const  { promisify } = require('util')
const sleep = promisify(setTimeout)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

index.js Outdated
const result = pressureHandler(req, reply, type, value)
if (result instanceof Promise) {
result.then(() => next()).catch(next)
} else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should handle the case where result is something else of a promise.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But how? What am I supposed to do with a non-Promise? Calling reply.send() with the object/string/etc?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either that or error the response

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Co-authored-by: Matteo Collina <[email protected]>
@mcollina
Copy link
Member

CI is falling on every environment, there is a typo.

@Jelenkee
Copy link
Contributor Author

CI is falling on every environment, there is a typo.

Guess why
image
One bracket too many 😉

-used async/await
@Jelenkee
Copy link
Contributor Author

These test are quite tricky. Sometimes they fail and sometimes they pass even if I didn't change the code. Even local.
7/9 is the best I can provide atm.
event loop delay is failing on both systems though I copied the usual maxEventLoopDelay test and added the pressure handler.

@Jelenkee
Copy link
Contributor Author

Jelenkee commented Jan 30, 2021

Again 7/9. But this time other environments failed

test.js Outdated
t.error(err)
fastify.server.unref()

if (monitorEventLoopDelay) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if this is not available we should skip the test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done but it didnt fix the failing tests completely

test.js Outdated Show resolved Hide resolved
@mcollina
Copy link
Member

mcollina commented Feb 3, 2021

The tests fails for me locally as well on Mac, it's not a flake.

@mcollina
Copy link
Member

mcollina commented Feb 3, 2021

I recommend to split test.js into two (or more) files under the test/ folder. I suspect there might be a few problems of concurrency/interaction between the tests. You might also want to rebase on top of master, as well as adding disabling parallelism of tests in tap.

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@Jelenkee
Copy link
Contributor Author

Jelenkee commented Feb 4, 2021

Finally all tests are green.
Quick! Merge as soon as possible before they turn red again 😉

@mcollina mcollina merged commit 95fd612 into fastify:master Feb 4, 2021
@mcollina
Copy link
Member

mcollina commented Feb 4, 2021

Deal!

Thanks for bearing with me. This module is really tricky.

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

Successfully merging this pull request may close these issues.

2 participants