summaryrefslogtreecommitdiff
path: root/src/storage/http_request.cpp
blob: ebb9a84823a0c68e274082de511353b59eaa427c (plain)
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
#include <mbgl/storage/http_request.hpp>
#include <mbgl/storage/sqlite_store.hpp>
#include <mbgl/storage/http_request_baton.hpp>

#include <uv.h>

#include <cassert>
#include <chrono>

namespace mbgl {

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdisabled-macro-expansion"
#pragma clang diagnostic ignored "-Wexit-time-destructors"
#pragma clang diagnostic ignored "-Wglobal-constructors"

struct CacheRequestBaton {
    HTTPRequest *request = nullptr;
    std::string path;
    util::ptr<SQLiteStore> store;
};

HTTPRequest::HTTPRequest(ResourceType type_, const std::string &path_, uv_loop_t *loop_, util::ptr<SQLiteStore> store_)
    : BaseRequest(path_), thread_id(std::this_thread::get_id()), loop(loop_), store(store_), type(type_) {
    if (store) {
        startCacheRequest();
    } else {
        startHTTPRequest(nullptr);
    }
}

void HTTPRequest::startCacheRequest() {
    assert(std::this_thread::get_id() == thread_id);

    cache_baton = new CacheRequestBaton;
    cache_baton->request = this;
    cache_baton->path = path;
    cache_baton->store = store;
    store->get(path, [](std::unique_ptr<Response> &&response_, void *ptr) {
        // Wrap in a unique_ptr, so it'll always get auto-destructed.
        std::unique_ptr<CacheRequestBaton> baton((CacheRequestBaton *)ptr);
        if (baton->request) {
            baton->request->cache_baton = nullptr;
            baton->request->handleCacheResponse(std::move(response_));
        }
    }, cache_baton);
}

void HTTPRequest::handleCacheResponse(std::unique_ptr<Response> &&res) {
    assert(std::this_thread::get_id() == thread_id);

    if (res) {
        // This entry was stored in the cache. Now determine if we need to revalidate.
        const int64_t now = std::chrono::duration_cast<std::chrono::seconds>(
                                std::chrono::system_clock::now().time_since_epoch()).count();
        if (res->expires > now) {
            response = std::move(res);
            notify();
            // Note: after calling notify(), the request object may cease to exist.
            // This HTTPRequest is completed.
            return;
        } else {
            // TODO: notify with preliminary results.
        }
    }

    startHTTPRequest(std::move(res));
}

void HTTPRequest::startHTTPRequest(std::unique_ptr<Response> &&res) {
    assert(std::this_thread::get_id() == thread_id);
    assert(!http_baton);

    http_baton = std::make_shared<HTTPRequestBaton>(path);
    http_baton->request = this;
    http_baton->async = new uv_async_t;
    http_baton->response = std::move(res);
    http_baton->async->data = new util::ptr<HTTPRequestBaton>(http_baton);

#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10
    uv_async_init(loop, http_baton->async, [](uv_async_t *async, int) {
#else
    uv_async_init(loop, http_baton->async, [](uv_async_t *async) {
#endif
        util::ptr<HTTPRequestBaton> &baton = *(util::ptr<HTTPRequestBaton> *)async->data;

        if (baton->request) {
            HTTPRequest *request = baton->request;
            request->http_baton.reset();
            baton->request = nullptr;
            request->handleHTTPResponse(baton->type, std::move(baton->response));
        }

        delete (util::ptr<HTTPRequestBaton> *)async->data;
        uv_close((uv_handle_t *)async, [](uv_handle_t *handle) {
            uv_async_t *async_handle = (uv_async_t *)handle;
            delete async_handle;
        });
    });
    attempts++;
    HTTPRequestBaton::start(http_baton);
}



void HTTPRequest::handleHTTPResponse(HTTPResponseType responseType, std::unique_ptr<Response> &&res) {
    assert(std::this_thread::get_id() == thread_id);
    assert(!http_baton);
    assert(!response);

    switch (responseType) {
        // This error was caused by a temporary error and it is likely that it will be resolved
        // immediately. We are going to try again right away. This is like the TemporaryError,
        // except that we will not perform exponential back-off.
        case HTTPResponseType::SingularError:
            if (attempts >= 4) {
                // Report as error after 4 attempts.
                response = std::move(res);
                notify();
            } else if (attempts >= 2) {
                // Switch to the back-off algorithm after the second failure.
                retryHTTPRequest(std::move(res), (1 << attempts) * 1000);
                return;
            } else {
                startHTTPRequest(std::move(res));
            }
            break;

        // This error might be resolved by waiting some time (e.g. server issues).
        // We are going to do an exponential back-off and will try again in a few seconds.
        case HTTPResponseType::TemporaryError:
            if (attempts >= 4) {
                // Report error back after it failed completely.
                response = std::move(res);
                notify();
            } else {
                retryHTTPRequest(std::move(res), (1 << attempts) * 1000);
            }
            break;

        // This error might be resolved once the network reachability status changes.
        // We are going to watch the network status for changes and will retry as soon as the
        // operating system notifies us of a network status change.
        case HTTPResponseType::ConnectionError:

            if (attempts >= 4) {
                // Report error back after it failed completely.
                response = std::move(res);
                notify();
            } else {
                // By default, we will retry every 60 seconds.
                retryHTTPRequest(std::move(res), 60000);
            }
            break;

        // The request was canceled programatically.
        case HTTPResponseType::Canceled:
            response.reset();
            notify();
            break;

        // This error probably won't be resolved by retrying anytime soon. We are giving up.
        case HTTPResponseType::PermanentError:
            response = std::move(res);
            notify();
            break;

        // The request returned data successfully. We retrieved and decoded the data successfully.
        case HTTPResponseType::Successful:
            if (store) {
                store->put(path, type, *res);
            }
            response = std::move(res);
            notify();
            break;

        // The request confirmed that the data wasn't changed. We already have the data.
        case HTTPResponseType::NotModified:
            if (store) {
                store->updateExpiration(path, res->expires);
            }
            response = std::move(res);
            notify();
            break;

        default:
            assert(!"Response wasn't set");
            break;
    }
}

using RetryBaton = std::pair<HTTPRequest *, std::unique_ptr<Response>>;

void HTTPRequest::retryHTTPRequest(std::unique_ptr<Response> &&res, uint64_t timeout) {
    assert(std::this_thread::get_id() == thread_id);
    assert(!backoff_timer);
    backoff_timer = new uv_timer_t();
    uv_timer_init(loop, backoff_timer);
    backoff_timer->data = new RetryBaton(this, std::move(res));

#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10
    uv_timer_start(backoff_timer, [](uv_timer_t *timer, int) {
#else
    uv_timer_start(backoff_timer, [](uv_timer_t *timer) {
#endif
        std::unique_ptr<RetryBaton> pair { static_cast<RetryBaton *>(timer->data) };
        pair->first->startHTTPRequest(std::move(pair->second));
        pair->first->backoff_timer = nullptr;
        uv_timer_stop(timer);
        uv_close((uv_handle_t *)timer, [](uv_handle_t *handle) { delete (uv_timer_t *)handle; });
    }, timeout, 0);
}

void HTTPRequest::removeHTTPBaton() {
    assert(std::this_thread::get_id() == thread_id);
    if (http_baton) {
        http_baton->request = nullptr;
        HTTPRequestBaton::stop(http_baton);
        http_baton.reset();
    }
}

void HTTPRequest::removeCacheBaton() {
    assert(std::this_thread::get_id() == thread_id);
    if (cache_baton) {
        // Make sre that this object doesn't accidentally get accessed when it is destructed before
        // the callback returned. They are being run in the same thread, so just setting it to
        // null is sufficient.
        // Note: We don't manually delete the CacheRequestBaton since it'll be deleted by the
        // callback.
        cache_baton->request = nullptr;
        cache_baton = nullptr;
    }
}

void HTTPRequest::removeBackoffTimer() {
    assert(std::this_thread::get_id() == thread_id);
    if (backoff_timer) {
        delete static_cast<RetryBaton *>(backoff_timer->data);
        uv_timer_stop(backoff_timer);
        uv_close((uv_handle_t *)backoff_timer, [](uv_handle_t *handle) { delete (uv_timer_t *)handle; });
        backoff_timer = nullptr;
    }
}

void HTTPRequest::retryImmediately() {
    assert(std::this_thread::get_id() == thread_id);
    if (!cache_baton && !http_baton) {
        if (backoff_timer) {
            // Retry immediately.
            uv_timer_stop(backoff_timer);
            std::unique_ptr<RetryBaton> pair { static_cast<RetryBaton *>(backoff_timer->data) };
            assert(pair->first == this);
            startHTTPRequest(std::move(pair->second));
            uv_close((uv_handle_t *)backoff_timer, [](uv_handle_t *handle) { delete (uv_timer_t *)handle; });
            backoff_timer = nullptr;
        } else {
            assert(!"We should always have a backoff_timer when there are no batons");
        }
    }
}

void HTTPRequest::cancel() {
    assert(std::this_thread::get_id() == thread_id);
    removeCacheBaton();
    removeHTTPBaton();
    removeBackoffTimer();
    notify();
}


HTTPRequest::~HTTPRequest() {
    assert(std::this_thread::get_id() == thread_id);
    cancel();
}

#pragma clang diagnostic pop

}