Tuesday, March 10, 2015

Documents List API Best Practices Handling errors and exponential backoff

We have recently added a new section to the Google Documents List API documentation, titled Handling API errors. A number of developers in the forum have asked what to do when certain requests cause errors, and this documentation responds to their general need for better information.

This new documentation details all errors and the scenarios that cause them. We strongly recommend that both new and advanced Google Documents List API developers read the section thoroughly.

An important technique described in the new docs is exponential backoff. Exponential backoff helps clients to automatically retry requests that fail for intermittent reasons. For example, an application might make too many requests to the API in a short period of time, resulting in HTTP 503 responses. In cases like this, it makes sense for API clients to automatically retry the requests until they succeed.

Exponential backoff can be implemented in all of the Google Data API client libraries. An example in Python follows:
import random
import time

def GetResourcesWithExponentialBackoff(client):
"""Gets all of the resources for the authorized user

Args:
client: gdata.docs.client.DocsClient authorized for a user.
Returns:
gdata.docs.data.ResourceFeed representing Resources found in request.
"""
for n in range(0, 5):
try:
response = client.GetResources()
return response
except:
time.sleep((2 ** n) + (random.randint(0, 1000) / 1000))
print "There has been an error, the request never succeeded."
return None

We strongly recommend developers take about 30 minutes and update applications to use exponential backoff. For help doing this, please read the documentation and post in the forum if you have any questions.

Vic Fryzel profile | twitter | blog

Vic is a Google engineer and open source software developer in the United States. His interests are generally in the artificial intelligence domain, specifically regarding neural networks and machine learning. Hes an amateur robotics and embedded systems engineer, and also maintains a significant interest in the security of software systems and engineering processes behind large software projects.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.