SAMZA-174: General-purpose implementation of a retry loop. Reviewed by Chris Riccomini.