Google Apps Script Retry With Exponential Backoff
A partial mashup
As you write add-ons which run thousands of times per day, you’ll probably see situations when a backend service such as SpreadsheetApp.getActiveSpreadsheet() occasionally throws an exception. The right way to handle this is with truncated exponential backoff, providing the exception isn’t due to exceeding quota (quota denominators are generally a second or greater; retrying within this time period just ramps load in a negative feedback loop, and retry intervals beyond this have unacceptable user latency).
Peter Hermann provides a sample Javascript implementation, along with a corresponding Apps Script library. So you’re done right? Well, that depends…what if some of the functions which require retry may be passed an argument, or your backoff conditions vary according to the function? The latter is easily addressed; the former requires partials, which we’d reviewed in the the Google Apps Script Libraries And Properties article.
Which brings us to this mashup of Hermann’s sample with partials; it’s generally applicable to Javascript beyond Apps Script:
You can use this function like so in your script:
