diff --git a/lib/storage/file.js b/lib/storage/file.js index 07418515f537..da33eb564e0c 100644 --- a/lib/storage/file.js +++ b/lib/storage/file.js @@ -303,7 +303,7 @@ File.prototype.createWriteStream = function(metadata) { var numBytesWritten; var resumableUri; - var RETRY_LIMIT = 3; + var RETRY_LIMIT = 5; var retries = 0; // This is used to hold all data coming in from the user's readable stream. If @@ -510,13 +510,17 @@ File.prototype.createWriteStream = function(metadata) { return; } - if (err.code > 499 && err.code < 600 && retries <= RETRY_LIMIT) { + if (err.code > 499 && err.code < 600 && retries < RETRY_LIMIT) { + // Exponential backoff: http://goo.gl/CifIFy + var randomMs = Math.round(Math.random() * 1000); + var waitTime = Math.pow(2, retries) * 1000 + randomMs; + retries++; // Reset `numBytesWritten` so we update this value by pinging the API. numBytesWritten = null; - resumeUpload(); + setTimeout(resumeUpload, waitTime); return; }