Skip to content

Commit b08572c

Browse files
authored
Add timeout option for HTTP requests (#1660)
1 parent db5ce6f commit b08572c

File tree

4 files changed

+18
-4
lines changed

4 files changed

+18
-4
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ and this project adheres to
66

77
## [Unreleased]
88

9+
### Added
10+
11+
- @cosmjs/tendermint-rpc: Add timeout option for HTTP requests in `HttpClient`
12+
and `HttpBatchClient`. ([#1660])
13+
14+
[#1660]: https://github.com/cosmos/cosmjs/pull/1660
15+
916
### Changed
1017

1118
- all: Drop support for Node.js < 20.

packages/tendermint-rpc/src/rpcclients/http.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,17 @@ export async function http(
1818
url: string,
1919
headers: Record<string, string> | undefined,
2020
request?: any,
21+
timeout?: number,
2122
): Promise<any> {
22-
const settings = {
23+
const settings: RequestInit = {
2324
method: method,
2425
body: request ? JSON.stringify(request) : undefined,
2526
headers: {
2627
// eslint-disable-next-line @typescript-eslint/naming-convention
2728
"Content-Type": "application/json",
2829
...headers,
2930
},
31+
signal: timeout ? AbortSignal.timeout(timeout) : undefined,
3032
};
3133
return fetch(url, settings)
3234
.then(filterBadStatus)

packages/tendermint-rpc/src/rpcclients/httpbatchclient.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export interface HttpBatchClientOptions {
1414
dispatchInterval: number;
1515
/** Max number of items sent in one request */
1616
batchSizeLimit: number;
17+
/** Timeout for HTTP requests in milliseconds. Set to undefined to disable timeout. */
18+
httpTimeout?: number;
1719
}
1820

1921
// Those values are private and can change any time.
@@ -40,6 +42,7 @@ export class HttpBatchClient implements RpcClient {
4042
this.options = {
4143
batchSizeLimit: options.batchSizeLimit ?? defaultHttpBatchClientOptions.batchSizeLimit,
4244
dispatchInterval: options.dispatchInterval ?? defaultHttpBatchClientOptions.dispatchInterval,
45+
httpTimeout: options.httpTimeout,
4346
};
4447
if (typeof endpoint === "string") {
4548
if (!hasProtocol(endpoint)) {
@@ -93,7 +96,7 @@ export class HttpBatchClient implements RpcClient {
9396
const requests = batch.map((s) => s.request);
9497
const requestIds = requests.map((request) => request.id);
9598

96-
http("POST", this.url, this.headers, requests).then(
99+
http("POST", this.url, this.headers, requests, this.options.httpTimeout).then(
97100
(raw) => {
98101
// Requests with a single entry return as an object
99102
const arr = Array.isArray(raw) ? raw : [raw];

packages/tendermint-rpc/src/rpcclients/httpclient.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ export interface HttpEndpoint {
2525
export class HttpClient implements RpcClient {
2626
protected readonly url: string;
2727
protected readonly headers: Record<string, string> | undefined;
28+
protected readonly timeout: number | undefined;
2829

29-
public constructor(endpoint: string | HttpEndpoint) {
30+
public constructor(endpoint: string | HttpEndpoint, timeout?: number) {
3031
if (typeof endpoint === "string") {
3132
if (!hasProtocol(endpoint)) {
3233
throw new Error("Endpoint URL is missing a protocol. Expected 'https://' or 'http://'.");
@@ -36,14 +37,15 @@ export class HttpClient implements RpcClient {
3637
this.url = endpoint.url;
3738
this.headers = endpoint.headers;
3839
}
40+
this.timeout = timeout;
3941
}
4042

4143
public disconnect(): void {
4244
// nothing to be done
4345
}
4446

4547
public async execute(request: JsonRpcRequest): Promise<JsonRpcSuccessResponse> {
46-
const response = parseJsonRpcResponse(await http("POST", this.url, this.headers, request));
48+
const response = parseJsonRpcResponse(await http("POST", this.url, this.headers, request, this.timeout));
4749
if (isJsonRpcErrorResponse(response)) {
4850
throw new Error(JSON.stringify(response.error));
4951
}

0 commit comments

Comments
 (0)