mirror of
https://github.com/Ocelot-Social-Community/Ocelot-Social.git
synced 2026-04-05 17:15:40 +00:00
prevent neo4j handle leaks in development
This commit is contained in:
parent
4245d2cbd4
commit
37d223d82f
@ -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) {
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user