prevent neo4j handle leaks in development

This commit is contained in:
Ulf Gebhardt 2026-04-04 05:10:13 +02:00
parent 4245d2cbd4
commit 37d223d82f
Signed by: ulfgebhardt
GPG Key ID: DA6B843E748679C9
2 changed files with 31 additions and 2 deletions

View File

@ -17,11 +17,25 @@ const defaultOptions = {
export function getDriver(options = {}) {
const { uri, username, password } = { ...defaultOptions, ...options }
if (!driver) {
driver = neo4j.driver(uri, neo4j.auth.basic(username, password))
driver = neo4j.driver(uri, neo4j.auth.basic(username, password), {
maxConnectionPoolSize: 50,
connectionAcquisitionTimeout: 30000,
})
}
return driver
}
export async function closeDriver() {
if (driver) {
await driver.close()
driver = undefined as unknown as Driver
}
if (neodeInstance) {
await neodeInstance.close()
neodeInstance = undefined as unknown as Neode
}
}
let neodeInstance: Neode
export function getNeode(options = {}) {
if (!neodeInstance) {

View File

@ -1,10 +1,11 @@
import CONFIG from './config'
import { closeDriver } from './db/neo4j'
import { loggerPlugin } from './plugins/apolloLogger'
import createProxy from './proxy'
import createServer from './server'
async function main() {
const { httpServer } = await createServer({
const { server, httpServer } = await createServer({
plugins: [loggerPlugin],
})
const url = new URL(CONFIG.GRAPHQL_URI)
@ -31,6 +32,20 @@ async function main() {
console.log(`Proxying requests to ${target}`)
})
}
// Graceful shutdown: close Neo4j driver and Apollo server on process signals.
// This prevents connection pool leaks during nodemon restarts in development
// and ensures clean shutdown in production.
const shutdown = async () => {
/* eslint-disable-next-line no-console */
console.log('Shutting down...')
await server.stop()
httpServer.close()
await closeDriver()
process.exit(0)
}
process.on('SIGTERM', shutdown)
process.on('SIGINT', shutdown)
}
// eslint-disable-next-line promise/prefer-await-to-callbacks, @typescript-eslint/use-unknown-in-catch-callback-variable