Skip to content

feat(client-core): Add fetch timeout and network retry options #9484

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 16, 2025
Merged
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
feat(client-core): Add fetch timeout and network retry options
  • Loading branch information
paveltiunov committed Apr 16, 2025
commit f8ee89b3f18ef174dbf67b1f4cd8f66465d34a1f
8 changes: 8 additions & 0 deletions packages/cubejs-client-core/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ declare module '@cubejs-client/core' {
headers?: Record<string, string>;
credentials?: 'omit' | 'same-origin' | 'include';
method?: 'GET' | 'PUT' | 'POST' | 'PATCH';
/**
* Fetch timeout in milliseconds. Would be passed as AbortSignal.timeout()
*/
fetchTimeout?: number;
};

export interface ITransportResponse<R> {
Expand Down Expand Up @@ -81,6 +85,10 @@ declare module '@cubejs-client/core' {
parseDateMeasures?: boolean;
resType?: 'default' | 'compact';
castNumerics?: boolean;
/**
* How many network errors would be retried before returning to users. Default to 0.
*/
networkErrorRetries?: number;
};

export type LoadMethodOptions = {
Expand Down
6 changes: 4 additions & 2 deletions packages/cubejs-client-core/src/HttpTransport.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import fetch from 'cross-fetch';
import 'url-search-params-polyfill';

class HttpTransport {
constructor({ authorization, apiUrl, method, headers = {}, credentials }) {
constructor({ authorization, apiUrl, method, headers = {}, credentials, fetchTimeout }) {
this.authorization = authorization;
this.apiUrl = apiUrl;
this.method = method;
this.headers = headers;
this.credentials = credentials;
this.fetchTimeout = fetchTimeout;
}

request(method, { baseRequestId, ...params }) {
Expand Down Expand Up @@ -36,7 +37,8 @@ class HttpTransport {
...this.headers
},
credentials: this.credentials,
body: requestMethod === 'POST' ? JSON.stringify(params) : null
body: requestMethod === 'POST' ? JSON.stringify(params) : null,
signal: this.fetchTimeout ? AbortSignal.timeout(this.fetchTimeout) : undefined,
});

return {
Expand Down
9 changes: 8 additions & 1 deletion packages/cubejs-client-core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class CubeApi {
this.pollInterval = options.pollInterval || 5;
this.parseDateMeasures = options.parseDateMeasures;
this.castNumerics = typeof options.castNumerics === 'boolean' ? options.castNumerics : false;
this.networkErrorRetries = options.networkErrorRetries || 0;

this.updateAuthorizationPromise = null;
}
Expand Down Expand Up @@ -104,6 +105,8 @@ class CubeApi {
}
};

let networkRetries = this.networkErrorRetries;

const loadImpl = async (response, next) => {
const requestInstance = await requestPromise;

Expand Down Expand Up @@ -135,7 +138,11 @@ class CubeApi {

skipAuthorizationUpdate = false;

if (response.status === 502) {
if (response.status === 502 ||
response.error &&
response.error.toLowerCase() === 'network error' &&
--networkRetries >= 0
) {
await checkMutex();
return continueWait(true);
}
Expand Down
Loading