summaryrefslogtreecommitdiff
path: root/src/mbgl/util/http_timeout.cpp
diff options
context:
space:
mode:
authorIvo van Dongen <info@ivovandongen.nl>2016-08-31 16:43:33 +0200
committerIvo van Dongen <info@ivovandongen.nl>2016-09-13 14:21:37 +0200
commit290bd07a92d7103c67f606c1423785069fc9b776 (patch)
tree00fa0a654830f3cf239932f266e311cab6ebeb59 /src/mbgl/util/http_timeout.cpp
parentfdaf26b2c02afa6042876962f92b1eaf0ada19bb (diff)
downloadqtlocation-mapboxgl-290bd07a92d7103c67f606c1423785069fc9b776.tar.gz
[core] OnlineFileSource - rate limit
Diffstat (limited to 'src/mbgl/util/http_timeout.cpp')
-rw-r--r--src/mbgl/util/http_timeout.cpp40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/mbgl/util/http_timeout.cpp b/src/mbgl/util/http_timeout.cpp
new file mode 100644
index 0000000000..ded0128ac9
--- /dev/null
+++ b/src/mbgl/util/http_timeout.cpp
@@ -0,0 +1,40 @@
+#include <mbgl/util/http_timeout.hpp>
+#include <mbgl/util/constants.hpp>
+
+namespace mbgl {
+namespace http {
+
+Duration errorRetryTimeout(Response::Error::Reason failedRequestReason, uint32_t failedRequests, optional<Timestamp> retryAfter) {
+
+ if (failedRequestReason == Response::Error::Reason::Server) {
+ // Retry after one second three times, then start exponential backoff.
+ return Seconds(failedRequests <= 3 ? 1 : 1u << std::min(failedRequests - 3, 31u));
+ } else if (failedRequestReason == Response::Error::Reason::Connection) {
+ // Immediate exponential backoff.
+ assert(failedRequests > 0);
+ return Seconds(1u << std::min(failedRequests - 1, 31u));
+ } else if (failedRequestReason == Response::Error::Reason::RateLimit) {
+ if (retryAfter) {
+ return *retryAfter - util::now();
+ } else {
+ //Default
+ return Seconds(util::DEFAULT_RATE_LIMIT_TIMEOUT);
+ }
+ } else {
+ // No error, or not an error that triggers retries.
+ return Duration::max();
+ }
+}
+
+Duration expirationTimeout(optional<Timestamp> expires, uint32_t expiredRequests) {
+ if (expiredRequests) {
+ return Seconds(1u << std::min(expiredRequests - 1, 31u));
+ } else if (expires) {
+ return std::max(Seconds::zero(), *expires - util::now());
+ } else {
+ return Duration::max();
+ }
+}
+
+} // namespace http
+} // namespace mbgl