From f85967daf83ded4b51b73a4a02c4862b30ebefb7 Mon Sep 17 00:00:00 2001 From: Ben Wilson Date: Fri, 4 Aug 2023 16:34:49 -0400 Subject: [PATCH] optimizations --- src/ORM/client/client.service.ts | 19 +++++++++++++++++-- src/app.module.ts | 3 ++- src/models/StratumV1Client.ts | 9 ++++++--- src/models/StratumV1ClientStatistics.ts | 9 ++++++++- src/services/app.service.ts | 15 ++++++++++++++- src/services/stratum-v1.service.ts | 17 ++++++++--------- 6 files changed, 55 insertions(+), 17 deletions(-) diff --git a/src/ORM/client/client.service.ts b/src/ORM/client/client.service.ts index 3829839..1d7dd88 100644 --- a/src/ORM/client/client.service.ts +++ b/src/ORM/client/client.service.ts @@ -17,10 +17,25 @@ export class ClientService { } - public async save(client: Partial) { - return await this.clientRepository.save(client); + // public async killDeadClients(){ + // var tenMinutes = new Date(new Date().getTime() - (60 * 60 * 1000)); + + // return await this.clientRepository.update({ + // deletedAt: IsNull(), + // updatedAt: LessThan(tenMinutes) + // }, { + // deletedAt: new Date() + // }); + // } + + public async heartbeat(address: string, clientName: string, sessionId: string) { + return await this.clientRepository.update({ address, clientName, sessionId }, { deletedAt: null, updatedAt: new Date() }); } + // public async save(client: Partial) { + // return await this.clientRepository.save(client); + // } + public async insert(partialClient: Partial): Promise { const insertResult = await this.clientRepository.insert(partialClient); diff --git a/src/app.module.ts b/src/app.module.ts index 0dccdbf..8154a0d 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -39,7 +39,8 @@ const ORMModules = [ autoLoadEntities: true, cache: true, logging: false, - enableWAL: true + enableWAL: true, + busyTimeout: 30 * 1000, }), CacheModule.register(), ScheduleModule.forRoot(), diff --git a/src/models/StratumV1Client.ts b/src/models/StratumV1Client.ts index afe2ea6..5333305 100644 --- a/src/models/StratumV1Client.ts +++ b/src/models/StratumV1Client.ts @@ -75,7 +75,7 @@ export class StratumV1Client extends EasyUnsubscribe { }); this.sessionStart = new Date(); - this.statistics = new StratumV1ClientStatistics(this.clientStatisticsService); + this.statistics = new StratumV1ClientStatistics(this.clientStatisticsService, this.clientService); this.extraNonceAndSessionId = this.getRandomHexString(); console.log(`New client ID: : ${this.extraNonceAndSessionId}`); } @@ -349,8 +349,11 @@ export class StratumV1Client extends EasyUnsubscribe { this.stratumV1JobsService.addJob(job); - - await this.promiseSocket.write(job.response(jobTemplate)); + try { + await this.promiseSocket.write(job.response(jobTemplate)); + } catch (e) { + console.log(e); + } console.log(`Sent new job to ${this.clientAuthorization.worker}.${this.extraNonceAndSessionId}. (clearJobs: ${jobTemplate.blockData.clearJobs}, fee?: ${!noFee})`) diff --git a/src/models/StratumV1ClientStatistics.ts b/src/models/StratumV1ClientStatistics.ts index 8fa3588..60c3bc5 100644 --- a/src/models/StratumV1ClientStatistics.ts +++ b/src/models/StratumV1ClientStatistics.ts @@ -1,5 +1,6 @@ import { ClientStatisticsService } from '../ORM/client-statistics/client-statistics.service'; import { ClientEntity } from '../ORM/client/client.entity'; +import { ClientService } from '../ORM/client/client.service'; const CACHE_SIZE = 30; const TARGET_SUBMISSION_PER_SECOND = 10; @@ -9,7 +10,10 @@ export class StratumV1ClientStatistics { private submissionCacheStart: Date; private submissionCache = []; - constructor(private readonly clientStatisticsService: ClientStatisticsService) { + constructor( + private readonly clientStatisticsService: ClientStatisticsService, + private readonly clientService: ClientService + ) { this.submissionCacheStart = new Date(); } @@ -38,6 +42,9 @@ export class StratumV1ClientStatistics { submissionHash }); + await this.clientService.heartbeat(client.address, client.clientName, client.sessionId) + + } public getLastSubmissionTime(): Date | null { return this.submissionCache[this.submissionCache.length - 1]?.time; diff --git a/src/services/app.service.ts b/src/services/app.service.ts index 492fc0b..7bed703 100644 --- a/src/services/app.service.ts +++ b/src/services/app.service.ts @@ -17,8 +17,13 @@ export class AppService implements OnModuleInit { } async onModuleInit() { + //https://phiresky.github.io/blog/2020/sqlite-performance-tuning/ //100 MB DB cache - await this.dataSource.query(`PRAGMA cache_size = -100000`); + await this.dataSource.query(`PRAGMA cache_size = -100000;`); + //Normal is still completely corruption safe in WAL mode, and means only WAL checkpoints have to wait for FSYNC. + await this.dataSource.query(`PRAGMA synchronous = normal;`); + //1Gb + await this.dataSource.query(`PRAGMA mmap_size = 1000000000;`); } @Cron(CronExpression.EVERY_HOUR) @@ -30,4 +35,12 @@ export class AppService implements OnModuleInit { console.log(`Deleted ${deletedClients.affected} old clients`); } } + + // @Cron(CronExpression.EVERY_10_MINUTES) + // private async killDeadClients() { + + // if (process.env.NODE_APP_INSTANCE == null || process.env.NODE_APP_INSTANCE == '0') { + // await this.clientService.killDeadClients(); + // } + // } } \ No newline at end of file diff --git a/src/services/stratum-v1.service.ts b/src/services/stratum-v1.service.ts index dc5d210..ddc1705 100644 --- a/src/services/stratum-v1.service.ts +++ b/src/services/stratum-v1.service.ts @@ -33,9 +33,9 @@ export class StratumV1Service implements OnModuleInit { async onModuleInit(): Promise { //await this.clientStatisticsService.deleteAll(); - if (process.env.NODE_APP_INSTANCE == null || process.env.NODE_APP_INSTANCE == '0') { - await this.clientService.deleteAll(); - } + + await this.clientService.deleteAll(); + this.startSocketServer(); } @@ -61,28 +61,27 @@ export class StratumV1Service implements OnModuleInit { promiseSocket.socket.on('end', async (error: Error) => { // Handle socket disconnection client.destroy(); - promiseSocket.destroy(); + await this.clientService.delete(client.extraNonceAndSessionId); const clientCount = await this.clientService.connectedClientCount(); - - console.log(`Client disconnected, ${client.extraNonceAndSessionId}, ${clientCount} total clients`); - + promiseSocket.destroy(); }); promiseSocket.socket.on('error', async (error: Error) => { client.destroy(); - promiseSocket.destroy(); + await this.clientService.delete(client.extraNonceAndSessionId); const clientCount = await this.clientService.connectedClientCount(); console.log(`Client disconnected, socket error, ${client.extraNonceAndSessionId}, ${clientCount} total clients`); - console.error(error); + promiseSocket.destroy(); + //console.error(error); });