Skip to content

Commit 31123ac

Browse files
committed
send final error message on timeout
1 parent fd0000e commit 31123ac

File tree

3 files changed

+39
-16
lines changed

3 files changed

+39
-16
lines changed

apps/browser-proxy/src/connection-manager.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ class ConnectionManager {
1919
return this.sockets.get(connectionId)
2020
}
2121

22+
public getSocketForDatabase(databaseId: DatabaseId) {
23+
const connectionId = this.socketsByDatabase.get(databaseId)
24+
return connectionId ? this.sockets.get(connectionId) : undefined
25+
}
26+
2227
public setSocket(databaseId: DatabaseId, connectionId: ConnectionId, socket: PostgresConnection) {
2328
this.sockets.set(connectionId, socket)
2429
this.socketsByDatabase.set(databaseId, connectionId)

apps/browser-proxy/src/tcp-server.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,6 @@ tcpServer.on('connection', async (socket) => {
2626
connectionId: string
2727
} | null = null
2828

29-
// 5 minutes idle timeout for the tcp connection
30-
socket.setTimeout(1000 * 60 * 5)
31-
socket.on('timeout', () => {
32-
debug('tcp connection timeout')
33-
socket.end()
34-
})
35-
3629
debug('new tcp connection')
3730

3831
const connection = await fromNodeSocket(socket, {
@@ -110,6 +103,21 @@ tcpServer.on('connection', async (socket) => {
110103
},
111104
})
112105

106+
// 5 minutes idle timeout for the tcp connection
107+
socket.setTimeout(1000 * 60 * 5)
108+
socket.on('timeout', () => {
109+
debug('tcp connection timeout')
110+
if (connectionState) {
111+
const errorMessage = BackendError.create({
112+
code: '57P05',
113+
message: 'terminating connection due to idle timeout (5 minutes)',
114+
severity: 'FATAL',
115+
}).flush()
116+
connection.streamWriter?.write(errorMessage)
117+
}
118+
socket.end()
119+
})
120+
113121
socket.on('close', () => {
114122
if (connectionState) {
115123
connectionManager.deleteSocketForDatabase(connectionState.databaseId)

apps/browser-proxy/src/websocket-server.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { connectionManager } from './connection-manager.ts'
88
import { DatabaseShared, DatabaseUnshared, logEvent } from './telemetry.ts'
99
import { parse } from './protocol.ts'
1010
import { pgDumpMiddleware } from './pg-dump-middleware/pg-dump-middleware.ts'
11+
import { BackendError } from 'pg-gateway'
1112

1213
const debug = mainDebug.extend('websocket-server')
1314

@@ -48,15 +49,6 @@ websocketServer.on('error', (error) => {
4849
websocketServer.on('connection', async (websocket, request) => {
4950
debug('websocket connection')
5051

51-
// 1 hour lifetime for the websocket connection
52-
const websocketConnectionTimeout = setTimeout(
53-
() => {
54-
debug('websocket connection timed out')
55-
websocket.close()
56-
},
57-
1000 * 60 * 60 * 1
58-
)
59-
6052
const host = request.headers.host
6153

6254
if (!host) {
@@ -109,6 +101,24 @@ websocketServer.on('connection', async (websocket, request) => {
109101
}
110102
})
111103

104+
// 1 hour lifetime for the websocket connection
105+
const websocketConnectionTimeout = setTimeout(
106+
() => {
107+
debug('websocket connection timed out')
108+
const tcpConnection = connectionManager.getSocketForDatabase(databaseId)
109+
if (tcpConnection) {
110+
const errorMessage = BackendError.create({
111+
code: '57P01',
112+
message: 'terminating connection due to lifetime timeout (1 hour)',
113+
severity: 'FATAL',
114+
}).flush()
115+
tcpConnection.streamWriter?.write(errorMessage)
116+
}
117+
websocket.close()
118+
},
119+
1000 * 60 * 60 * 1
120+
)
121+
112122
websocket.on('close', () => {
113123
clearTimeout(websocketConnectionTimeout)
114124
connectionManager.deleteWebsocket(databaseId)

0 commit comments

Comments
 (0)