Skip to content

Commit

Permalink
move the code in seperate function
Browse files Browse the repository at this point in the history
  • Loading branch information
Rakhi authored and Rakhi committed Aug 8, 2023
1 parent 3a752a0 commit 5964f2c
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions collectors/okta/okta_collector.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,10 @@ class OktaCollector extends PawsCollector {
})
.catch((error) => {
error.errorCode = this._isNoErrorCode(error);
if (this._isThrottlingError(error)) {
// if x-rate-limit-reset value return by api then accordingly delay the api call to avoid throttle error again other wise increase the delay by 1min till max 15min.
let resetSeconds = state.poll_interval_sec;
if (error['headers'] && error['headers']['x-rate-limit-reset']) {
const retryEpochSeconds = parseInt((error['headers']['x-rate-limit-reset']), 10);
const currentEpochSeconds = moment().unix();
resetSeconds = retryEpochSeconds - currentEpochSeconds;
}
const delaySeconds = resetSeconds && resetSeconds < MAX_POLL_INTERVAL ? resetSeconds + 60 : MAX_POLL_INTERVAL;
state.poll_interval_sec = delaySeconds;
AlLogger.info(`OKTA000003 API limit Exceeded. The quota will be reset at ${moment().add(delaySeconds, 'seconds').toISOString()}`);
let isThrottle = this._isThrottlingError(error, state);
if (isThrottle.rateLimitError) {
state.poll_interval_sec = isThrottle.delaySeconds;
AlLogger.info(`OKTA000003 API limit Exceeded. The quota will be reset at ${moment().add(isThrottle.delaySeconds, 'seconds').toISOString()}`);
collector.reportApiThrottling(function () {
return callback(null, [], state, state.poll_interval_sec);
});
Expand All @@ -100,9 +93,24 @@ class OktaCollector extends PawsCollector {
return error.errorCode ? error.errorCode : error.status;
}

_isThrottlingError(error) {
return (error.status === 429) ||
(error.message && error.message.match(THROTTLING_ERROR_REGEXP));
_isThrottlingError(error, state) {
let isThrottlingError = {}
isThrottlingError.rateLimitError = (error.status === 429) ||
(error.message && error.message.match(THROTTLING_ERROR_REGEXP));

if (isThrottlingError.rateLimitError) {
// if x-rate-limit-reset value return by api then accordingly delay the api call to avoid throttle error again other wise increase the delay by 1min till max 15min.
let resetSeconds = state.poll_interval_sec;
if (error['headers'] && error['headers']['x-rate-limit-reset']) {
// If x-rate-limit-reset is returned by the API,Parse the Unix epoch time from the header
const retryEpochSeconds = parseInt((error['headers']['x-rate-limit-reset']), 10);
const currentEpochSeconds = moment().unix();
resetSeconds = retryEpochSeconds - currentEpochSeconds;
}
const delaySeconds = resetSeconds && resetSeconds < MAX_POLL_INTERVAL ? resetSeconds + 60 : MAX_POLL_INTERVAL;
isThrottlingError.delaySeconds = delaySeconds;
}
return isThrottlingError;
}

_getNextCollectionState(curState) {
Expand Down

0 comments on commit 5964f2c

Please sign in to comment.