-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generalize apollo-server graceful shutdown to all integrations (#5635)
Previously, the batteries-included `apollo-server` package had a special override of `stop()` which drains the HTTP server before letting the actual Apollo Server `stop()` machinery begin. This meant that `apollo-server` followed this nice shutdown lifecycle: - Stop listening for new connections - Close all idle connections and start closing connections as they go idle - Wait a grace period for all connections to close and force-close any remaining ones - Transition ApolloServer to the stopping state, where no operations will run - Run stop hooks (eg send final usage report) This was great... but only `apollo-server` worked this way, because only `apollo-server` has full knowledge and control over its HTTP server. This PR adds a server draining step to the ApolloServer lifecycle and plugin interface, and provides a built-in plugin which drains a Node `http.Server` using the logic of the first three steps above. `apollo-server`'s behavior is now just to automatically install the plugin. Specifically: - Add a new 'phase' called `draining` that fits between `started` and `stopping`. Like `started`, operations can still execute during `draining`. Like `stopping`, any concurrent call to `stop()` will just block until the first `stop()` call finishes rather than starting a second shutdown process. - Add a new `drainServer` plugin hook (on the object returned by `serverWillStart`). Invoke all `drainServer` hooks in parallel during the `draining` phase. - Make calling `stop()` when `start()` has not yet completed successfully into an error. That behavior was previously undefined. Note that as of #5639, the automatic `stop()` call from signal handlers can't happen before `start()` succeeds. - Add `ApolloServerPluginDrainHttpServer` to `apollo-server-core`. This plugin implements `drainServer` using the `Stopper` class that was previously in the `apollo-server` package. The default grace period is 10 seconds. - Clean up integration tests to just use `stop()` with the plugin instead of separately stopping the HTTP server. Note that for Fastify specifically we also call `app.close` although there is some weirdness here around both `app.close` and our Stopper closing the same server. A comment describes the weirdness; perhaps Fastify experts can improve this later. - The Hapi web framework has built in logic that is similar to our Stopper, so `apollo-server-hapi` exports `ApolloServerPluginStopHapiServer` which should be used instead of the other plugin with Hapi. - Remove some examples from READMEs and point to examples in the docs instead. Keeping both up to date is extra work. - Fix some test issues (eg, have FakeTimers only mock out Date.now instead of setImmediate, drop an erroneous `const` which made an `app` not get cleaned up, etc). Fixes #5074.
- Loading branch information
Showing
34 changed files
with
753 additions
and
450 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
--- | ||
title: "API Reference: Drain HTTP server plugin" | ||
sidebar_title: Drain HTTP server | ||
api_reference: true | ||
--- | ||
|
||
## Using the plugin | ||
|
||
This API reference documents the `ApolloServerPluginDrainHttpServer` plugin. | ||
|
||
This plugin is designed for use with [`apollo-server-express` and other framework-specific packages](../../integrations/middleware/#all-supported-packages) which are built on top of [Node `http.Server`s](https://nodejs.org/api/http.html#http_class_http_server). It is highly recommended that you use this plugin with packages like `apollo-server-express` if you want your server to shut down gracefully. | ||
|
||
You do not need to explicitly use this plugin with the batteries-included `apollo-server` package: that package automatically uses this plugin internally. | ||
|
||
When you use this plugin, Apollo Server will drain your HTTP server when you call the `stop()` method (which is also called for you when the `SIGTERM` and `SIGINT` signals are received, unless disabled with the [`stopOnTerminationSignals` constructor option](../apollo-server/#stoponterminationsignals)). Specifically, it will: | ||
|
||
* Stop listening for new connections | ||
* Close idle connections (i.e., connections with no current HTTP request) | ||
* Close active connections whenever they become idle | ||
* Wait for all connections to be closed | ||
* After a grace period, if any connections remain active, forcefully close them. | ||
|
||
This plugin is exported from the `apollo-server-core` package. It is tested with `apollo-server-express`, `apollo-server-koa`, and `apollo-server-fastify`. (If you're using Hapi, you should instead use the `ApolloServerPluginStopHapiServer` plugin exported from the `apollo-server-hapi` package.) | ||
|
||
Here's a basic example of how to use it with Express. See the [framework integrations docs](../../integrations/middleware/) for examples of how to use it with other frameworks. | ||
|
||
```js | ||
const express = require('express'); | ||
const { ApolloServer } = require('apollo-server-express'); | ||
const { ApolloServerPluginDrainHttpServer } = require('apollo-server-core'); | ||
const { typeDefs, resolvers } = require('./schema'); | ||
|
||
async function startApolloServer() { | ||
const app = express(); | ||
const httpServer = http.createServer(app); | ||
const server = new ApolloServer({ | ||
typeDefs, | ||
resolvers, | ||
plugins: [ApolloServerPluginDrainHttpServer({ httpServer })], | ||
}); | ||
await server.start(); | ||
|
||
// Mount Apollo middleware here. | ||
server.applyMiddleware({ app }); | ||
await new Promise(resolve => httpServer.listen({ port: 4000 }, resolve)); | ||
console.log(`🚀 Server ready at http://localhost:4000${server.graphqlPath}`); | ||
return { server, app }; | ||
} | ||
``` | ||
|
||
## Options | ||
|
||
<table class="field-table"> | ||
<thead> | ||
<tr> | ||
<th>Name /<br/>Type</th> | ||
<th>Description</th> | ||
</tr> | ||
</thead> | ||
|
||
<tbody> | ||
|
||
<tr> | ||
<td> | ||
|
||
###### `httpServer` | ||
|
||
[`http.Server`](https://nodejs.org/api/http.html#http_class_http_server) | ||
</td> | ||
<td> | ||
|
||
The server to drain; required. | ||
</td> | ||
</tr> | ||
|
||
<tr> | ||
<td> | ||
|
||
###### `stopGracePeriodMillis` | ||
|
||
`number` | ||
</td> | ||
<td> | ||
|
||
How long to wait before forcefully closing non-idle connections. Defaults to `10_000` (ten seconds). | ||
</td> | ||
</tr> | ||
|
||
</tbody> | ||
</table> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.