Skip to content

Commit 516b4bf

Browse files
authored
Merge pull request #407 from ambermartin681/feat/386-request-deduplication
feat: implement request deduplication with getDedupStats and dedupe o…
2 parents bb5bda7 + a103a2c commit 516b4bf

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

src/client.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,25 +1321,36 @@ export class StellarSplitClient {
13211321
*/
13221322
async getInvoice(
13231323
invoiceId: string,
1324+
opts?: { retry?: PerMethodRetryOptions; dedupe?: boolean }
13241325
opts?: { retry?: PerMethodRetryOptions; traceId?: string; timeout?: number }
13251326
): Promise<Invoice> {
13261327
return this._withCache("getInvoice", [invoiceId], async () => {
13271328
const fetcher = this._batcher
13281329
? () => this._batcher!.getInvoice(invoiceId)
13291330
: () => this._fetchInvoice(invoiceId, opts?.traceId);
13301331

1332+
const useDedupe = opts?.dedupe !== false;
13311333
const effectiveRetry = opts?.retry ?? (this._retryOptions ? {} : undefined);
13321334
if (this._retryOptions && effectiveRetry !== undefined) {
13331335
return await executeWithRetry(
1334-
() => this._dedup.dedupe(invoiceId, fetcher),
1336+
() => useDedupe ? this._dedup.dedupe(invoiceId, fetcher) : fetcher(),
13351337
this._retryOptions,
13361338
opts?.retry
13371339
);
13381340
}
1339-
return await this._dedup.dedupe(invoiceId, fetcher);
1341+
return useDedupe ? this._dedup.dedupe(invoiceId, fetcher) : fetcher();
13401342
});
13411343
}
13421344

1345+
/**
1346+
* Returns deduplication statistics for observability.
1347+
* @returns { deduped: number, total: number } — deduped is how many calls were short-circuited.
1348+
*/
1349+
getDedupStats(): { deduped: number; total: number } {
1350+
return this._dedup.getDedupStats();
1351+
}
1352+
1353+
private async _fetchInvoice(invoiceId: string): Promise<Invoice> {
13431354
private async _fetchInvoice(invoiceId: string, traceId?: string): Promise<Invoice> {
13441355
const startTime = Date.now();
13451356
const req = { method: "getInvoice", params: [invoiceId], headers: traceId ? { "X-Trace-Id": traceId } : undefined };

src/dedup.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,8 @@ export class Deduplicator<T> {
1919
const total = this._hits + this._misses;
2020
return total === 0 ? 0 : this._hits / total;
2121
}
22+
23+
getDedupStats(): { deduped: number; total: number } {
24+
return { deduped: this._hits, total: this._hits + this._misses };
25+
}
2226
}

0 commit comments

Comments
 (0)