Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/web3-adapter/alchemySendHttp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,20 @@ const ALCHEMY_HEADERS = new Headers({
});
const RATE_LIMIT_STATUS = 429;

// Requests without a bound can hang indefinitely when the upstream stalls
// (TCP accepted, no response). Default to 30s so callers surface a network
// error instead of an unresolved promise.
const DEFAULT_REQUEST_TIMEOUT = 30_000;

export function makeJsonRpcHttpSender(url: string): AlchemySendJsonRpcFunction {
return async (request) => {
const response = await fetch(url, {
method: "POST",
headers: ALCHEMY_HEADERS,
body: JSON.stringify(request),
...(typeof AbortSignal !== "undefined" && AbortSignal.timeout
? { signal: AbortSignal.timeout(DEFAULT_REQUEST_TIMEOUT) }
: {}),
});
const { status } = response;
switch (status) {
Expand Down
10 changes: 9 additions & 1 deletion src/web3-adapter/sendRestPayload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import URI from "urijs";
import { FullConfig } from "../types";
import { delay } from "../util/promises";

// Unbounded fetches hang forever when an upstream stalls (TCP accepted, no
// response). Bound REST calls at 30s by default.
const DEFAULT_REQUEST_TIMEOUT = 30000;

export interface RestPayloadSender {
sendRestPayload: SendRestPayloadFunction;
}
Expand Down Expand Up @@ -67,7 +71,11 @@ export function makeRestPayloadSender({
.path(isNftPath(path) ? formatNftPath(path) : apiKey + path)
.toString();
for (let i = 0; i < maxRetries + 1; i++) {
const response = await fetch(endpoint);
const response = await fetch(endpoint, {
...(typeof AbortSignal !== "undefined" && AbortSignal.timeout
? { signal: AbortSignal.timeout(DEFAULT_REQUEST_TIMEOUT) }
: {}),
});
const { status } = response;
switch (status) {
case 200:
Expand Down