From 96d3e6849854b70af10153fff555c2b10e306bea Mon Sep 17 00:00:00 2001 From: abraunegg Date: Mon, 4 Jan 2021 08:28:34 +1100 Subject: [PATCH] Add new config option to rate limit connection to OneDrive (#1210) * Add new config option to rate limit connection to OneDrive --- config | 3 ++- docs/USAGE.md | 22 ++++++++++++++++++++++ src/config.d | 2 ++ src/onedrive.d | 27 ++++++++++++++++++++++++--- 4 files changed, 50 insertions(+), 4 deletions(-) diff --git a/config b/config index 3b5f9d15..7ba34365 100644 --- a/config +++ b/config @@ -41,4 +41,5 @@ # azure_tenant_id = "common" # sync_business_shared_folders = "false" # sync_dir_permissions = "700" -# sync_file_permissions = "600" \ No newline at end of file +# sync_file_permissions = "600" +# rate_limit = "131072" \ No newline at end of file diff --git a/docs/USAGE.md b/docs/USAGE.md index 0604612a..ea4d0da9 100644 --- a/docs/USAGE.md +++ b/docs/USAGE.md @@ -302,6 +302,7 @@ The default configuration file is listed below: # sync_business_shared_folders = "false" # sync_dir_permissions = "700" # sync_file_permissions = "600" +# rate_limit = "131072" ``` @@ -541,6 +542,27 @@ check_nosync = "true" # disable_notifications = "false" ``` +### How to 'rate limit' the application to control bandwidth consumed for upload & download operations +To minimise the Internet bandwidth for upload and download operations, you can configure the 'rate_limit' option within the config file. + +Example valid values for this are as follows: +* 131072 = 128 KB/s - minimum for basic application operations to prevent timeouts +* 262144 = 256 KB/s +* 524288 = 512 KB/s +* 1048576 = 1 MB/s +* 10485760 = 10 MB/s +* 104857600 = 100 MB/s + +Example: +```text +# sync_business_shared_folders = "false" +# sync_dir_permissions = "700" +# sync_file_permissions = "600" +rate_limit = "131072" +``` + +**Note:** A number greater than '131072' is a valid value, with '104857600' being tested as an upper limit. + ### Shared folders (OneDrive Personal) Folders shared with you can be synced by adding them to your OneDrive. To do that open your Onedrive, go to the Shared files list, right click on the folder you want to sync and then click on "Add to my OneDrive". diff --git a/src/config.d b/src/config.d index 7dbefc5f..cd8c8caf 100644 --- a/src/config.d +++ b/src/config.d @@ -116,6 +116,8 @@ final class Config longValues["sync_dir_permissions"] = defaultDirectoryPermissionMode; // Configure the default file permission attributes for newly created file longValues["sync_file_permissions"] = defaultFilePermissionMode; + // Configure download / upload rate limits + longValues["rate_limit"] = 0; // DEVELOPER OPTIONS // display_memory = true | false diff --git a/src/onedrive.d b/src/onedrive.d index e5e5b815..d66d3cdb 100644 --- a/src/onedrive.d +++ b/src/onedrive.d @@ -119,7 +119,7 @@ final class OneDriveApi // Timeout for connecting http.connectTimeout = (dur!"seconds"(10)); // with the following settings we force - // - if there is no data flow for 5min, abort + // - if there is no data flow for 10min, abort // - if the download time for one item exceeds 1h, abort // // timeout for activity on connection @@ -128,7 +128,7 @@ final class OneDriveApi // It contains the time in number seconds that the // transfer speed should be below the CURLOPT_LOW_SPEED_LIMIT // for the library to consider it too slow and abort. - http.dataTimeout = (dur!"seconds"(300)); + http.dataTimeout = (dur!"seconds"(600)); // maximum time an operation is allowed to take // This includes dns resolution, connecting, data transfer, etc. http.operationTimeout = (dur!"seconds"(3600)); @@ -329,6 +329,27 @@ final class OneDriveApi http.handle.set(CurlOption.http_version,2); } + // Configure upload / download rate limits if configured + long userRateLimit = cfg.getValueLong("rate_limit"); + // 131072 = 128 KB/s - minimum for basic application operations to prevent timeouts + // A 0 value means rate is unlimited, and is the curl default + + if (userRateLimit > 0) { + // User configured rate limit + writeln("User Configured Rate Limit: ", userRateLimit); + + // If user provided rate limit is < 131072, flag that this is too low, setting to the minimum of 131072 + if (userRateLimit < 131072) { + // user provided limit too low + log.log("WARNING: User configured rate limit too low for normal application processing and preventing application timeouts. Overriding to default minimum of 131072 (128KB/s)"); + userRateLimit = 131072; + } + + // set rate limit + http.handle.set(CurlOption.max_send_speed_large,userRateLimit); + http.handle.set(CurlOption.max_recv_speed_large,userRateLimit); + } + // Do we set the dryRun handlers? if (cfg.getValueBool("dry_run")) { .dryRun = true; @@ -337,7 +358,7 @@ final class OneDriveApi } } } - + // Shutdown OneDrive HTTP construct void shutdown() {