Skip to content

Commit

Permalink
ref(browser): Use ratelimit utils in base transport
Browse files Browse the repository at this point in the history
  • Loading branch information
AbhiPrasad committed Mar 4, 2022
1 parent c946cf6 commit 18a8a1a
Showing 1 changed file with 18 additions and 47 deletions.
65 changes: 18 additions & 47 deletions packages/browser/src/transports/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@ import {
} from '@sentry/types';
import {
createClientReportEnvelope,
disabledUntil,
dsnToString,
eventStatusFromHttpCode,
getGlobalObject,
isDebugBuild,
isRateLimited,
logger,
makePromiseBuffer,
parseRetryAfterHeader,
PromiseBuffer,
RateLimits,
serializeEnvelope,
updateRateLimits,
} from '@sentry/utils';

import { sendReport } from './utils';
Expand All @@ -53,7 +56,7 @@ export abstract class BaseTransport implements Transport {
protected readonly _buffer: PromiseBuffer<SentryResponse> = makePromiseBuffer(30);

/** Locks transport after receiving rate limits in a response */
protected readonly _rateLimits: Record<string, Date> = {};
protected _rateLimits: RateLimits = {};

protected _outcomes: { [key: string]: number } = {};

Expand Down Expand Up @@ -165,13 +168,12 @@ export abstract class BaseTransport implements Transport {
reject: (reason?: unknown) => void;
}): void {
const status = eventStatusFromHttpCode(response.status);
/**
* "The name is case-insensitive."
* https://developer.mozilla.org/en-US/docs/Web/API/Headers/get
*/
const limited = this._handleRateLimit(headers);
if (limited && isDebugBuild()) {
logger.warn(`Too many ${requestType} requests, backing off until: ${this._disabledUntil(requestType)}`);

this._rateLimits = updateRateLimits(this._rateLimits, headers);
if (isRateLimited(this._rateLimits, requestType) && isDebugBuild()) {
logger.warn(
`Too many ${requestType} requests, backing off until: ${disabledUntil(this._rateLimits, requestType)}`,
);
}

if (status === 'success') {
Expand All @@ -184,52 +186,21 @@ export abstract class BaseTransport implements Transport {

/**
* Gets the time that given category is disabled until for rate limiting
*
* @deprecated Please use `disabledUntil` from @sentry/utils
*/
protected _disabledUntil(requestType: SentryRequestType): Date {
protected _disabledUntil(requestType: SentryRequestType): number {
const category = requestTypeToCategory(requestType);
return this._rateLimits[category] || this._rateLimits.all;
return disabledUntil(this._rateLimits, category);
}

/**
* Checks if a category is rate limited
*
* @deprecated Please use `isRateLimited` from @sentry/utils
*/
protected _isRateLimited(requestType: SentryRequestType): boolean {
return this._disabledUntil(requestType) > new Date(Date.now());
}

/**
* Sets internal _rateLimits from incoming headers. Returns true if headers contains a non-empty rate limiting header.
*/
protected _handleRateLimit(headers: Record<string, string | null>): boolean {
const now = Date.now();
const rlHeader = headers['x-sentry-rate-limits'];
const raHeader = headers['retry-after'];

if (rlHeader) {
// rate limit headers are of the form
// <header>,<header>,..
// where each <header> is of the form
// <retry_after>: <categories>: <scope>: <reason_code>
// where
// <retry_after> is a delay in ms
// <categories> is the event type(s) (error, transaction, etc) being rate limited and is of the form
// <category>;<category>;...
// <scope> is what's being limited (org, project, or key) - ignored by SDK
// <reason_code> is an arbitrary string like "org_quota" - ignored by SDK
for (const limit of rlHeader.trim().split(',')) {
const parameters = limit.split(':', 2);
const headerDelay = parseInt(parameters[0], 10);
const delay = (!isNaN(headerDelay) ? headerDelay : 60) * 1000; // 60sec default
for (const category of parameters[1].split(';')) {
this._rateLimits[category || 'all'] = new Date(now + delay);
}
}
return true;
} else if (raHeader) {
this._rateLimits.all = new Date(now + parseRetryAfterHeader(now, raHeader));
return true;
}
return false;
return isRateLimited(this._rateLimits, requestType);
}

protected abstract _sendRequest(
Expand Down

0 comments on commit 18a8a1a

Please sign in to comment.