Skip to content

Commit

Permalink
feat: add disableRetryOnRateLimit connection option (solana-labs#17709)
Browse files Browse the repository at this point in the history
Disables auto-retrying when rate limited from HTTP 429 response.
The default behavior, auto-retrying, is unchanged.
  • Loading branch information
abrkn authored Jun 4, 2021
1 parent 81bafd9 commit 847e074
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions web3.js/src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,7 @@ function createRpcClient(
useHttps: boolean,
httpHeaders?: HttpHeaders,
fetchMiddleware?: FetchMiddleware,
disableRetryOnRateLimit?: boolean,
): RpcClient {
let agentManager: AgentManager | undefined;
if (!process.env.BROWSER) {
Expand Down Expand Up @@ -764,6 +765,9 @@ function createRpcClient(
if (res.status !== 429 /* Too many requests */) {
break;
}
if (disableRetryOnRateLimit === true) {
break;
}
too_many_requests_retries -= 1;
if (too_many_requests_retries === 0) {
break;
Expand Down Expand Up @@ -1924,6 +1928,8 @@ export type ConnectionConfig = {
httpHeaders?: HttpHeaders;
/** Optional fetch middleware callback */
fetchMiddleware?: FetchMiddleware;
/** Optional Disable retring calls when server responds with HTTP 429 (Too Many Requests) */
disableRetryOnRateLimit?: boolean;
};

/**
Expand Down Expand Up @@ -2010,13 +2016,15 @@ export class Connection {
let wsEndpoint;
let httpHeaders;
let fetchMiddleware;
let disableRetryOnRateLimit;
if (commitmentOrConfig && typeof commitmentOrConfig === 'string') {
this._commitment = commitmentOrConfig;
} else if (commitmentOrConfig) {
this._commitment = commitmentOrConfig.commitment;
wsEndpoint = commitmentOrConfig.wsEndpoint;
httpHeaders = commitmentOrConfig.httpHeaders;
fetchMiddleware = commitmentOrConfig.fetchMiddleware;
disableRetryOnRateLimit = commitmentOrConfig.disableRetryOnRateLimit;
}

this._rpcEndpoint = endpoint;
Expand All @@ -2027,6 +2035,7 @@ export class Connection {
useHttps,
httpHeaders,
fetchMiddleware,
disableRetryOnRateLimit,
);
this._rpcRequest = createRpcRequest(this._rpcClient);
this._rpcBatchRequest = createRpcBatchRequest(this._rpcClient);
Expand Down

0 comments on commit 847e074

Please sign in to comment.