mirror of
https://github.com/abraunegg/onedrive
synced 2024-05-10 17:56:44 +02:00
* Adding a retry method for downloading files when a HTTP request returned status code 429 (Too Many Requests) response is returned from OneDrive
This commit is contained in:
parent
a08df3d7fd
commit
b9faa5cd1f
|
@ -582,6 +582,13 @@ final class OneDriveApi
|
|||
log.vlog("OneDrive returned a 'HTTP 415 - Unsupported Media Type' - gracefully handling error");
|
||||
break;
|
||||
|
||||
// 429 - Too Many Requests
|
||||
case 429:
|
||||
// Too many requests in a certain time window
|
||||
// https://docs.microsoft.com/en-us/sharepoint/dev/general-development/how-to-avoid-getting-throttled-or-blocked-in-sharepoint-online
|
||||
log.vlog("OneDrive returned a 'HTTP 429 - Too Many Requests' - gracefully handling error");
|
||||
break;
|
||||
|
||||
// Server side (OneDrive) Errors
|
||||
// 500 - Internal Server Error
|
||||
// 502 - Bad Gateway
|
||||
|
|
27
src/sync.d
27
src/sync.d
|
@ -5,6 +5,7 @@ import std.exception: enforce;
|
|||
import std.file, std.json, std.path;
|
||||
import std.regex;
|
||||
import std.stdio, std.string, std.uni, std.uri;
|
||||
import core.time, core.thread;
|
||||
import config, itemdb, onedrive, selective, upload, util;
|
||||
static import log;
|
||||
|
||||
|
@ -650,7 +651,31 @@ final class SyncEngine
|
|||
write("Downloading file ", path, " ... ");
|
||||
JSONValue fileSizeDetails = onedrive.getFileSize(item.driveId, item.id);
|
||||
auto fileSize = fileSizeDetails["size"].integer;
|
||||
onedrive.downloadById(item.driveId, item.id, path, fileSize);
|
||||
try {
|
||||
onedrive.downloadById(item.driveId, item.id, path, fileSize);
|
||||
} catch (OneDriveException e) {
|
||||
if (e.httpStatusCode == 429) {
|
||||
// HTTP request returned status code 429 (Too Many Requests)
|
||||
// https://github.com/abraunegg/onedrive/issues/133
|
||||
// Back off & retry with incremental delay
|
||||
int retryCount = 10;
|
||||
int retryAttempts = 1;
|
||||
int backoffInterval = 2;
|
||||
while (retryAttempts < retryCount){
|
||||
Thread.sleep(dur!"seconds"(retryAttempts*backoffInterval));
|
||||
try {
|
||||
onedrive.downloadById(item.driveId, item.id, path, fileSize);
|
||||
// successful download
|
||||
retryAttempts = retryCount;
|
||||
} catch (OneDriveException e) {
|
||||
if (e.httpStatusCode == 429) {
|
||||
// Increment & loop around
|
||||
retryAttempts++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
writeln("done.");
|
||||
log.fileOnly("Downloading file ", path, " ... done.");
|
||||
setTimes(path, item.mtime, item.mtime);
|
||||
|
|
Loading…
Reference in a new issue