under-pressure icon indicating copy to clipboard operation
under-pressure copied to clipboard

Exclude paths/routes from 503 errors

Open melroy89 opened this issue 2 years ago • 2 comments

Prerequisites

  • [X] I have written a descriptive issue title
  • [X] I have searched existing issues to ensure the feature has not already been requested

🚀 Feature Proposal

In some cases you do not want to trigger the "Service Unavailable" 503 error, a very good example would be the Swagger documentation page.

Meaning there should be an option for some kind of whitelist.

Motivation

Some pages or routes shouldn't be blocked, for example I would like to keep showing the Swagger Docs page, even if the healthCheck fails for instance.

Example

Just an proto-type idea for such an interface by introducing an option like routeWhitelist:

fastify.register(require('@fastify/under-pressure'), {
  maxEventLoopDelay: 1000,
  routeWhitelist: ['/docs'],
})

An alternative solution is to provide a config option to the route config settings for under-pressure. Just like you can set a log level per route, you should be able to "ignore" the route from under-pressure plugin.

melroy89 avatar Mar 26 '24 00:03 melroy89

Instead of providing the paths in plugin options, we can also inspect the route options.

fastify.get('/bypass', {
  config: {
    underPressure: {
      bypass: true
    }
  }
}, async function() {
  return { ok: true }
})

climba03003 avatar Mar 26 '24 05:03 climba03003

That is what I was saying as well as my alternative solution above. So let's go with that solution then, since the alternative solution using route configs is maybe better.

EDIT: Anyway, I will pick up this item. This should be durable to do for me.

melroy89 avatar Mar 26 '24 16:03 melroy89

You can do this:

const bypassPressure = {
  config: {
    pressureHandler: () => {}
  }
}

fastify.post('/', bypassPressure, async (request, reply) => {
 // ...
})

HyperCrowd avatar Jun 11 '24 14:06 HyperCrowd

I guess I fixed this last week! Thanks @HyperCrowd.

mcollina avatar Jun 11 '24 14:06 mcollina

I guess I fixed this last week! Thanks @HyperCrowd.

You did. Thanks! pressureHandler is the way now.

melroy89 avatar Jun 12 '24 08:06 melroy89

Wait... I proposed a config for under-pressure plugin specifically. This is adding an option for routes. Which is cool ofc.

However, now have the following code for configuring the Swagger route:

    await fastify.register(fastifySwaggerUi, {
      routePrefix: '/docs',
      logLevel: 'silent'
    })

How can I set pressureHandler to an empty function? Since @fastify/swagger-ui is a plugin with a routePrefix option. I don't see any option to provide route configs. After all, my request was about the swagger documentation page.

@mcollina could you re-open the issue? Unless you think I can provide route options to the fastify.register() function or an option on @fastify/swagger-ui? Or.. do you want to change https://github.com/fastify/fastify-swagger-ui instead?

melroy89 avatar Jun 12 '24 20:06 melroy89