Fix connection reservation logic (#9153)

This commit is contained in:
Denis Bykhov
2025-06-03 08:52:04 +07:00
committed by GitHub
parent 51cc82b175
commit 1705a46bb4
5 changed files with 15 additions and 28 deletions
+1 -1
View File
@@ -53,7 +53,7 @@ export interface SessionData {
workspace: WorkspaceIds
socialStringsToUsers: Map<PersonId, AccountUuid>
asyncRequests?: (() => Promise<void>)[]
asyncRequests?: ((ctx: MeasureContext) => Promise<void>)[]
}
/**
+1 -1
View File
@@ -179,7 +179,7 @@ export class TriggersMiddleware extends BaseMiddleware implements Middleware {
} else {
ctx.contextData.asyncRequests = [
...(ctx.contextData.asyncRequests ?? []),
async () => {
async (ctx) => {
// In case of async context, we execute both async and sync triggers as sync
await this.processAsyncTriggers(ctx, triggerControl, findAll, txes, triggers)
}
+9 -21
View File
@@ -139,12 +139,12 @@ class ConnectionInfo {
readonly managed: boolean
) {}
async withReserve (reserveOrPool: boolean, action: (reservedClient: DBClient) => Promise<any>): Promise<any> {
async withReserve (action: (reservedClient: DBClient) => Promise<any>, forced: boolean = false): Promise<any> {
let reserved: DBClient | undefined
// Check if we have at least one available connection and reserve one more if required.
if (this.available.length === 0) {
if (reserveOrPool) {
if (this.managed || forced) {
reserved = await this.client.reserve()
}
} else {
@@ -164,24 +164,12 @@ class ConnectionInfo {
} catch (err: any) {
console.error('failed to release', err)
}
} else {
// after use we put into available
if (reserved !== undefined) {
} else if (reserved !== undefined) {
if (this.available.length > 0) {
reserved?.release()
} else {
this.available.push(reserved)
}
if (this.available.length > 1) {
// We need to release any >= 1
const toRelease = this.available.splice(1, this.available.length - 1)
for (const r of toRelease) {
try {
r.release()
} catch (err: any) {
console.error('failed to relase', err)
}
}
}
}
}
}
@@ -212,7 +200,7 @@ class ConnectionMgr {
try {
while (true) {
const retry: boolean | Error = await connection.withReserve(true, async (client) => {
const retry: boolean | Error = await connection.withReserve(async (client) => {
tries++
try {
await client.execute('BEGIN;')
@@ -231,7 +219,7 @@ class ConnectionMgr {
return false
}
}
})
}, true)
if (retry === true) {
break
}
@@ -261,7 +249,7 @@ class ConnectionMgr {
try {
while (true) {
const retry: false | { result: any } | Error = await connection.withReserve(false, async (client) => {
const retry: false | { result: any } | Error = await connection.withReserve(async (client) => {
tries++
try {
return { result: await fn(client) }
+2 -3
View File
@@ -318,9 +318,8 @@ export function getDBClient (
database,
max: 10,
min: 2,
connect_timeout: 10,
idle_timeout: 30,
max_lifetime: 300,
connect_timeout: 30,
idle_timeout: 0,
transform: {
undefined: null
},
+2 -2
View File
@@ -251,8 +251,8 @@ export class ClientSession implements Session {
onEnd = useReserveContext ? ctx.pipeline.context.adapterManager?.reserveContext?.(cid) : undefined
const handleAyncs = async (): Promise<void> => {
try {
for (const r of (ctx.ctx.contextData as SessionData).asyncRequests ?? []) {
await r()
for (const r of asyncs) {
await r(ctx.ctx)
}
} finally {
onEnd?.()