From 6a7334b882a47ca193209f2012843e42aa3ed4e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Thu, 15 Oct 2015 21:30:21 +0200 Subject: [core] add support for stale responses We're now returning stale responses from cache. Those responses will have the `stale` flag set to true. Currently, all requesters in the core code discard stale responses, and cancel the request immediately after they got a non-stale response. --- src/mbgl/storage/default_file_source.cpp | 78 +++++++++++++++++++-------- src/mbgl/storage/default_file_source_impl.hpp | 2 + src/mbgl/storage/request.cpp | 1 - src/mbgl/storage/response.cpp | 12 +++++ 4 files changed, 70 insertions(+), 23 deletions(-) create mode 100644 src/mbgl/storage/response.cpp (limited to 'src/mbgl/storage') diff --git a/src/mbgl/storage/default_file_source.cpp b/src/mbgl/storage/default_file_source.cpp index 7b3cff8253..71c42f6220 100644 --- a/src/mbgl/storage/default_file_source.cpp +++ b/src/mbgl/storage/default_file_source.cpp @@ -8,7 +8,6 @@ #include #include -#include #include #include #include @@ -102,43 +101,79 @@ void DefaultFileSource::Impl::add(Request* req) { const Resource& resource = req->resource; DefaultFileRequest* request = find(resource); - if (request) { - request->observers.insert(req); - return; + if (!request) { + request = &pending.emplace(resource, resource).first->second; } - request = &pending.emplace(resource, resource).first->second; + // Add this request as an observer so that it'll get notified when something about this + // request changes. request->observers.insert(req); - if (cache) { - startCacheRequest(request); + update(request); + + if (request->response) { + // We've got a response, so send the (potentially stale) response to the requester. + req->notify(request->response); + } +} + +void DefaultFileSource::Impl::update(DefaultFileRequest* request) { + if (request->response) { + // We've at least obtained a cache value, potentially we also got a final response. + // The observers have been notified already; send what we have to the new one as well. + + // Before returning the existing response, make sure that it is still fresh. + if (!request->response->stale && request->response->isExpired()) { + // Create a new Response object with `stale = true`, but the same data, and + // replace the current request object we have. + // TODO: Make content shared_ptrs so we won't make copies of the content. + auto response = std::make_shared(*request->response); + response->stale = true; + request->response = response; + } + + if (request->response->stale && !request->realRequest) { + // We've returned a stale response; now make sure the requester also gets a fresh + // response eventually. It's possible that there's already a request in progress. + // Note that this will also trigger updates to all other existing listeners. + // Since we already have data, we're going to verify + startRealRequest(request, request->response); + } + } else if (!request->cacheRequest && !request->realRequest) { + // There is no request in progress, and we don't have a response yet. This means we'll have + // to start the request ourselves. + if (cache) { + startCacheRequest(request); + } else { + startRealRequest(request); + } } else { - startRealRequest(request); + // There is a request in progress. We just have to wait. } } void DefaultFileSource::Impl::startCacheRequest(DefaultFileRequest* request) { // Check the cache for existing data so that we can potentially // revalidate the information without having to redownload everything. - request->cacheRequest = cache->get(request->resource, [this, request](std::unique_ptr response) { - auto expired = [&response] { - const int64_t now = std::chrono::duration_cast( - SystemClock::now().time_since_epoch()).count(); - return response->expires <= now; - }; - - if (!response || expired()) { + request->cacheRequest = cache->get(request->resource, [this, request](std::shared_ptr response) { + request->cacheRequest = nullptr; + if (response) { + response->stale = response->isExpired(); + + // Notify in all cases; requestors can decide whether they want to use stale responses. + notify(request, response, FileCache::Hint::No); + } + + if (!response || response->stale) { // No response or stale cache. Run the real request. - startRealRequest(request, std::move(response)); - } else { - // The response is fresh. We're good to notify the caller. - notify(request, std::move(response), FileCache::Hint::No); + startRealRequest(request, response); } }); } void DefaultFileSource::Impl::startRealRequest(DefaultFileRequest* request, std::shared_ptr response) { auto callback = [request, this] (std::shared_ptr res, FileCache::Hint hint) { + request->realRequest = nullptr; notify(request, res, hint); }; @@ -181,6 +216,7 @@ void DefaultFileSource::Impl::notify(DefaultFileRequest* request, std::shared_pt assert(response); // Notify all observers. + request->response = response; for (auto req : request->observers) { req->notify(response); } @@ -189,8 +225,6 @@ void DefaultFileSource::Impl::notify(DefaultFileRequest* request, std::shared_pt // Store response in database cache->put(request->resource, response, hint); } - - pending.erase(request->resource); } } diff --git a/src/mbgl/storage/default_file_source_impl.hpp b/src/mbgl/storage/default_file_source_impl.hpp index bb28933798..980eedc118 100644 --- a/src/mbgl/storage/default_file_source_impl.hpp +++ b/src/mbgl/storage/default_file_source_impl.hpp @@ -15,6 +15,7 @@ class RequestBase; struct DefaultFileRequest { const Resource resource; std::set observers; + std::shared_ptr response; std::unique_ptr cacheRequest; RequestBase* realRequest = nullptr; @@ -39,6 +40,7 @@ public: private: DefaultFileRequest* find(const Resource&); + void update(DefaultFileRequest*); void startCacheRequest(DefaultFileRequest*); void startRealRequest(DefaultFileRequest*, std::shared_ptr = nullptr); void notify(DefaultFileRequest*, std::shared_ptr, FileCache::Hint); diff --git a/src/mbgl/storage/request.cpp b/src/mbgl/storage/request.cpp index f8ad555379..55913846cf 100644 --- a/src/mbgl/storage/request.cpp +++ b/src/mbgl/storage/request.cpp @@ -49,7 +49,6 @@ Request::~Request() = default; // Called in the FileSource thread. void Request::notify(const std::shared_ptr &response_) { - assert(!std::atomic_load(&response)); assert(response_); std::atomic_store(&response, response_); async->send(); diff --git a/src/mbgl/storage/response.cpp b/src/mbgl/storage/response.cpp new file mode 100644 index 0000000000..628a2a3b99 --- /dev/null +++ b/src/mbgl/storage/response.cpp @@ -0,0 +1,12 @@ +#include +#include + +namespace mbgl { + +bool Response::isExpired() const { + const int64_t now = std::chrono::duration_cast( + SystemClock::now().time_since_epoch()).count(); + return expires <= now; +} + +} // namespace mbgl -- cgit v1.2.1