- Split single p-queue (5 req/s) into 3 isolated queues: operations (5/s), instruments (20/s), users (5/s) - Removed dead instruments param from mapBrokerPortfolio - portfolio/positions endpoints share raw GetPortfolio cache - Docs: T_BANK_INSTRUMENTS_RATE_LIMIT, CACHE_TBANK_POSITIONS_TTL, rate limiting section in tbank-invest.md
40 lines
1.3 KiB
TypeScript
40 lines
1.3 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { CacheService } from '../../cache/cache.service';
|
|
import { TBANK_CACHE_KEYS } from '../tbank.config';
|
|
import type { TBankInstrument, TBankInstrumentResponse } from '../types/tbank-proto.types';
|
|
import { TBankClientService } from './tbank-client.service';
|
|
|
|
@Injectable()
|
|
export class BrokerInstrumentsService {
|
|
constructor(
|
|
private readonly tbankClient: TBankClientService,
|
|
private readonly cacheService: CacheService,
|
|
) {}
|
|
|
|
async findByInstrumentUid(instrumentUid: string): Promise<TBankInstrument | null> {
|
|
const result = await this.cacheService.getOrFetch(
|
|
TBANK_CACHE_KEYS.instrument,
|
|
[instrumentUid],
|
|
() => this.fetchByUid(instrumentUid),
|
|
'tbankInstrumentTtl',
|
|
);
|
|
|
|
return result.data;
|
|
}
|
|
|
|
private async fetchByUid(instrumentUid: string): Promise<TBankInstrument | null> {
|
|
const instrumentsClient = this.tbankClient.getServiceClient('InstrumentsService') as any;
|
|
const response = await this.tbankClient.callUnary<
|
|
{ idType: string; id: string },
|
|
TBankInstrumentResponse
|
|
>(
|
|
'InstrumentsService/GetInstrumentBy',
|
|
instrumentsClient.getInstrumentBy.bind(instrumentsClient),
|
|
{ idType: 'INSTRUMENT_ID_TYPE_UID', id: instrumentUid },
|
|
'instruments',
|
|
);
|
|
|
|
return response.instrument ?? null;
|
|
}
|
|
}
|