This commit is contained in:
Para Dox
2025-06-01 19:45:38 +07:00
parent 367a1890e8
commit 62928caa7a
11 changed files with 960 additions and 0 deletions

101
split-proxy/index.js Normal file
View File

@@ -0,0 +1,101 @@
const express = require('express');
const config = require('./config');
const logger = require('./logger');
const RPCProxy = require('./proxy');
const app = express();
const proxy = new RPCProxy();
// Middleware to parse JSON bodies
app.use(express.json({
limit: '50mb',
type: 'application/json'
}));
// Health check endpoint
app.get('/health', (req, res) => {
res.json({
status: 'ok',
primaryEndpoint: config.primaryRpc,
secondaryEndpoint: config.secondaryRpc,
primaryRole: config.primaryRole,
currentStreamEndpoint: proxy.streamEndpoint,
currentCompareEndpoint: proxy.compareEndpoint,
});
});
// Main JSON-RPC endpoint
app.post('/', async (req, res) => {
await proxy.handleRequest(req, res);
});
// Error handling middleware
app.use((err, req, res, next) => {
logger.error({
error: err.message,
stack: err.stack,
}, 'Unhandled error');
if (!res.headersSent) {
res.status(500).json({
jsonrpc: '2.0',
error: {
code: -32603,
message: 'Internal error',
},
id: req.body?.id,
});
}
});
// Start server
const server = app.listen(config.port, () => {
logger.info({
port: config.port,
primaryRpc: config.primaryRpc,
secondaryRpc: config.secondaryRpc,
primaryRole: config.primaryRole,
latencyThreshold: config.latencyThresholdMs,
sizeDiffThreshold: config.sizeDiffThreshold,
}, 'ETH JSON-RPC proxy started');
});
// Graceful shutdown
process.on('SIGTERM', () => {
logger.info('SIGTERM received, shutting down gracefully');
server.close(() => {
logger.info('Server closed');
process.exit(0);
});
});
process.on('SIGINT', () => {
logger.info('SIGINT received, shutting down gracefully');
server.close(() => {
logger.info('Server closed');
process.exit(0);
});
});
// Dynamic role switching with SIGUSR1
process.on('SIGUSR1', () => {
logger.info('SIGUSR1 received, switching primary/secondary roles');
try {
const newConfig = proxy.switchRoles();
logger.info({
...newConfig,
signal: 'SIGUSR1',
}, 'Successfully switched roles');
} catch (error) {
logger.error({
error: error.message,
signal: 'SIGUSR1',
}, 'Failed to switch roles');
}
});
// Log instructions for role switching on startup
logger.info('To switch primary/secondary roles at runtime, send SIGUSR1 signal:');
logger.info(' kill -USR1 <pid> OR docker kill -s USR1 <container>');
module.exports = app;