ton-provider-system 0.1.5 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1318,6 +1318,8 @@ var TokenBucketRateLimiter = class {
1318
1318
  if (this.currentBackoff > 0) {
1319
1319
  this.logger.debug(`Applying backoff: ${this.currentBackoff}ms`);
1320
1320
  await sleep(this.currentBackoff);
1321
+ this.lastRefill = Date.now();
1322
+ this.currentBackoff = 0;
1321
1323
  }
1322
1324
  while (this.tokens <= 0) {
1323
1325
  if (Date.now() - startTime > timeoutMs) {
@@ -2557,16 +2559,42 @@ async function getTonClient(manager) {
2557
2559
  async function getTonClientWithRateLimit(manager) {
2558
2560
  const adapter = new NodeAdapter(manager);
2559
2561
  const client = await adapter.getClient();
2560
- const withRateLimit = async (fn) => {
2561
- await manager.getEndpointWithRateLimit();
2562
- try {
2563
- const result = await fn();
2564
- manager.reportSuccess();
2565
- return result;
2566
- } catch (error) {
2567
- manager.reportError(error);
2568
- throw error;
2562
+ const withRateLimit = async (fn, maxRetries = 3) => {
2563
+ let lastError;
2564
+ for (let attempt = 0; attempt <= maxRetries; attempt++) {
2565
+ try {
2566
+ await manager.getEndpointWithRateLimit(6e4);
2567
+ const result = await fn();
2568
+ manager.reportSuccess();
2569
+ return result;
2570
+ } catch (error) {
2571
+ lastError = error;
2572
+ const errorMsg = error?.message || String(error) || "";
2573
+ const is429 = errorMsg.includes("429") || errorMsg.includes("rate limit") || error?.status === 429 || error?.response?.status === 429;
2574
+ if (is429 && attempt < maxRetries) {
2575
+ manager.reportError(error);
2576
+ const managerState = manager.getState();
2577
+ const provider = manager.getActiveProvider();
2578
+ let backoff = 1e3;
2579
+ if (provider && managerState.providers) {
2580
+ const providerState = managerState.providers.get(provider.id);
2581
+ if (providerState?.rateLimit?.currentBackoff) {
2582
+ backoff = providerState.rateLimit.currentBackoff;
2583
+ }
2584
+ }
2585
+ const additionalDelay = Math.min(attempt * 500, 2e3);
2586
+ const waitTime = backoff + additionalDelay;
2587
+ console.warn(
2588
+ `[ProviderSystem] Rate limit error (429), retrying in ${waitTime}ms (attempt ${attempt + 1}/${maxRetries + 1})`
2589
+ );
2590
+ await sleep(waitTime);
2591
+ continue;
2592
+ }
2593
+ manager.reportError(error);
2594
+ throw error;
2595
+ }
2569
2596
  }
2597
+ throw lastError || new Error("Rate limit retries exhausted");
2570
2598
  };
2571
2599
  return { client, withRateLimit };
2572
2600
  }