I’m refactoring my cUseful library for some upcoming articles. This means updating some long standing functions that some of you may be using. All the existing ones will continue to work as before, but the new versions of them will have some additional features and will be found in various namespaces within the cUseful library.
Here’s the key, and it’s also on github

Mcbr-v4SsYKJP7JMohttAZyz3TLx7pV4j

The first new namespace is cUseful.Utils, and the first enhanced function is exponential backoff. You can see how the previous version worked with Custom checking for exponential backoff.
The new version has a couple of new features.

What is exponential backoff

This is recommended technique to use for calling services that are rate limited. They will be retried a few times if they are detected as having failed with errors that can be recovered from using a special wait algorithm. This is a much better technique than using Utilities.sleep between each call since it only waits if it needs to and therefore doesn’t waste any execution time.

How to use

Your callback function will be called with the options in force and the attempt number. The options and there defaults are as follows

Examples

In its simplest and normal form, only one argument is needed – the function to execute.
The Properties service can fail if you call it too rapidly in succession. This will take of that problem, and retry if necessary
The default checker function looks for Apps Script errors. It can be overriden and used to retry errors from external APIS that return different errors. In this example, the Github API indicates a recoverable rate limit with its own error message. If an operation that generated an error should be retried, then return true.
The lookahead can be used to force a retry, even if the function doesn’t return an error. For example, the NASA API simply sends back a message in place of the usual data. If a retry should be attempted for an operation that didn’t actually produce an error, then return true.

The code

Here’s the function code.
For more like this see Google Apps Scripts Snippets

Why not join our forum, follow the blog or follow me on twitter to ensure you get updates when they are available.