socket cleanup

This commit is contained in:
Benjamin Wilson
2025-02-03 15:39:03 -05:00
parent 40ee610600
commit 674b02ca9d
+44 -31
View File
@@ -43,48 +43,61 @@ export class StratumV1Service implements OnModuleInit {
private startSocketServer(port: number) { private startSocketServer(port: number) {
const server = new Server(async (socket: Socket) => { const server = new Server(async (socket: Socket) => {
// Set 5-minute timeout
socket.setTimeout(1000 * 60 * 5);
//5 min const client = new StratumV1Client(
socket.setTimeout(1000 * 60 * 5); socket,
this.stratumV1JobsService,
this.bitcoinRpcService,
this.clientService,
this.clientStatisticsService,
this.notificationService,
this.blocksService,
this.configService,
this.addressSettingsService
);
const client = new StratumV1Client( // Unified cleanup function
socket, const cleanup = async (reason: string) => {
this.stratumV1JobsService, if (client.extraNonceAndSessionId != null) {
this.bitcoinRpcService, await client.destroy();
this.clientService, console.log(`Client ${client.extraNonceAndSessionId} disconnected, Reason: ${reason}`);
this.clientStatisticsService, }
this.notificationService, if (!socket.destroyed) {
this.blocksService, socket.end();
this.configService, socket.destroy();
this.addressSettingsService }
); };
// Handle client disconnection
socket.on('close', async (hadError: boolean) => {
await cleanup(hadError ? "Error" : "Normal Closure");
});
socket.on('close', async (hadError: boolean) => { // Handle socket timeouts
if (client.extraNonceAndSessionId != null) { socket.on('timeout', async () => {
// Handle socket disconnection console.log(`Socket timeout ${socket.remoteAddress}, ${socket.bytesRead}/${socket.bytesWritten}, ${client.clientSubscription?.userAgent}`);
await client.destroy(); await cleanup("Timeout");
//console.log(`Client ${client.extraNonceAndSessionId} disconnected, hadError?:${hadError}`); });
}
});
socket.on('timeout', () => { // Handle errors properly
console.log(`socket timeout ${socket.remoteAddress}, ${socket.bytesRead}/${socket.bytesWritten}, ${client.clientSubscription?.userAgent}`); socket.on('error', async (error: Error) => {
socket.end(); console.log(`Socket error: ${error.message}`);
socket.destroy(); await cleanup("Error");
}); });
socket.on('error', async (error: Error) => { });
// //console.log(`Client disconnected, socket error, ${client.extraNonceAndSessionId}`);
});
// Ensure server itself handles errors
server.on('error', (err) => {
console.error(`Server error: ${err.message}`);
}); });
server.listen(port, () => { server.listen(port, () => {
console.log(`Stratum server is listening on port ${port}`); console.log(`Stratum server is listening on port ${port}`);
}); });
}
}
} }