diff options
author | Bruno de Oliveira Abinader <bruno@mapbox.com> | 2015-11-25 13:30:32 +0200 |
---|---|---|
committer | Bruno de Oliveira Abinader <bruno@mapbox.com> | 2015-11-27 17:47:02 +0200 |
commit | 31fd2822f516337f084f85db7e369d0633113b73 (patch) | |
tree | 9ad07f1bc0b3924f225ecfc628955bbd683e1d99 /src | |
parent | 4475f5486000bab8f1121f5cec6091bc04f165f1 (diff) | |
download | qtlocation-mapboxgl-31fd2822f516337f084f85db7e369d0633113b73.tar.gz |
[core] Replace time_t with std::chrono::seconds
Added aliases for std::chrono typedefs (eg. 'Seconds' for
std::chrono::seconds). These aliases are used together with templated
helper functions to replace time_t with std::chrono::seconds for most
cases, in particular for 'modified' and 'expires' values in Response.
Diffstat (limited to 'src')
-rw-r--r-- | src/mbgl/storage/default_file_source.cpp | 31 | ||||
-rw-r--r-- | src/mbgl/storage/default_file_source_impl.hpp | 3 | ||||
-rw-r--r-- | src/mbgl/storage/http_request_base.cpp | 9 | ||||
-rw-r--r-- | src/mbgl/storage/http_request_base.hpp | 3 | ||||
-rw-r--r-- | src/mbgl/storage/response.cpp | 5 | ||||
-rw-r--r-- | src/mbgl/util/chrono.cpp | 20 |
6 files changed, 43 insertions, 28 deletions
diff --git a/src/mbgl/storage/default_file_source.cpp b/src/mbgl/storage/default_file_source.cpp index dd8f8370ec..9e2289102e 100644 --- a/src/mbgl/storage/default_file_source.cpp +++ b/src/mbgl/storage/default_file_source.cpp @@ -231,17 +231,17 @@ void DefaultFileSource::Impl::reschedule(DefaultFileRequestImpl& request) { return; } - const auto timeout = request.getRetryTimeout(); + const Seconds timeout = request.getRetryTimeout(); - if (timeout == 0) { + if (timeout == Seconds::zero()) { update(request); - } else if (timeout > 0) { + } else if (timeout > Seconds::zero()) { if (!request.timerRequest) { request.timerRequest = std::make_unique<uv::timer>(util::RunLoop::getLoop()); } // timeout is in seconds, but the timer takes milliseconds. - request.timerRequest->start(1000 * timeout, 0, [this, &request] { + request.timerRequest->start(asMilliseconds(timeout).count(), 0, [this, &request] { assert(!request.realRequest); startRealRequest(request); }); @@ -298,14 +298,16 @@ const std::shared_ptr<const Response>& DefaultFileRequestImpl::getResponse() con return response; } -int64_t DefaultFileRequestImpl::getRetryTimeout() const { +Seconds DefaultFileRequestImpl::getRetryTimeout() const { + Seconds timeout = Seconds::zero(); + if (!response) { // If we don't have a response, we should retry immediately. - return 0; + return timeout; } // A value < 0 means that we should not retry. - int64_t timeout = -1; + timeout = Seconds(-1); if (response->error) { assert(failedRequests > 0); @@ -314,14 +316,14 @@ int64_t DefaultFileRequestImpl::getRetryTimeout() const { // Retry immediately, unless we have a certain number of attempts already const int graceRetries = 3; if (failedRequests <= graceRetries) { - timeout = 1; + timeout = Seconds(1); } else { - timeout = 1 << std::min(failedRequests - graceRetries, 31); + timeout = Seconds(1 << std::min(failedRequests - graceRetries, 31)); } } break; case Response::Error::Reason::Connection: { // Exponential backoff - timeout = 1 << std::min(failedRequests - 1, 31); + timeout = Seconds(1 << std::min(failedRequests - 1, 31)); } break; default: // Do not retry due to error. @@ -330,14 +332,11 @@ int64_t DefaultFileRequestImpl::getRetryTimeout() const { } // Check to see if this response expires earlier than a potential error retry. - if (response->expires > 0) { - const int64_t expires = - response->expires - - std::chrono::duration_cast<std::chrono::seconds>(SystemClock::now().time_since_epoch()) - .count(); + if (response->expires > Seconds::zero()) { + const Seconds secsToExpire = response->expires - toSeconds(SystemClock::now()); // Only update the timeout if we don't have one yet, and only if the new timeout is shorter // than the previous one. - timeout = timeout < 0 ? expires : std::min(timeout, std::max<int64_t>(0, expires)); + timeout = timeout < Seconds::zero() ? secsToExpire: std::min(timeout, std::max(Seconds::zero(), secsToExpire)); } return timeout; diff --git a/src/mbgl/storage/default_file_source_impl.hpp b/src/mbgl/storage/default_file_source_impl.hpp index c063a7d1f8..771274b7ad 100644 --- a/src/mbgl/storage/default_file_source_impl.hpp +++ b/src/mbgl/storage/default_file_source_impl.hpp @@ -5,6 +5,7 @@ #include <mbgl/storage/asset_context_base.hpp> #include <mbgl/storage/http_context_base.hpp> #include <mbgl/util/noncopyable.hpp> +#include <mbgl/util/chrono.hpp> #include <set> #include <unordered_map> @@ -57,7 +58,7 @@ public: // Returns the seconds we have to wait until we need to redo this request. A value of 0 // means that we need to redo it immediately, and a negative value means that we're not setting // a timeout at all. - int64_t getRetryTimeout() const; + Seconds getRetryTimeout() const; // Checks the currently stored response and replaces it with an idential one, except with the // stale flag set, if the response is expired. diff --git a/src/mbgl/storage/http_request_base.cpp b/src/mbgl/storage/http_request_base.cpp index f9e40c4f2b..a1bb71714d 100644 --- a/src/mbgl/storage/http_request_base.cpp +++ b/src/mbgl/storage/http_request_base.cpp @@ -5,18 +5,15 @@ namespace mbgl { -int64_t HTTPRequestBase::parseCacheControl(const char *value) { +Seconds HTTPRequestBase::parseCacheControl(const char *value) { if (value) { const auto cacheControl = http::CacheControl::parse(value); - if (cacheControl.maxAge) { - return std::chrono::duration_cast<std::chrono::seconds>( - std::chrono::system_clock::now().time_since_epoch()).count() + - *cacheControl.maxAge; + return toSeconds(SystemClock::now()) + Seconds(*cacheControl.maxAge); } } - return 0; + return Seconds::zero(); } } diff --git a/src/mbgl/storage/http_request_base.hpp b/src/mbgl/storage/http_request_base.hpp index 4c296050c7..eeefdf28d4 100644 --- a/src/mbgl/storage/http_request_base.hpp +++ b/src/mbgl/storage/http_request_base.hpp @@ -2,6 +2,7 @@ #define MBGL_STORAGE_HTTP_REQUEST_BASE #include <mbgl/storage/request_base.hpp> +#include <mbgl/util/chrono.hpp> namespace mbgl { @@ -16,7 +17,7 @@ public: virtual void cancel() override { cancelled = true; }; protected: - static int64_t parseCacheControl(const char *value); + static Seconds parseCacheControl(const char *value); bool cancelled; }; diff --git a/src/mbgl/storage/response.cpp b/src/mbgl/storage/response.cpp index 1dcd397b62..4b1125c110 100644 --- a/src/mbgl/storage/response.cpp +++ b/src/mbgl/storage/response.cpp @@ -18,11 +18,8 @@ Response& Response::operator=(const Response& res) { } bool Response::isExpired() const { - const int64_t now = - std::chrono::duration_cast<std::chrono::seconds>(SystemClock::now().time_since_epoch()) - .count(); // Note: expires == 0 also counts as expired! - return expires <= now; + return SystemTimePoint(expires) <= SystemClock::now(); } Response::Error::Error(Reason reason_, const std::string& message_) diff --git a/src/mbgl/util/chrono.cpp b/src/mbgl/util/chrono.cpp new file mode 100644 index 0000000000..4102092a90 --- /dev/null +++ b/src/mbgl/util/chrono.cpp @@ -0,0 +1,20 @@ +#include <mbgl/util/chrono.hpp> + +namespace mbgl { + +template Duration toDuration<Clock, Duration>(TimePoint); +template SystemDuration toDuration<SystemClock, SystemDuration>(SystemTimePoint); + +template Seconds asSeconds<Duration>(Duration); +template Seconds asSeconds<Milliseconds>(Milliseconds); + +template Seconds toSeconds<Clock, Duration>(TimePoint); +template Seconds toSeconds<SystemClock, SystemDuration>(SystemTimePoint); + +template Milliseconds asMilliseconds<Duration>(Duration); +template Milliseconds asMilliseconds<Seconds>(Seconds); + +template Milliseconds toMilliseconds<Clock, Duration>(TimePoint); +template Milliseconds toMilliseconds<SystemClock, SystemDuration>(SystemTimePoint); + +} |