Exponential backoff

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.

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

{

sleepFor:750, // base starting sleep period between attempts if needed

maxAttempts:5, // how many times to try before giving up

logAttempts:true, // whether to alert in the log file if backoff had to happen

checker:function(), // a built in function that checks if an error is a retyable app script error - can be overridden with your own

lookahead:null // if specifed, a function that can force a retry even though no error has been thrown

}

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

var value = cUseful.Utils.expBackoff (function () {

return PropertiesService.getScriptProperties().getProperty ('key');

});

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.