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

View File

@ -17,10 +17,25 @@ export class ClientService {
}
public async save(client: Partial<ClientEntity>) {
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<ClientEntity>) {
// return await this.clientRepository.save(client);
// }
public async insert(partialClient: Partial<ClientEntity>): Promise<ClientEntity> {
const insertResult = await this.clientRepository.insert(partialClient);

View File

@ -39,7 +39,8 @@ const ORMModules = [
autoLoadEntities: true,
cache: true,
logging: false,
enableWAL: true
enableWAL: true,
busyTimeout: 30 * 1000,
}),
CacheModule.register(),
ScheduleModule.forRoot(),

View File

@ -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})`)

View File

@ -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;

View File

@ -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();
// }
// }
}

View File

@ -33,9 +33,9 @@ export class StratumV1Service implements OnModuleInit {
async onModuleInit(): Promise<void> {
//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);
});