optimizations

This commit is contained in:
Ben Wilson
2023-08-04 16:34:49 -04:00
parent 6423e2cb67
commit f85967daf8
6 changed files with 55 additions and 17 deletions
+17 -2
View File
@@ -17,10 +17,25 @@ export class ClientService {
} }
public async save(client: Partial<ClientEntity>) { // public async killDeadClients(){
return await this.clientRepository.save(client); // 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<ClientEntity>) {
// return await this.clientRepository.save(client);
// }
public async insert(partialClient: Partial<ClientEntity>): Promise<ClientEntity> { public async insert(partialClient: Partial<ClientEntity>): Promise<ClientEntity> {
const insertResult = await this.clientRepository.insert(partialClient); const insertResult = await this.clientRepository.insert(partialClient);
+2 -1
View File
@@ -39,7 +39,8 @@ const ORMModules = [
autoLoadEntities: true, autoLoadEntities: true,
cache: true, cache: true,
logging: false, logging: false,
enableWAL: true enableWAL: true,
busyTimeout: 30 * 1000,
}), }),
CacheModule.register(), CacheModule.register(),
ScheduleModule.forRoot(), ScheduleModule.forRoot(),
+6 -3
View File
@@ -75,7 +75,7 @@ export class StratumV1Client extends EasyUnsubscribe {
}); });
this.sessionStart = new Date(); this.sessionStart = new Date();
this.statistics = new StratumV1ClientStatistics(this.clientStatisticsService); this.statistics = new StratumV1ClientStatistics(this.clientStatisticsService, this.clientService);
this.extraNonceAndSessionId = this.getRandomHexString(); this.extraNonceAndSessionId = this.getRandomHexString();
console.log(`New client ID: : ${this.extraNonceAndSessionId}`); console.log(`New client ID: : ${this.extraNonceAndSessionId}`);
} }
@@ -349,8 +349,11 @@ export class StratumV1Client extends EasyUnsubscribe {
this.stratumV1JobsService.addJob(job); this.stratumV1JobsService.addJob(job);
try {
await this.promiseSocket.write(job.response(jobTemplate)); 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})`) console.log(`Sent new job to ${this.clientAuthorization.worker}.${this.extraNonceAndSessionId}. (clearJobs: ${jobTemplate.blockData.clearJobs}, fee?: ${!noFee})`)
+8 -1
View File
@@ -1,5 +1,6 @@
import { ClientStatisticsService } from '../ORM/client-statistics/client-statistics.service'; import { ClientStatisticsService } from '../ORM/client-statistics/client-statistics.service';
import { ClientEntity } from '../ORM/client/client.entity'; import { ClientEntity } from '../ORM/client/client.entity';
import { ClientService } from '../ORM/client/client.service';
const CACHE_SIZE = 30; const CACHE_SIZE = 30;
const TARGET_SUBMISSION_PER_SECOND = 10; const TARGET_SUBMISSION_PER_SECOND = 10;
@@ -9,7 +10,10 @@ export class StratumV1ClientStatistics {
private submissionCacheStart: Date; private submissionCacheStart: Date;
private submissionCache = []; private submissionCache = [];
constructor(private readonly clientStatisticsService: ClientStatisticsService) { constructor(
private readonly clientStatisticsService: ClientStatisticsService,
private readonly clientService: ClientService
) {
this.submissionCacheStart = new Date(); this.submissionCacheStart = new Date();
} }
@@ -38,6 +42,9 @@ export class StratumV1ClientStatistics {
submissionHash submissionHash
}); });
await this.clientService.heartbeat(client.address, client.clientName, client.sessionId)
} }
public getLastSubmissionTime(): Date | null { public getLastSubmissionTime(): Date | null {
return this.submissionCache[this.submissionCache.length - 1]?.time; return this.submissionCache[this.submissionCache.length - 1]?.time;
+14 -1
View File
@@ -17,8 +17,13 @@ export class AppService implements OnModuleInit {
} }
async onModuleInit() { async onModuleInit() {
//https://phiresky.github.io/blog/2020/sqlite-performance-tuning/
//100 MB DB cache //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) @Cron(CronExpression.EVERY_HOUR)
@@ -30,4 +35,12 @@ export class AppService implements OnModuleInit {
console.log(`Deleted ${deletedClients.affected} old clients`); 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();
// }
// }
} }
+8 -9
View File
@@ -33,9 +33,9 @@ export class StratumV1Service implements OnModuleInit {
async onModuleInit(): Promise<void> { async onModuleInit(): Promise<void> {
//await this.clientStatisticsService.deleteAll(); //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(); this.startSocketServer();
} }
@@ -61,28 +61,27 @@ export class StratumV1Service implements OnModuleInit {
promiseSocket.socket.on('end', async (error: Error) => { promiseSocket.socket.on('end', async (error: Error) => {
// Handle socket disconnection // Handle socket disconnection
client.destroy(); client.destroy();
promiseSocket.destroy();
await this.clientService.delete(client.extraNonceAndSessionId); await this.clientService.delete(client.extraNonceAndSessionId);
const clientCount = await this.clientService.connectedClientCount(); const clientCount = await this.clientService.connectedClientCount();
console.log(`Client disconnected, ${client.extraNonceAndSessionId}, ${clientCount} total clients`); console.log(`Client disconnected, ${client.extraNonceAndSessionId}, ${clientCount} total clients`);
promiseSocket.destroy();
}); });
promiseSocket.socket.on('error', async (error: Error) => { promiseSocket.socket.on('error', async (error: Error) => {
client.destroy(); client.destroy();
promiseSocket.destroy();
await this.clientService.delete(client.extraNonceAndSessionId); await this.clientService.delete(client.extraNonceAndSessionId);
const clientCount = await this.clientService.connectedClientCount(); const clientCount = await this.clientService.connectedClientCount();
console.log(`Client disconnected, socket error, ${client.extraNonceAndSessionId}, ${clientCount} total clients`); console.log(`Client disconnected, socket error, ${client.extraNonceAndSessionId}, ${clientCount} total clients`);
console.error(error); promiseSocket.destroy();
//console.error(error);
}); });