@@ -19,15 +19,18 @@ import {
1919} from '@sentry/types' ;
2020import {
2121 createClientReportEnvelope ,
22+ disabledUntil ,
2223 dsnToString ,
2324 eventStatusFromHttpCode ,
2425 getGlobalObject ,
2526 isDebugBuild ,
27+ isRateLimited ,
2628 logger ,
2729 makePromiseBuffer ,
28- parseRetryAfterHeader ,
2930 PromiseBuffer ,
31+ RateLimits ,
3032 serializeEnvelope ,
33+ updateRateLimits ,
3134} from '@sentry/utils' ;
3235
3336import { sendReport } from './utils' ;
@@ -53,7 +56,7 @@ export abstract class BaseTransport implements Transport {
5356 protected readonly _buffer : PromiseBuffer < SentryResponse > = makePromiseBuffer ( 30 ) ;
5457
5558 /** Locks transport after receiving rate limits in a response */
56- protected readonly _rateLimits : Record < string , Date > = { } ;
59+ protected _rateLimits : RateLimits = { } ;
5760
5861 protected _outcomes : { [ key : string ] : number } = { } ;
5962
@@ -165,13 +168,12 @@ export abstract class BaseTransport implements Transport {
165168 reject : ( reason ?: unknown ) => void ;
166169 } ) : void {
167170 const status = eventStatusFromHttpCode ( response . status ) ;
168- /**
169- * "The name is case-insensitive."
170- * https://developer.mozilla.org/en-US/docs/Web/API/Headers/get
171- */
172- const limited = this . _handleRateLimit ( headers ) ;
173- if ( limited ) {
171+
172+ this . _rateLimits = updateRateLimits ( this . _rateLimits , headers ) ;
173+ // eslint-disable-next-line deprecation/deprecation
174+ if ( this . _isRateLimited ( requestType ) && isDebugBuild ( ) ) {
174175 isDebugBuild ( ) &&
176+ // eslint-disable-next-line deprecation/deprecation
175177 logger . warn ( `Too many ${ requestType } requests, backing off until: ${ this . _disabledUntil ( requestType ) } ` ) ;
176178 }
177179
@@ -185,52 +187,22 @@ export abstract class BaseTransport implements Transport {
185187
186188 /**
187189 * Gets the time that given category is disabled until for rate limiting
190+ *
191+ * @deprecated Please use `disabledUntil` from @sentry/utils
188192 */
189193 protected _disabledUntil ( requestType : SentryRequestType ) : Date {
190194 const category = requestTypeToCategory ( requestType ) ;
191- return this . _rateLimits [ category ] || this . _rateLimits . all ;
195+ return new Date ( disabledUntil ( this . _rateLimits , category ) ) ;
192196 }
193197
194198 /**
195199 * Checks if a category is rate limited
200+ *
201+ * @deprecated Please use `isRateLimited` from @sentry/utils
196202 */
197203 protected _isRateLimited ( requestType : SentryRequestType ) : boolean {
198- return this . _disabledUntil ( requestType ) > new Date ( Date . now ( ) ) ;
199- }
200-
201- /**
202- * Sets internal _rateLimits from incoming headers. Returns true if headers contains a non-empty rate limiting header.
203- */
204- protected _handleRateLimit ( headers : Record < string , string | null > ) : boolean {
205- const now = Date . now ( ) ;
206- const rlHeader = headers [ 'x-sentry-rate-limits' ] ;
207- const raHeader = headers [ 'retry-after' ] ;
208-
209- if ( rlHeader ) {
210- // rate limit headers are of the form
211- // <header>,<header>,..
212- // where each <header> is of the form
213- // <retry_after>: <categories>: <scope>: <reason_code>
214- // where
215- // <retry_after> is a delay in ms
216- // <categories> is the event type(s) (error, transaction, etc) being rate limited and is of the form
217- // <category>;<category>;...
218- // <scope> is what's being limited (org, project, or key) - ignored by SDK
219- // <reason_code> is an arbitrary string like "org_quota" - ignored by SDK
220- for ( const limit of rlHeader . trim ( ) . split ( ',' ) ) {
221- const parameters = limit . split ( ':' , 2 ) ;
222- const headerDelay = parseInt ( parameters [ 0 ] , 10 ) ;
223- const delay = ( ! isNaN ( headerDelay ) ? headerDelay : 60 ) * 1000 ; // 60sec default
224- for ( const category of parameters [ 1 ] . split ( ';' ) ) {
225- this . _rateLimits [ category || 'all' ] = new Date ( now + delay ) ;
226- }
227- }
228- return true ;
229- } else if ( raHeader ) {
230- this . _rateLimits . all = new Date ( now + parseRetryAfterHeader ( raHeader , now ) ) ;
231- return true ;
232- }
233- return false ;
204+ const category = requestTypeToCategory ( requestType ) ;
205+ return isRateLimited ( this . _rateLimits , category ) ;
234206 }
235207
236208 protected abstract _sendRequest (
0 commit comments