[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.
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
@@ -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_)
|
||||
|
||||
20
src/mbgl/util/chrono.cpp
Normal file
20
src/mbgl/util/chrono.cpp
Normal file
@@ -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);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user