Skip to content

Draining connections on close #3617

@jsumners

Description

@jsumners

Given the simple service:

'use strict'

const fastify = require('fastify')({
  logger: { level: 'debug' },
  keepAliveTimeout: 60000
})

process.on('SIGINT', () => {
  fastify.close(() => {
    process.exit()
  })
})

fastify.route({
  path: '/',
  method: 'GET',
  handler (req, res) {
    res.send({ hello: 'world' })
  }
})

fastify.listen(8000)

We can start the server and then issue the following request to it:

GET / HTTP/1.1
connection: keep-alive
keep-alive: timeout=30, max=100

Subsequently, we send SIGINT to the server to indicate we want it to shutdown. At this point, we will see that the server does not shutdown before the keep-alive has completed. Our tests show that new connections are not possible, and re-usage of the existing connection should be met with a 503: https://github.com/fastify/fastify/blob/93fe532986b96ae28a087b2ec385a8abb16cce55/test/close.test.js

Video showing server wait
fastify-shutdown.mp4

What we do not seem to have is a forceful termination of those open connections. I recognize that the spec says:

A host may keep an idle connection open for longer than timeout seconds, but the host should attempt to retain a connection for at least timeout seconds.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Keep-Alive

But when we are shutting down, we usually don't want to do that.

I think our fastify.close method is directly mapped to the avvio close method. If we want to support forceful connection draining, would we do it here?:

fastify/lib/server.js

Lines 177 to 179 in 93fe532

function close () {
this.close()
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions