Skip to content

Commit

Permalink
draft: improved operation state mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
aruokhai committed Oct 14, 2024
1 parent 10f031f commit b6ba493
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 20 deletions.
6 changes: 0 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
"@nestjs/typeorm": "^10.0.2",
"@nestjs/websockets": "^10.3.7",
"axios": "^1.7.2",
"cron-validator": "^1.3.1",
"currency.js": "^2.0.4",
"js-yaml": "^4.1.0",
"reflect-metadata": "^0.1.13",
Expand Down
14 changes: 6 additions & 8 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@ import { BlockProviderModule } from '@/block-data-providers/block-provider.modul
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (configService: ConfigService) => {
return {
type: 'sqlite',
database: configService.get<string>('db.path'),
synchronize: configService.get<boolean>('db.synchronize'),
autoLoadEntities: true,
};
},
useFactory: (configService: ConfigService) => ({
type: 'sqlite',
database: configService.get<string>('db.path'),
synchronize: configService.get<boolean>('db.synchronize'),
autoLoadEntities: true,
}),
}),
TransactionsModule,
SilentBlocksModule,
Expand Down
9 changes: 4 additions & 5 deletions src/operation-state/operation-state.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { InjectRepository } from '@nestjs/typeorm';

@Injectable()
export class OperationStateService {
private cacheSize = 2016; // Define the maximum queue size
private cacheSize = 2016; // Define the maximum cache size

constructor(
@InjectRepository(OperationState)
Expand All @@ -16,7 +16,7 @@ export class OperationStateService {
const state = (
await this.operationStateRepository.find({
order: {
indexedBlockHeight: 'DESC', // Get the newest item first
indexedBlockHeight: 'DESC',
},
take: 1,
})
Expand All @@ -36,7 +36,7 @@ export class OperationStateService {
await this.trimState();
}

// Remove and return the oldest item in the queue
// Remove and return the oldest item in the state cache
async dequeue_operation_state(): Promise<OperationState | null> {
const latest_state = (
await this.operationStateRepository.find({
Expand All @@ -55,12 +55,11 @@ export class OperationStateService {
return null;
}

// Ensure the state size does not exceed the cache size
private async trimState(): Promise<void> {
const queueCount = await this.operationStateRepository.count();

if (queueCount > this.cacheSize) {
// Delete the oldest entries beyond the buffer size
// Delete the oldest entries from the cache
const old_states = await this.operationStateRepository.find({
order: {
indexedBlockHeight: 'ASC',
Expand Down

0 comments on commit b6ba493

Please sign in to comment.