ton-provider-system 0.1.6 → 0.1.8
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 +83 -27
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +83 -27
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -1474,7 +1474,7 @@ declare function getTonClient(manager: ProviderManager): Promise<TonClient>;
|
|
|
1474
1474
|
*/
|
|
1475
1475
|
declare function getTonClientWithRateLimit(manager: ProviderManager): Promise<{
|
|
1476
1476
|
client: TonClient;
|
|
1477
|
-
withRateLimit: <T>(fn: () => Promise<T
|
|
1477
|
+
withRateLimit: <T>(fn: () => Promise<T>, maxRetries?: number) => Promise<T>;
|
|
1478
1478
|
}>;
|
|
1479
1479
|
/**
|
|
1480
1480
|
* Get TonClient for network (one-shot convenience)
|
package/dist/index.d.ts
CHANGED
|
@@ -1474,7 +1474,7 @@ declare function getTonClient(manager: ProviderManager): Promise<TonClient>;
|
|
|
1474
1474
|
*/
|
|
1475
1475
|
declare function getTonClientWithRateLimit(manager: ProviderManager): Promise<{
|
|
1476
1476
|
client: TonClient;
|
|
1477
|
-
withRateLimit: <T>(fn: () => Promise<T
|
|
1477
|
+
withRateLimit: <T>(fn: () => Promise<T>, maxRetries?: number) => Promise<T>;
|
|
1478
1478
|
}>;
|
|
1479
1479
|
/**
|
|
1480
1480
|
* Get TonClient for network (one-shot convenience)
|
package/dist/index.js
CHANGED
|
@@ -947,14 +947,23 @@ var HealthChecker = class {
|
|
|
947
947
|
markDegraded(providerId, network, error) {
|
|
948
948
|
const key = this.getResultKey(providerId, network);
|
|
949
949
|
const existing = this.results.get(key);
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
950
|
+
const result = existing ? {
|
|
951
|
+
...existing,
|
|
952
|
+
status: "degraded",
|
|
953
|
+
error: error || "Marked as degraded",
|
|
954
|
+
lastTested: /* @__PURE__ */ new Date()
|
|
955
|
+
} : {
|
|
956
|
+
id: providerId,
|
|
957
|
+
network,
|
|
958
|
+
success: false,
|
|
959
|
+
status: "degraded",
|
|
960
|
+
latencyMs: null,
|
|
961
|
+
seqno: null,
|
|
962
|
+
blocksBehind: 0,
|
|
963
|
+
lastTested: /* @__PURE__ */ new Date(),
|
|
964
|
+
error: error || "Marked as degraded"
|
|
965
|
+
};
|
|
966
|
+
this.results.set(key, result);
|
|
958
967
|
}
|
|
959
968
|
/**
|
|
960
969
|
* Mark a provider as offline
|
|
@@ -962,14 +971,25 @@ var HealthChecker = class {
|
|
|
962
971
|
markOffline(providerId, network, error) {
|
|
963
972
|
const key = this.getResultKey(providerId, network);
|
|
964
973
|
const existing = this.results.get(key);
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
}
|
|
974
|
+
const result = existing ? {
|
|
975
|
+
...existing,
|
|
976
|
+
status: "offline",
|
|
977
|
+
success: false,
|
|
978
|
+
// Ensure success is false for offline providers
|
|
979
|
+
error: error || "Marked as offline",
|
|
980
|
+
lastTested: /* @__PURE__ */ new Date()
|
|
981
|
+
} : {
|
|
982
|
+
id: providerId,
|
|
983
|
+
network,
|
|
984
|
+
success: false,
|
|
985
|
+
status: "offline",
|
|
986
|
+
latencyMs: null,
|
|
987
|
+
seqno: null,
|
|
988
|
+
blocksBehind: 0,
|
|
989
|
+
lastTested: /* @__PURE__ */ new Date(),
|
|
990
|
+
error: error || "Marked as offline"
|
|
991
|
+
};
|
|
992
|
+
this.results.set(key, result);
|
|
973
993
|
}
|
|
974
994
|
// ========================================================================
|
|
975
995
|
// Private Methods
|
|
@@ -2092,12 +2112,22 @@ var _ProviderManager = class _ProviderManager {
|
|
|
2092
2112
|
if (!this.initialized || !this.network) return;
|
|
2093
2113
|
const provider = this.selector.getBestProvider(this.network);
|
|
2094
2114
|
if (!provider) return;
|
|
2095
|
-
const errorMsg = error instanceof Error ? error.message : error;
|
|
2096
|
-
|
|
2115
|
+
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
2116
|
+
const errorMsgLower = errorMsg.toLowerCase();
|
|
2117
|
+
const is429 = errorMsgLower.includes("429") || errorMsgLower.includes("rate limit");
|
|
2118
|
+
const is503 = errorMsgLower.includes("503") || errorMsgLower.includes("service unavailable");
|
|
2119
|
+
const is502 = errorMsgLower.includes("502") || errorMsgLower.includes("bad gateway");
|
|
2120
|
+
const is404 = errorMsgLower.includes("404") || errorMsgLower.includes("not found");
|
|
2121
|
+
const isTimeout = errorMsgLower.includes("timeout") || errorMsgLower.includes("abort");
|
|
2122
|
+
if (isRateLimitError(error) || is429) {
|
|
2097
2123
|
this.rateLimiter.reportRateLimitError(provider.id);
|
|
2098
2124
|
this.healthChecker.markDegraded(provider.id, this.network, errorMsg);
|
|
2125
|
+
} else if (is503 || is502 || is404 || isTimeout) {
|
|
2126
|
+
this.rateLimiter.reportError(provider.id);
|
|
2127
|
+
this.healthChecker.markOffline(provider.id, this.network, errorMsg);
|
|
2099
2128
|
} else {
|
|
2100
2129
|
this.rateLimiter.reportError(provider.id);
|
|
2130
|
+
this.healthChecker.markDegraded(provider.id, this.network, errorMsg);
|
|
2101
2131
|
}
|
|
2102
2132
|
this.selector.handleProviderFailure(provider.id, this.network);
|
|
2103
2133
|
this.notifyListeners();
|
|
@@ -2556,16 +2586,42 @@ async function getTonClient(manager) {
|
|
|
2556
2586
|
async function getTonClientWithRateLimit(manager) {
|
|
2557
2587
|
const adapter = new NodeAdapter(manager);
|
|
2558
2588
|
const client = await adapter.getClient();
|
|
2559
|
-
const withRateLimit = async (fn) => {
|
|
2560
|
-
|
|
2561
|
-
|
|
2562
|
-
|
|
2563
|
-
|
|
2564
|
-
|
|
2565
|
-
|
|
2566
|
-
|
|
2567
|
-
|
|
2589
|
+
const withRateLimit = async (fn, maxRetries = 3) => {
|
|
2590
|
+
let lastError;
|
|
2591
|
+
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
2592
|
+
try {
|
|
2593
|
+
await manager.getEndpointWithRateLimit(6e4);
|
|
2594
|
+
const result = await fn();
|
|
2595
|
+
manager.reportSuccess();
|
|
2596
|
+
return result;
|
|
2597
|
+
} catch (error) {
|
|
2598
|
+
lastError = error;
|
|
2599
|
+
const errorMsg = error?.message || String(error) || "";
|
|
2600
|
+
const is429 = errorMsg.includes("429") || errorMsg.includes("rate limit") || error?.status === 429 || error?.response?.status === 429;
|
|
2601
|
+
if (is429 && attempt < maxRetries) {
|
|
2602
|
+
manager.reportError(error);
|
|
2603
|
+
const managerState = manager.getState();
|
|
2604
|
+
const provider = manager.getActiveProvider();
|
|
2605
|
+
let backoff = 1e3;
|
|
2606
|
+
if (provider && managerState.providers) {
|
|
2607
|
+
const providerState = managerState.providers.get(provider.id);
|
|
2608
|
+
if (providerState?.rateLimit?.currentBackoff) {
|
|
2609
|
+
backoff = providerState.rateLimit.currentBackoff;
|
|
2610
|
+
}
|
|
2611
|
+
}
|
|
2612
|
+
const additionalDelay = Math.min(attempt * 500, 2e3);
|
|
2613
|
+
const waitTime = backoff + additionalDelay;
|
|
2614
|
+
console.warn(
|
|
2615
|
+
`[ProviderSystem] Rate limit error (429), retrying in ${waitTime}ms (attempt ${attempt + 1}/${maxRetries + 1})`
|
|
2616
|
+
);
|
|
2617
|
+
await sleep(waitTime);
|
|
2618
|
+
continue;
|
|
2619
|
+
}
|
|
2620
|
+
manager.reportError(error);
|
|
2621
|
+
throw error;
|
|
2622
|
+
}
|
|
2568
2623
|
}
|
|
2624
|
+
throw lastError || new Error("Rate limit retries exhausted");
|
|
2569
2625
|
};
|
|
2570
2626
|
return { client, withRateLimit };
|
|
2571
2627
|
}
|