Skip to content

Improve drainServer for Fastify #5642

Closed
@glasser

Description

@glasser

In #5635 (probably AS3.2) we add the drainServer hook, a built-in ApolloServerPluginDrainHttpServer plugin that works on Node http.Servers, and a Hapi-specific server.stop() drain plugin.

Figuring out exactly what to do with Fastify seems a bit subtle. Fastify provides an app.close function which invokes some sort of complex onClose system. Among other things, it will call close on the http server and block for that to return (which means that all connections must finish).

We provide sample code on https://www.apollographql.com/docs/apollo-server/integrations/middleware/#apollo-server-fastify (and in the apollo-server-fastify ApolloServer.test.ts) which installs a custom plugin that calls app.close alongside the standard ApolloServerPluginDrainHttpServer plugin. This is a bit janky because both plugins are going to try to close the http server, and so the second one to do so will get an error! It's OK-ish if ApolloServerPluginDrainHttpServer gets that error because it ignores errors from close, but if the closes get called in the other order maybe it's bad?

The problem is that if you don't include ApolloServerPluginDrainHttpServer then nothing actively closes idle connections and you'll end up waiting until your process is forcibly killed. But if you don't call app.close then I guess other parts of the Fastify lifecycle might not run?

The ideal solution might be to replace the http server inside Fastify with one with a fancier app.close. Or maybe it's to use a version/option of ApolloServerPluginDrainHttpServer that doesn't actually call close itself, just tries to deal with open connections?

Figuring out the details here probably involves a lot more Fastify expertise than exists on the Apollo Server team. So for now we're going to not add any sort of app.close plugin to the apollo-server-fastify API, recommend some kinda verbose boilerplate in the docs that links here, and hope somebody more excited about Fastify submits a better answer.

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