fix(fe2): redis connection leakage

This commit is contained in:
Kristaps Fabians Geikins 2024-01-25 10:50:04 +02:00
Родитель 3e40a22ca7
Коммит ff5c965df9
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 16D20A16730A0111
2 изменённых файлов: 20 добавлений и 5 удалений

Просмотреть файл

@ -1,11 +1,18 @@
import { Redis } from 'ioredis'
/**
* Re-using the same client for all SSR reqs (shouldn't be a problem)
*/
let redis: InstanceType<typeof Redis> | undefined = undefined
/**
* Provide redis (only in SSR)
*/
export default defineNuxtPlugin(() => {
const { redisUrl } = useRuntimeConfig()
const redis = redisUrl?.length ? new Redis(redisUrl) : undefined
if (!redis) {
redis = redisUrl?.length ? new Redis(redisUrl) : undefined
}
return {
provide: {

Просмотреть файл

@ -1,4 +1,4 @@
import { MaybeAsync, Optional, md5 } from '@speckle/shared'
import { MaybeAsync, Optional, md5, wait } from '@speckle/shared'
import { dbNotificationLogger } from '@/logging/logging'
import { knex } from '@/modules/core/dbSchema'
import * as Knex from 'knex'
@ -94,10 +94,12 @@ function setupConnection(connection: pg.Connection) {
}
function reconnectClient() {
const interval = setInterval(async () => {
const reconnect = async () => {
try {
await endConnection()
dbNotificationLogger.info('Attempting to (re-)connect...')
const newConnection = await (
knex.client as Knex.Knex.Client
).acquireRawConnection()
@ -105,7 +107,6 @@ function reconnectClient() {
connection = newConnection
redisClient = createRedisClient(getRedisUrl(), {})
clearInterval(interval)
setupConnection(newConnection)
} catch (e: unknown) {
dbNotificationLogger.error(
@ -113,10 +114,17 @@ function reconnectClient() {
'Notification listener connection acquisition failed'
)
}
}, 10000)
}
void reconnect().catch(async () => {
await wait(5000) // Wait 5s and retry
reconnectClient()
})
}
const endConnection = async () => {
dbNotificationLogger.info('Ending connection...')
if (connection) {
connection.end()
connection = undefined