Handle rate limit errors in your code
When an SDK method or function executes when an API rate limit is exceeded in your
environment, the method or function throws an ApiException with the
message Too many API calls. Consider including logic in your code
that tests exceptions for this message and if caught, executes the script again
after waiting for a certain amount of time.
Calls that are made while a rate limit is exceeded are not counted in API rate measurements.
You can use the APIUsageAPI class of an SDK to determine call rates.
(See API Usage in the API Reference.) For
example you can search for all API calls that occur during a certain time period.
Parse the returned data to count the total calls. You can also find the number of
code 429 responses. (See Date-range searches.)
The following example catches exceptions or errors that are caused when an API rate limit is exceeded. When caught, an exponential backoff algorithm calculates the delay until the call is retried. The number of retries is capped to a maximum number.
See the following code examples:Python, JavaScript,Java .
Python
while True:
# Create a computer object and set the policy ID
computer = api.Computer()
computer.policy_id = policy_id
try:
# Modify the computer on Deep Security Manager and store the ID of the returned computer
computer = computers_api.modify_computer(computer_ids[change_count], computer, api_version, overrides=False)
modified_computer_ids.append(computer.id)
retries = 0
# Increment the count and return if all computers are modified
change_count += 1
if change_count == len(computer_ids):
return modified_computer_ids
except api_exception as e:
if e.status == 429 and retries < MAX_RETRIES:
# The error is due to exceeding an API rate limit
retries += 1
# Calculate sleep time
exp_backoff = (2 ** (retries +3)) / 1000
print("API rate limit is exceeded. Retry in {} s.".format(exp_backoff))
time.sleep(exp_backoff)
else:
# Return all other exception causes or when max retries is exceeded
return "Exception: " + str(e)
JavaScript
function modifyRecursive(computerID, retry = 0) {
return new Promise((resolve, reject) => {
// Modify the computer on the manager
computersApi
.modifyComputer(computerID, computer, apiVersion, { overrides: false })
.then(returnedComputer => {
// Resolve the ID of the modified computer
resolve(returnedComputer.ID);
})
.catch(function(error) {
if (error === "Too many API requests." && retry <= max_retries) {
// API rate limit is exceeded - calculate retry delay
const expBackoff = Math.pow(2, retry + 3);
console.log(`API rate limit exceeded. Trying again in ${expBackoff} ms.`);
setTimeout(() => {
resolve(modifyRecursive(computerID, retry + 1));
}, expBackoff);
} else {
// Any other errors or maximum retries is exceeded
reject(error);
}
});
});
}
Java
while (modifiedComputerIDs.size() < computerIDs.size()) {
// Create a computer and set the policy ID.
Computer requestComputer = new Computer();
requestComputer.setPolicyID(policyID);
// Modify the computer on Deep Security Manager
try {
// Index of computer in ComputerIDs to modify in this iteration
int i = modifiedComputerIDs.size();
Expand expand = new Expand();
Computer responseComputer = computersApi.modifyComputer(computerIDs.get(i), requestComputer, expand.list(), Boolean.FALSE, apiVersion);
modifiedComputerIDs.add(responseComputer.getID());
retries = 0;
} catch (ApiException e) {
// Check for rate limit error -- calculate sleep time and sleep
if (e.getCode() == 429 && retries <= maxRetries) {
retries += 1;
Double exp_backoff = Double.valueOf(Math.pow(2, retries + 3));
System.out.println(String.format("API rate limit exceeded. Retry in %s ms.", Integer.valueOf(exp_backoff.intValue())));
TimeUnit.MILLISECONDS.sleep(exp_backoff.intValue());
}
// Throw exception if not due to rate limiting, or max retries is exceeded
else
throw (e);
}
}
