Throttling on API Routes

Table of Contents

Plow Technologies has rolled out throttling on some OnPing API routes, with more to be added in the future. This is necessary to protect our systems from being overloaded by automated requests and to preserve availability and quality of service for all of our customers.

When a user makes more requests than is permitted by our throttling policy, OnPing will return the HTTP status code 429 Too Many Requests.

This status code indicates that the user has made more requests than the throttling policy allows. The response will include a header indicating how many seconds to wait before making another request. Note that waiting this long does not guarantee that a request will be successful. It is only the server’s estimate of how long you must wait before one more request will be allowed.

It is not difficult to make scripts gracefully handling throttling. The “Too many requests” status code should not be treated as an error, but as a request to slow the rate of requests. Automated processes which receive this response should wait at least the specified amount of time and then retry the request.

Python #

In Python, we can investigate the status of a response returned to a request made with the requests library:

import requests
import time

while(true):
r = requests.get('https://onping.plowtech.net/path/to/api', json = myJson, cookies = myCookies)
if r.status_code == requests.codes.too_many_requests:
time.sleep(int(r.headers['retry-after'])+5)
continue
else:
break
print r.text

By putting the request inside the loop, we can easily conditionally retry the request. Before retrying, we look up the retry-after header from the response, and sleep for that many seconds

Powered by BetterDocs