tackle client disconnects

This commit is contained in:
Para Dox
2025-06-01 21:03:11 +07:00
parent 8ac43fdbac
commit cef8eff6f5
2 changed files with 89 additions and 5 deletions

View File

@@ -12,6 +12,31 @@ app.use(express.json({
type: 'application/json'
}));
// Request timeout middleware
app.use((req, res, next) => {
// Set request timeout
req.setTimeout(config.requestTimeout, () => {
logger.error({
method: req.method,
url: req.url,
timeout: config.requestTimeout,
}, 'Request timeout');
if (!res.headersSent) {
res.status(504).json({
jsonrpc: '2.0',
error: {
code: -32000,
message: 'Request timeout',
},
id: req.body?.id,
});
}
});
next();
});
// Health check endpoint
app.get('/health', (req, res) => {
res.json({
@@ -60,6 +85,19 @@ const server = app.listen(config.port, () => {
}, 'ETH JSON-RPC proxy started');
});
// Set server timeouts to be longer than request timeout
// This prevents the server from closing connections while requests are in flight
server.timeout = config.requestTimeout + 5000; // Add 5 seconds buffer
server.keepAliveTimeout = config.requestTimeout + 5000;
server.headersTimeout = config.requestTimeout + 6000; // Should be > keepAliveTimeout
logger.info({
serverTimeout: server.timeout,
keepAliveTimeout: server.keepAliveTimeout,
headersTimeout: server.headersTimeout,
requestTimeout: config.requestTimeout,
}, 'Server timeout configuration');
// Graceful shutdown
process.on('SIGTERM', () => {
logger.info('SIGTERM received, shutting down gracefully');