Skip to content
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

#5697 Added timeout before next retry for 429 error #5788

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
37 changes: 27 additions & 10 deletions extension/js/common/api/email-provider/gmail/gmail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import { Google } from './google.js';
import { GoogleOAuth } from '../../authentication/google/google-oauth.js';
import { SendableMsg } from '../sendable-msg.js';
import { KeyStore } from '../../../platform/store/key-store.js';
import { AjaxErr } from '../../shared/api-error.js';
import { AjaxErr, ApiErr, MAX_RATE_LIMIT_ERROR_RETRY_COUNT } from '../../shared/api-error.js';
import { Time } from '../../../browser/time.js';

export type GmailResponseFormat = 'raw' | 'full' | 'metadata';

Expand All @@ -33,8 +34,16 @@ export class Gmail extends EmailProviderApi implements EmailProviderInterface {
}
};

public threadGet = async (threadId: string, format?: GmailResponseFormat, progressCb?: ProgressCb): Promise<GmailRes.GmailThread> => {
return await Google.gmailCall<GmailRes.GmailThread>(this.acctEmail, `threads/${threadId}`, { method: 'GET', data: { format } }, { download: progressCb });
public threadGet = async (threadId: string, format?: GmailResponseFormat, progressCb?: ProgressCb, retryCount = 0): Promise<GmailRes.GmailThread> => {
try {
return await Google.gmailCall<GmailRes.GmailThread>(this.acctEmail, `threads/${threadId}`, { method: 'GET', data: { format } }, { download: progressCb });
} catch (e) {
if (ApiErr.isRateLimit(e) && retryCount < MAX_RATE_LIMIT_ERROR_RETRY_COUNT) {
await Time.sleep(1000);
return await this.threadGet(threadId, format, progressCb, retryCount + 1);
}
throw e;
}
};

public threadList = async (labelId: string): Promise<GmailRes.GmailThreadList> => {
Expand Down Expand Up @@ -126,13 +135,21 @@ export class Gmail extends EmailProviderApi implements EmailProviderInterface {
* because strings over 1 MB may fail to get to/from bg page. A way to mitigate that would be to pass `R.GmailMsg$raw` prop
* as a Buf instead of a string.
*/
public msgGet = async (msgId: string, format: GmailResponseFormat, progressCb?: ProgressCb): Promise<GmailRes.GmailMsg> => {
return await Google.gmailCall<GmailRes.GmailMsg>(
this.acctEmail,
`messages/${msgId}`,
{ method: 'GET', data: { format: format || 'full' } },
progressCb ? { download: progressCb } : undefined
);
public msgGet = async (msgId: string, format: GmailResponseFormat, progressCb?: ProgressCb, retryCount = 0): Promise<GmailRes.GmailMsg> => {
try {
return await Google.gmailCall<GmailRes.GmailMsg>(
this.acctEmail,
`messages/${msgId}`,
{ method: 'GET', data: { format: format || 'full' } },
progressCb ? { download: progressCb } : undefined
);
} catch (e) {
if (ApiErr.isRateLimit(e) && retryCount < MAX_RATE_LIMIT_ERROR_RETRY_COUNT) {
await Time.sleep(1000);
return await this.msgGet(msgId, format, progressCb, retryCount + 1);
}
throw e;
}
};

public msgsGet = async (msgIds: string[], format: GmailResponseFormat): Promise<GmailRes.GmailMsg[]> => {
Expand Down
2 changes: 2 additions & 0 deletions extension/js/common/api/shared/api-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ abstract class AuthErr extends Error {}
export class GoogleAuthErr extends AuthErr {}
export class BackendAuthErr extends AuthErr {}

export const MAX_RATE_LIMIT_ERROR_RETRY_COUNT = 5;

abstract class ApiCallErr extends Error {
protected static describeApiAction = (req: { url: string; method?: string; data?: unknown }) => {
const describeBody = typeof req.data === 'undefined' ? '(no body)' : typeof req.data;
Expand Down
Loading