This repository has been archived by the owner on Aug 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
online_file_source.cpp
346 lines (284 loc) · 10.7 KB
/
online_file_source.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#include <mbgl/storage/online_file_source.hpp>
#include <mbgl/storage/http_file_source.hpp>
#include <mbgl/storage/network_status.hpp>
#include <mbgl/storage/response.hpp>
#include <mbgl/platform/log.hpp>
#include <mbgl/util/constants.hpp>
#include <mbgl/util/thread.hpp>
#include <mbgl/util/mapbox.hpp>
#include <mbgl/util/exception.hpp>
#include <mbgl/util/chrono.hpp>
#include <mbgl/util/async_task.hpp>
#include <mbgl/util/noncopyable.hpp>
#include <mbgl/util/timer.hpp>
#include <algorithm>
#include <cassert>
#include <list>
#include <unordered_set>
#include <unordered_map>
namespace mbgl {
class OnlineFileRequest : public AsyncRequest {
public:
using Callback = std::function<void (Response)>;
OnlineFileRequest(Resource, Callback, OnlineFileSource::Impl&);
~OnlineFileRequest() override;
void networkIsReachableAgain();
void schedule(optional<Timestamp> expires);
void completed(Response);
OnlineFileSource::Impl& impl;
Resource resource;
std::unique_ptr<AsyncRequest> request;
util::Timer timer;
Callback callback;
// Counts the number of times a response was already expired when received. We're using
// this to add a delay when making a new request so we don't keep retrying immediately
// in case of a server serving expired tiles.
uint32_t expiredRequests = 0;
// Counts the number of subsequent failed requests. We're using this value for exponential
// backoff when retrying requests.
uint32_t failedRequests = 0;
Response::Error::Reason failedRequestReason = Response::Error::Reason::Success;
};
class OnlineFileSource::Impl {
public:
Impl() {
NetworkStatus::Subscribe(&reachability);
}
~Impl() {
NetworkStatus::Unsubscribe(&reachability);
}
void add(OnlineFileRequest* request) {
allRequests.insert(request);
}
void remove(OnlineFileRequest* request) {
allRequests.erase(request);
if (activeRequests.erase(request)) {
activatePendingRequest();
} else {
auto it = pendingRequestsMap.find(request);
if (it != pendingRequestsMap.end()) {
pendingRequestsList.erase(it->second);
pendingRequestsMap.erase(it);
}
}
}
void activateOrQueueRequest(OnlineFileRequest* request) {
assert(allRequests.find(request) != allRequests.end());
assert(activeRequests.find(request) == activeRequests.end());
assert(!request->request);
if (activeRequests.size() >= HTTPFileSource::maximumConcurrentRequests()) {
queueRequest(request);
} else {
activateRequest(request);
}
}
void queueRequest(OnlineFileRequest* request) {
auto it = pendingRequestsList.insert(pendingRequestsList.end(), request);
pendingRequestsMap.emplace(request, std::move(it));
}
void activateRequest(OnlineFileRequest* request) {
activeRequests.insert(request);
request->request = httpFileSource.request(request->resource, [=] (Response response) {
activeRequests.erase(request);
activatePendingRequest();
request->request.reset();
request->completed(response);
});
}
void activatePendingRequest() {
if (pendingRequestsList.empty()) {
return;
}
OnlineFileRequest* request = pendingRequestsList.front();
pendingRequestsList.pop_front();
pendingRequestsMap.erase(request);
activateRequest(request);
}
private:
void networkIsReachableAgain() {
for (auto& request : allRequests) {
request->networkIsReachableAgain();
}
}
/**
* The lifetime of a request is:
*
* 1. Waiting for timeout (revalidation or retry)
* 2. Pending (waiting for room in the active set)
* 3. Active (open network connection)
* 4. Back to #1
*
* Requests in any state are in `allRequests`. Requests in the pending state are in
* `pendingRequests`. Requests in the active state are in `activeRequests`.
*/
std::unordered_set<OnlineFileRequest*> allRequests;
std::list<OnlineFileRequest*> pendingRequestsList;
std::unordered_map<OnlineFileRequest*, std::list<OnlineFileRequest*>::iterator> pendingRequestsMap;
std::unordered_set<OnlineFileRequest*> activeRequests;
HTTPFileSource httpFileSource;
util::AsyncTask reachability { std::bind(&Impl::networkIsReachableAgain, this) };
};
OnlineFileSource::OnlineFileSource()
: impl(std::make_unique<Impl>()) {
}
OnlineFileSource::~OnlineFileSource() = default;
std::unique_ptr<AsyncRequest> OnlineFileSource::request(const Resource& resource, Callback callback) {
Resource res = resource;
switch (resource.kind) {
case Resource::Kind::Unknown:
break;
case Resource::Kind::Style:
res.url = mbgl::util::mapbox::normalizeStyleURL(resource.url, accessToken);
break;
case Resource::Kind::Source:
res.url = util::mapbox::normalizeSourceURL(resource.url, accessToken);
break;
case Resource::Kind::Glyphs:
res.url = util::mapbox::normalizeGlyphsURL(resource.url, accessToken);
break;
case Resource::Kind::SpriteImage:
case Resource::Kind::SpriteJSON:
res.url = util::mapbox::normalizeSpriteURL(resource.url, accessToken);
break;
case Resource::Kind::Tile:
res.url = util::mapbox::normalizeTileURL(resource.url, accessToken);
break;
}
return std::make_unique<OnlineFileRequest>(res, callback, *impl);
}
OnlineFileRequest::OnlineFileRequest(Resource resource_, Callback callback_, OnlineFileSource::Impl& impl_)
: impl(impl_),
resource(std::move(resource_)),
callback(std::move(callback_)) {
impl.add(this);
// Force an immediate first request if we don't have an expiration time.
if (resource.priorExpires) {
schedule(resource.priorExpires);
} else {
schedule(util::now());
}
}
OnlineFileRequest::~OnlineFileRequest() {
impl.remove(this);
}
static Duration errorRetryTimeout(Response::Error::Reason failedRequestReason, uint32_t failedRequests) {
if (failedRequestReason == Response::Error::Reason::Server) {
// Retry after one second three times, then start exponential backoff.
return Seconds(failedRequests <= 3 ? 1 : 1 << std::min(failedRequests - 3, 31u));
} else if (failedRequestReason == Response::Error::Reason::Connection) {
// Immediate exponential backoff.
assert(failedRequests > 0);
return Seconds(1 << std::min(failedRequests - 1, 31u));
} else {
// No error, or not an error that triggers retries.
return Duration::max();
}
}
static Duration expirationTimeout(optional<Timestamp> expires, uint32_t expiredRequests) {
if (expiredRequests) {
return Seconds(1 << std::min(expiredRequests - 1, 31u));
} else if (expires) {
return std::max(Seconds::zero(), *expires - util::now());
} else {
return Duration::max();
}
}
Timestamp interpolateExpiration(const Timestamp& current,
optional<Timestamp> prior,
bool& expired) {
auto now = util::now();
if (current > now) {
return current;
}
if (!bool(prior)) {
expired = true;
return current;
}
// Expiring date is going backwards,
// fallback to exponential backoff.
if (current < *prior) {
expired = true;
return current;
}
auto delta = current - *prior;
// Server is serving the same expired resource
// over and over, fallback to exponential backoff.
if (delta == Duration::zero()) {
expired = true;
return current;
}
// Assume that either the client or server clock is wrong and
// try to interpolate a valid expiration date (from the client POV)
// observing a minimum timeout.
return now + std::max<Seconds>(delta, util::CLOCK_SKEW_RETRY_TIMEOUT);
}
void OnlineFileRequest::schedule(optional<Timestamp> expires) {
if (request) {
// There's already a request in progress; don't start another one.
return;
}
// If we're not being asked for a forced refresh, calculate a timeout that depends on how many
// consecutive errors we've encountered, and on the expiration time, if present.
Duration timeout = std::min(errorRetryTimeout(failedRequestReason, failedRequests),
expirationTimeout(expires, expiredRequests));
if (timeout == Duration::max()) {
return;
}
// Emulate a Connection error when the Offline mode is forced with
// a really long timeout. The request will get re-triggered when
// the NetworkStatus is set back to Online.
if (NetworkStatus::Get() == NetworkStatus::Status::Offline) {
failedRequestReason = Response::Error::Reason::Connection;
failedRequests = 1;
timeout = Duration::max();
}
timer.start(timeout, Duration::zero(), [&] {
impl.activateOrQueueRequest(this);
});
}
void OnlineFileRequest::completed(Response response) {
// If we didn't get various caching headers in the response, continue using the
// previous values. Otherwise, update the previous values to the new values.
if (!response.modified) {
response.modified = resource.priorModified;
} else {
resource.priorModified = response.modified;
}
bool isExpired = false;
if (response.expires) {
auto prior = resource.priorExpires;
resource.priorExpires = response.expires;
response.expires = interpolateExpiration(*response.expires, prior, isExpired);
}
if (isExpired) {
expiredRequests++;
} else {
expiredRequests = 0;
}
if (!response.etag) {
response.etag = resource.priorEtag;
} else {
resource.priorEtag = response.etag;
}
if (response.error) {
failedRequests++;
failedRequestReason = response.error->reason;
} else {
failedRequests = 0;
failedRequestReason = Response::Error::Reason::Success;
}
schedule(response.expires);
// Calling the callback may result in `this` being deleted. It needs to be done last,
// and needs to make a local copy of the callback to ensure that it remains valid for
// the duration of the call.
auto callback_ = callback;
callback_(response);
}
void OnlineFileRequest::networkIsReachableAgain() {
// We need all requests to fail at least once before we are going to start retrying
// them, and we only immediately restart request that failed due to connection issues.
if (failedRequestReason == Response::Error::Reason::Connection) {
schedule(util::now());
}
}
} // namespace mbgl