@sentry/core
Version:
Base implementation for all Sentry JavaScript SDKs
80 lines (79 loc) • 3.44 kB
JavaScript
import { safeDateNow } from "./randomSafeContext.js";
// src/utils/ratelimit.ts
const DEFAULT_RETRY_AFTER = 6e4;
/**
* Extracts Retry-After value from the request header or returns default value
* @param header string representation of 'Retry-After' header
* @param now current unix timestamp
*
*/
function parseRetryAfterHeader(header, now = safeDateNow()) {
const headerDelay = parseInt(`${header}`, 10);
if (!isNaN(headerDelay)) return headerDelay * 1e3;
const headerDate = Date.parse(`${header}`);
if (!isNaN(headerDate)) return headerDate - now;
return DEFAULT_RETRY_AFTER;
}
/**
* Gets the time that the given category is disabled until for rate limiting.
* In case no category-specific limit is set but a general rate limit across all categories is active,
* that time is returned.
*
* @return the time in ms that the category is disabled until or 0 if there's no active rate limit.
*/
function disabledUntil(limits, dataCategory) {
return limits[dataCategory] || limits.all || 0;
}
/**
* Checks if a category is rate limited
*/
function isRateLimited(limits, dataCategory, now = safeDateNow()) {
return disabledUntil(limits, dataCategory) > now;
}
/**
* Update ratelimits from incoming headers.
*
* @return the updated RateLimits object.
*/
function updateRateLimits(limits, response, now = safeDateNow()) {
const updatedRateLimits = { ...limits };
forEachRateLimit(response, now, (category, until) => {
updatedRateLimits[category] = Math.max(updatedRateLimits[category] || 0, until);
});
return updatedRateLimits;
}
/**
* Calls `extendRateLimit` with the category and deadline of each rate limit in the response.
*/
function forEachRateLimit({ statusCode, headers }, now, extendRateLimit) {
const rateLimitHeader = headers?.["x-sentry-rate-limits"];
const retryAfterHeader = headers?.["retry-after"];
if (rateLimitHeader)
/**
* rate limit headers are of the form
* <header>,<header>,..
* where each <header> is of the form
* <retry_after>: <categories>: <scope>: <reason_code>: <namespaces>
* where
* <retry_after> is a delay in seconds
* <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
* <namespaces> Semicolon-separated list of metric namespace identifiers. Defines which namespace(s) will be affected.
* Only present if rate limit applies to the metric_bucket data category.
*/
for (const limit of rateLimitHeader.trim().split(",")) {
const [retryAfter, categories, , , namespaces] = limit.split(":", 5);
const headerDelay = parseInt(retryAfter, 10);
const delay = (!isNaN(headerDelay) ? headerDelay : 60) * 1e3;
if (!categories) extendRateLimit("all", now + delay);
else for (const category of categories.split(";")) if (category === "metric_bucket") {
if (!namespaces || namespaces.split(";").includes("custom")) extendRateLimit(category, now + delay);
} else extendRateLimit(category, now + delay);
}
else if (retryAfterHeader) extendRateLimit("all", now + parseRetryAfterHeader(retryAfterHeader, now));
else if (statusCode === 429) extendRateLimit("all", now + 6e4);
}
export { DEFAULT_RETRY_AFTER, disabledUntil, isRateLimited, parseRetryAfterHeader, updateRateLimits };
//# sourceMappingURL=ratelimit.js.map