feat: add disableRetryOnRateLimit connection option (#17709)

Disables auto-retrying when rate limited from HTTP 429 response.
The default behavior, auto-retrying, is unchanged.
This commit is contained in:
Andreas Brekken
2021-06-04 11:17:19 +07:00
committed by GitHub
parent 81bafd9daf
commit 847e074943

View File

@ -714,6 +714,7 @@ function createRpcClient(
useHttps: boolean, useHttps: boolean,
httpHeaders?: HttpHeaders, httpHeaders?: HttpHeaders,
fetchMiddleware?: FetchMiddleware, fetchMiddleware?: FetchMiddleware,
disableRetryOnRateLimit?: boolean,
): RpcClient { ): RpcClient {
let agentManager: AgentManager | undefined; let agentManager: AgentManager | undefined;
if (!process.env.BROWSER) { if (!process.env.BROWSER) {
@ -764,6 +765,9 @@ function createRpcClient(
if (res.status !== 429 /* Too many requests */) { if (res.status !== 429 /* Too many requests */) {
break; break;
} }
if (disableRetryOnRateLimit === true) {
break;
}
too_many_requests_retries -= 1; too_many_requests_retries -= 1;
if (too_many_requests_retries === 0) { if (too_many_requests_retries === 0) {
break; break;
@ -1924,6 +1928,8 @@ export type ConnectionConfig = {
httpHeaders?: HttpHeaders; httpHeaders?: HttpHeaders;
/** Optional fetch middleware callback */ /** Optional fetch middleware callback */
fetchMiddleware?: FetchMiddleware; fetchMiddleware?: FetchMiddleware;
/** Optional Disable retring calls when server responds with HTTP 429 (Too Many Requests) */
disableRetryOnRateLimit?: boolean;
}; };
/** /**
@ -2010,6 +2016,7 @@ export class Connection {
let wsEndpoint; let wsEndpoint;
let httpHeaders; let httpHeaders;
let fetchMiddleware; let fetchMiddleware;
let disableRetryOnRateLimit;
if (commitmentOrConfig && typeof commitmentOrConfig === 'string') { if (commitmentOrConfig && typeof commitmentOrConfig === 'string') {
this._commitment = commitmentOrConfig; this._commitment = commitmentOrConfig;
} else if (commitmentOrConfig) { } else if (commitmentOrConfig) {
@ -2017,6 +2024,7 @@ export class Connection {
wsEndpoint = commitmentOrConfig.wsEndpoint; wsEndpoint = commitmentOrConfig.wsEndpoint;
httpHeaders = commitmentOrConfig.httpHeaders; httpHeaders = commitmentOrConfig.httpHeaders;
fetchMiddleware = commitmentOrConfig.fetchMiddleware; fetchMiddleware = commitmentOrConfig.fetchMiddleware;
disableRetryOnRateLimit = commitmentOrConfig.disableRetryOnRateLimit;
} }
this._rpcEndpoint = endpoint; this._rpcEndpoint = endpoint;
@ -2027,6 +2035,7 @@ export class Connection {
useHttps, useHttps,
httpHeaders, httpHeaders,
fetchMiddleware, fetchMiddleware,
disableRetryOnRateLimit,
); );
this._rpcRequest = createRpcRequest(this._rpcClient); this._rpcRequest = createRpcRequest(this._rpcClient);
this._rpcBatchRequest = createRpcBatchRequest(this._rpcClient); this._rpcBatchRequest = createRpcBatchRequest(this._rpcClient);