Merge branch '1368-http-request-crash'

This commit is contained in:
John Firebaugh
2015-04-30 14:18:14 -04:00
3 changed files with 81 additions and 79 deletions

View File

@@ -58,23 +58,20 @@ public:
void retry() override;
private:
void cancelTimer();
void start();
void handleResult(NSData *data, NSURLResponse *res, NSError *error);
void handleResponse();
void retry(uint64_t timeout);
static void restart(uv_timer_t *timer, int);
HTTPNSURLContext *context = nullptr;
bool cancelled = false;
NSURLSessionDataTask *task = nullptr;
std::unique_ptr<Response> response;
const std::shared_ptr<const Response> existingResponse;
ResponseStatus status = ResponseStatus::PermanentError;
uv_async_t *async = nullptr;
uv::async async;
uv::timer timer;
int attempts = 0;
uv_timer_t *timer = nullptr;
enum : bool { PreemptImmediately, ExponentialBackoff } strategy = PreemptImmediately;
static const int maxAttempts = 4;
@@ -134,26 +131,17 @@ HTTPRequest::HTTPRequest(HTTPNSURLContext* context_, const Resource& resource_,
: RequestBase(resource_, callback_),
context(context_),
existingResponse(existingResponse_),
async(new uv_async_t) {
async(loop, [this] { handleResponse(); }),
timer(loop) {
context->addRequest(this);
async->data = this;
uv_async_init(loop, async, [](uv_async_t *as, int) {
auto impl = reinterpret_cast<HTTPRequest *>(as->data);
impl->handleResponse();
});
start();
}
HTTPRequest::~HTTPRequest() {
assert(!task);
assert(async);
// Stop the backoff timer to avoid re-triggering this request.
cancelTimer();
uv::close(async);
timer.stop();
context->removeRequest(this);
}
@@ -230,7 +218,7 @@ void HTTPRequest::cancel() {
cancelled = true;
// Stop the backoff timer to avoid re-triggering this request.
cancelTimer();
timer.stop();
if (task) {
[task cancel];
@@ -241,14 +229,6 @@ void HTTPRequest::cancel() {
}
}
void HTTPRequest::cancelTimer() {
if (timer) {
uv_timer_stop(timer);
uv::close(timer);
timer = nullptr;
}
}
int64_t parseCacheControl(const char *value) {
if (value) {
unsigned long long seconds = 0;
@@ -364,40 +344,26 @@ void HTTPRequest::handleResult(NSData *data, NSURLResponse *res, NSError *error)
response->message = "response class is not NSHTTPURLResponse";
}
uv_async_send(async);
async.send();
}
void HTTPRequest::retry(uint64_t timeout) {
response.reset();
assert(!timer);
timer = new uv_timer_t;
timer->data = this;
uv_timer_init(async->loop, timer);
uv_timer_start(timer, restart, timeout, 0);
timer.stop();
timer.start(timeout, 0, [this] { start(); });
}
void HTTPRequest::retry() {
// All batons get notified when the network status changed, but some of them
// might not actually wait for the network to become available again.
if (timer && strategy == PreemptImmediately) {
if (strategy == PreemptImmediately) {
// Triggers the timer upon the next event loop iteration.
uv_timer_stop(timer);
uv_timer_start(timer, restart, 0, 0);
timer.stop();
timer.start(0, 0, [this] { start(); });
}
}
void HTTPRequest::restart(uv_timer_t *timer, int) {
// Restart the request.
auto impl = reinterpret_cast<HTTPRequest *>(timer->data);
// Get rid of the timer.
impl->timer = nullptr;
uv::close(timer);
impl->start();
}
std::unique_ptr<HTTPContext> HTTPContext::createContext(uv_loop_t* loop) {
return util::make_unique<HTTPNSURLContext>(loop);
}

View File

@@ -3,13 +3,13 @@
#include <mbgl/util/noncopyable.hpp>
#include <mbgl/storage/file_cache.hpp>
#include <mbgl/storage/resource.hpp>
#include <memory>
#include <functional>
namespace mbgl {
struct Resource;
class Response;
class RequestBase : private util::noncopyable {
@@ -26,7 +26,7 @@ public:
virtual void retry() {};
protected:
const Resource& resource;
Resource resource;
Callback notify;
};

View File

@@ -28,13 +28,6 @@ UV_EXTERN void uv_key_set(uv_key_t* key, void* value);
namespace uv {
template <class T>
void close(std::unique_ptr<T> ptr) {
uv_close(reinterpret_cast<uv_handle_t*>(ptr.release()), [](uv_handle_t* handle) {
delete reinterpret_cast<T*>(handle);
});
}
class loop : public mbgl::util::noncopyable {
public:
inline loop() {
@@ -74,40 +67,50 @@ private:
uv_loop_t *l = nullptr;
};
class async : public mbgl::util::noncopyable {
template <class T>
class handle : public mbgl::util::noncopyable {
public:
inline handle() : t(reinterpret_cast<uv_handle_t*>(new T)) {
t->data = this;
}
inline ~handle() {
uv_close(t.release(), [](uv_handle_t* handle) {
delete reinterpret_cast<T*>(handle);
});
}
inline void ref() {
uv_ref(t.get());
}
inline void unref() {
uv_unref(t.get());
}
inline T* get() {
return reinterpret_cast<T*>(t.get());
}
private:
std::unique_ptr<uv_handle_t> t;
};
class async : public handle<uv_async_t> {
public:
inline async(uv_loop_t* loop, std::function<void ()> fn_)
: a(new uv_async_t)
, fn(fn_)
{
a->data = this;
if (uv_async_init(loop, a.get(), async_cb) != 0) {
: fn(fn_) {
if (uv_async_init(loop, get(), async_cb) != 0) {
throw std::runtime_error("failed to initialize async");
}
}
inline ~async() {
close(std::move(a));
}
inline void send() {
if (uv_async_send(a.get()) != 0) {
if (uv_async_send(get()) != 0) {
throw std::runtime_error("failed to async send");
}
}
inline void ref() {
uv_ref(reinterpret_cast<uv_handle_t*>(a.get()));
}
inline void unref() {
uv_unref(reinterpret_cast<uv_handle_t*>(a.get()));
}
inline uv_async_t* get() {
return a.get();
}
private:
#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10
static void async_cb(uv_async_t* a, int) {
@@ -117,7 +120,40 @@ private:
reinterpret_cast<async*>(a->data)->fn();
}
std::unique_ptr<uv_async_t> a;
std::function<void ()> fn;
};
class timer : public handle<uv_timer_t> {
public:
inline timer(uv_loop_t* loop) {
if (uv_timer_init(loop, get()) != 0) {
throw std::runtime_error("failed to initialize timer");
}
}
inline void start(uint64_t timeout, uint64_t repeat, std::function<void ()> fn_) {
fn = fn_;
if (uv_timer_start(get(), timer_cb, timeout, repeat) != 0) {
throw std::runtime_error("failed to start timer");
}
}
inline void stop() {
fn = nullptr;
if (uv_timer_stop(get()) != 0) {
throw std::runtime_error("failed to stop timer");
}
}
private:
#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10
static void timer_cb(uv_timer_t* t, int) {
#else
static void timer_cb(uv_timer_t* t) {
#endif
reinterpret_cast<timer*>(t->data)->fn();
}
std::function<void ()> fn;
};