From ed19b4076e0ec9252202086e65ffcc42510b6cdd Mon Sep 17 00:00:00 2001 From: Fabio Alessandrelli Date: Sun, 24 Nov 2019 13:20:24 +0100 Subject: [PATCH] Add download_chunk_size property to HTTPRequest. This allows setting the `read_chunk_size` of the internal HTTPClient. This is important to reduce the allocation overhead and number of file writes when downloading large files, allowing for better download speed. --- core/io/http_client.cpp | 6 ++++++ core/io/http_client.h | 1 + doc/classes/HTTPClient.xml | 12 +++--------- doc/classes/HTTPRequest.xml | 4 ++++ platform/javascript/http_client_javascript.cpp | 4 ++++ scene/main/http_request.cpp | 16 ++++++++++++++++ scene/main/http_request.h | 3 +++ 7 files changed, 37 insertions(+), 9 deletions(-) diff --git a/core/io/http_client.cpp b/core/io/http_client.cpp index 170bef4430..382f900d59 100644 --- a/core/io/http_client.cpp +++ b/core/io/http_client.cpp @@ -713,6 +713,10 @@ void HTTPClient::set_read_chunk_size(int p_size) { read_chunk_size = p_size; } +int HTTPClient::get_read_chunk_size() const { + return read_chunk_size; +} + HTTPClient::HTTPClient() { tcp_connection.instance(); @@ -816,6 +820,7 @@ void HTTPClient::_bind_methods() { ClassDB::bind_method(D_METHOD("get_response_body_length"), &HTTPClient::get_response_body_length); ClassDB::bind_method(D_METHOD("read_response_body_chunk"), &HTTPClient::read_response_body_chunk); ClassDB::bind_method(D_METHOD("set_read_chunk_size", "bytes"), &HTTPClient::set_read_chunk_size); + ClassDB::bind_method(D_METHOD("get_read_chunk_size"), &HTTPClient::get_read_chunk_size); ClassDB::bind_method(D_METHOD("set_blocking_mode", "enabled"), &HTTPClient::set_blocking_mode); ClassDB::bind_method(D_METHOD("is_blocking_mode_enabled"), &HTTPClient::is_blocking_mode_enabled); @@ -827,6 +832,7 @@ void HTTPClient::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::BOOL, "blocking_mode_enabled"), "set_blocking_mode", "is_blocking_mode_enabled"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "connection", PROPERTY_HINT_RESOURCE_TYPE, "StreamPeer", 0), "set_connection", "get_connection"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "read_chunk_size", PROPERTY_HINT_RANGE, "256,16777216"), "set_read_chunk_size", "get_read_chunk_size"); BIND_ENUM_CONSTANT(METHOD_GET); BIND_ENUM_CONSTANT(METHOD_HEAD); diff --git a/core/io/http_client.h b/core/io/http_client.h index 85ee1959a2..ce7fe0491b 100644 --- a/core/io/http_client.h +++ b/core/io/http_client.h @@ -220,6 +220,7 @@ public: bool is_blocking_mode_enabled() const; void set_read_chunk_size(int p_size); + int get_read_chunk_size() const; Error poll(); diff --git a/doc/classes/HTTPClient.xml b/doc/classes/HTTPClient.xml index 52e4b94051..3347eeafa7 100644 --- a/doc/classes/HTTPClient.xml +++ b/doc/classes/HTTPClient.xml @@ -170,15 +170,6 @@ Sends the body data raw, as a byte array and does not encode it in any way. - - - - - - - Sets the size of the buffer used and maximum bytes to read per iteration. See [method read_response_body_chunk]. - - @@ -187,6 +178,9 @@ The connection to use for this client. + + The size of the buffer used and maximum bytes to read per iteration. See [method read_response_body_chunk]. + diff --git a/doc/classes/HTTPRequest.xml b/doc/classes/HTTPRequest.xml index d0e8a5972f..98ba08e6a2 100644 --- a/doc/classes/HTTPRequest.xml +++ b/doc/classes/HTTPRequest.xml @@ -93,6 +93,10 @@ Maximum allowed size for response bodies. + + The size of the buffer used and maximum bytes to read per iteration. See [member HTTPClient.read_chunk_size]. + Set this to a higher value (e.g. 65536 for 64 KiB) when downloading large files to achieve better speeds at the cost of memory. + The file to download into. Will output any received file into it. diff --git a/platform/javascript/http_client_javascript.cpp b/platform/javascript/http_client_javascript.cpp index e6e933811f..1f3f2ed53c 100644 --- a/platform/javascript/http_client_javascript.cpp +++ b/platform/javascript/http_client_javascript.cpp @@ -211,6 +211,10 @@ void HTTPClient::set_read_chunk_size(int p_size) { read_limit = p_size; } +int HTTPClient::get_read_chunk_size() const { + return read_limit; +} + Error HTTPClient::poll() { switch (status) { diff --git a/scene/main/http_request.cpp b/scene/main/http_request.cpp index 6c922adbd2..0ae330b2ed 100644 --- a/scene/main/http_request.cpp +++ b/scene/main/http_request.cpp @@ -457,6 +457,18 @@ String HTTPRequest::get_download_file() const { return download_to_file; } + +void HTTPRequest::set_download_chunk_size(int p_chunk_size) { + + ERR_FAIL_COND(get_http_client_status() != HTTPClient::STATUS_DISCONNECTED); + + client->set_read_chunk_size(p_chunk_size); +} + +int HTTPRequest::get_download_chunk_size() const { + return client->get_read_chunk_size(); +} + HTTPClient::Status HTTPRequest::get_http_client_status() const { return client->get_status(); } @@ -524,9 +536,13 @@ void HTTPRequest::_bind_methods() { ClassDB::bind_method(D_METHOD("set_timeout", "timeout"), &HTTPRequest::set_timeout); ClassDB::bind_method(D_METHOD("get_timeout"), &HTTPRequest::get_timeout); + ClassDB::bind_method(D_METHOD("set_download_chunk_size"), &HTTPRequest::set_download_chunk_size); + ClassDB::bind_method(D_METHOD("get_download_chunk_size"), &HTTPRequest::get_download_chunk_size); + ClassDB::bind_method(D_METHOD("_timeout"), &HTTPRequest::_timeout); ADD_PROPERTY(PropertyInfo(Variant::STRING, "download_file", PROPERTY_HINT_FILE), "set_download_file", "get_download_file"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "download_chunk_size", PROPERTY_HINT_RANGE, "256,16777216"), "set_download_chunk_size", "get_download_chunk_size"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_threads"), "set_use_threads", "is_using_threads"); ADD_PROPERTY(PropertyInfo(Variant::INT, "body_size_limit", PROPERTY_HINT_RANGE, "-1,2000000000"), "set_body_size_limit", "get_body_size_limit"); ADD_PROPERTY(PropertyInfo(Variant::INT, "max_redirects", PROPERTY_HINT_RANGE, "-1,64"), "set_max_redirects", "get_max_redirects"); diff --git a/scene/main/http_request.h b/scene/main/http_request.h index f1f91235a6..fa01172d9f 100644 --- a/scene/main/http_request.h +++ b/scene/main/http_request.h @@ -126,6 +126,9 @@ public: void set_download_file(const String &p_file); String get_download_file() const; + void set_download_chunk_size(int p_chunk_size); + int get_download_chunk_size() const; + void set_body_size_limit(int p_bytes); int get_body_size_limit() const;