Skip to content

Commit

Permalink
fix: Add SentryRequestType to RateLimitingCategory mapping (#3328)
Browse files Browse the repository at this point in the history
* fix: Add SentryRequestType to RateLimitingCategory mapping

* Update remaining tests
  • Loading branch information
kamilogorek authored Mar 23, 2021
1 parent 3eb9e05 commit a6f8dc2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
15 changes: 12 additions & 3 deletions packages/browser/src/transports/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ import {
} from '@sentry/types';
import { logger, parseRetryAfterHeader, PromiseBuffer, SentryError } from '@sentry/utils';

const CATEGORY_MAPPING: {
[key in SentryRequestType]: string;
} = {
event: 'error',
transaction: 'transaction',
session: 'session',
};

/** Base Transport class implementation */
export abstract class BaseTransport implements Transport {
/**
Expand Down Expand Up @@ -80,15 +88,16 @@ export abstract class BaseTransport implements Transport {
/**
* Gets the time that given category is disabled until for rate limiting
*/
protected _disabledUntil(category: string): Date {
protected _disabledUntil(requestType: SentryRequestType): Date {
const category = CATEGORY_MAPPING[requestType];
return this._rateLimits[category] || this._rateLimits.all;
}

/**
* Checks if a category is rate limited
*/
protected _isRateLimited(category: string): boolean {
return this._disabledUntil(category) > new Date(Date.now());
protected _isRateLimited(requestType: SentryRequestType): boolean {
return this._disabledUntil(requestType) > new Date(Date.now());
}

/**
Expand Down
8 changes: 4 additions & 4 deletions packages/browser/test/unit/transports/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ describe('FetchTransport', () => {
.returns(afterLimit);

const headers = new Headers();
headers.set('X-Sentry-Rate-Limits', `${retryAfterSeconds}:event:scope`);
headers.set('X-Sentry-Rate-Limits', `${retryAfterSeconds}:error:scope`);
fetch.returns(Promise.resolve({ status: 429, headers }));

try {
Expand Down Expand Up @@ -290,7 +290,7 @@ describe('FetchTransport', () => {
.returns(afterLimit);

const headers = new Headers();
headers.set('X-Sentry-Rate-Limits', `${retryAfterSeconds}:event;transaction:scope`);
headers.set('X-Sentry-Rate-Limits', `${retryAfterSeconds}:error;transaction:scope`);
fetch.returns(Promise.resolve({ status: 429, headers }));

try {
Expand Down Expand Up @@ -366,7 +366,7 @@ describe('FetchTransport', () => {
.returns(afterLimit);

const headers = new Headers();
headers.set('X-Sentry-Rate-Limits', `${retryAfterSeconds}:event;transaction:scope`);
headers.set('X-Sentry-Rate-Limits', `${retryAfterSeconds}:error;transaction:scope`);
fetch.returns(Promise.resolve({ status: 429, headers }));

try {
Expand Down Expand Up @@ -433,7 +433,7 @@ describe('FetchTransport', () => {
.returns(afterLimit);

const headers = new Headers();
headers.set('X-Sentry-Rate-Limits', `${retryAfterSeconds}:event;transaction:scope`);
headers.set('X-Sentry-Rate-Limits', `${retryAfterSeconds}:error;transaction:scope`);
fetch.returns(Promise.resolve({ status: 200, headers }));

let eventRes = await transport.sendEvent(eventPayload);
Expand Down
6 changes: 3 additions & 3 deletions packages/browser/test/unit/transports/xhr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ describe('XHRTransport', () => {
const withinLimit = beforeLimit + (retryAfterSeconds / 2) * 1000;
const afterLimit = beforeLimit + retryAfterSeconds * 1000;

server.respondWith('POST', storeUrl, [429, { 'X-Sentry-Rate-Limits': `${retryAfterSeconds}:event:scope` }, '']);
server.respondWith('POST', storeUrl, [429, { 'X-Sentry-Rate-Limits': `${retryAfterSeconds}:error:scope` }, '']);
server.respondWith('POST', envelopeUrl, [200, {}, '']);

const dateStub = stub(Date, 'now')
Expand Down Expand Up @@ -202,7 +202,7 @@ describe('XHRTransport', () => {

server.respondWith('POST', storeUrl, [
429,
{ 'X-Sentry-Rate-Limits': `${retryAfterSeconds}:event;transaction:scope` },
{ 'X-Sentry-Rate-Limits': `${retryAfterSeconds}:error;transaction:scope` },
'',
]);
server.respondWith('POST', envelopeUrl, [200, {}, '']);
Expand Down Expand Up @@ -356,7 +356,7 @@ describe('XHRTransport', () => {
const withinLimit = beforeLimit + (retryAfterSeconds / 2) * 1000;
const afterLimit = beforeLimit + retryAfterSeconds * 1000;

server.respondWith('POST', storeUrl, [200, { 'X-Sentry-Rate-Limits': `${retryAfterSeconds}:event:scope` }, '']);
server.respondWith('POST', storeUrl, [200, { 'X-Sentry-Rate-Limits': `${retryAfterSeconds}:error:scope` }, '']);

const dateStub = stub(Date, 'now')
// 1st event - _isRateLimited - false
Expand Down

0 comments on commit a6f8dc2

Please sign in to comment.