Skip to content

Avoid return 503 after closing fastify #1714

@allevo

Description

@allevo

🚀 Feature Proposal

Currently while fastify is shutting down, all APIs returns 503.
I would like to make this behaviour configurable.

Motivation

In Kubernetes, when a deployment is updated, the process receives a SIGTERM. After that, your container continues to receive incoming requests. With the current behaviour all those responses are 503, even if the process is able to serve them.

So, I cannot directly attach the SIGTERM event to fastify.close method.

Example

const fastify = require('fastify')({ returns503OnClose: false }) // default true

The changes involve those lines:

fastify/fastify.js

Lines 240 to 250 in 901911c

avvio.once('preReady', () => {
fastify.onClose((instance, done) => {
fastify[kState].closing = true
router.closeRoutes()
if (fastify[kState].listening) {
instance.server.close(done)
} else {
done(null)
}
})
})

where the router.closeRoutes() is done only if the flag returns503OnClose is set to true.

NB: According to node documentation the callback will be called only when all connections are closed, so no requests are discarded.

Edit:

old conversation #886

Metadata

Metadata

Assignees

No one assigned

    Labels

    discussionIssues or PRs with this label will never stalev2.xIssue or pr related to Fastify v2

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions