nghttp: move ownership of ContinueTimer to Request

Each Request now owns its own (optional) ContinueTimer for
Expect/Continue handshakes. This removes the need for
shared_ptr/weak_ptr logic.
This commit is contained in:
Jacob Champion 2016-03-29 13:11:27 -07:00
parent aa64e7ad3c
commit 4bed7854b5
2 changed files with 17 additions and 29 deletions

View File

@ -325,9 +325,9 @@ void continue_timeout_cb(struct ev_loop *loop, ev_timer *w, int revents) {
} // namespace } // namespace
ContinueTimer::ContinueTimer(struct ev_loop *loop, Request *req) ContinueTimer::ContinueTimer(struct ev_loop *loop, Request *req)
: loop(loop), : loop(loop) {
req(req) {
ev_timer_init(&timer, continue_timeout_cb, 1., 0.); ev_timer_init(&timer, continue_timeout_cb, 1., 0.);
timer.data = req;
} }
ContinueTimer::~ContinueTimer() { ContinueTimer::~ContinueTimer() {
@ -335,7 +335,6 @@ ContinueTimer::~ContinueTimer() {
} }
void ContinueTimer::start() { void ContinueTimer::start() {
timer.data = req;
ev_timer_start(loop, &timer); ev_timer_start(loop, &timer);
} }
@ -483,10 +482,8 @@ int submit_request(HttpClient *client, const Headers &headers, Request *req) {
req->req_nva = std::move(build_headers); req->req_nva = std::move(build_headers);
if (expect_continue) { if (expect_continue) {
auto timer = std::make_shared<ContinueTimer>(client->loop, req); auto timer = make_unique<ContinueTimer>(client->loop, req);
req->continue_timer = std::move(timer);
req->continue_timer = timer;
client->continue_timers.push_back(timer);
} }
return 0; return 0;
@ -689,7 +686,9 @@ int HttpClient::initiate_connection() {
void HttpClient::disconnect() { void HttpClient::disconnect() {
state = ClientState::IDLE; state = ClientState::IDLE;
continue_timers.clear(); for (auto req = std::begin(reqvec); req != std::end(reqvec); ++req) {
(*req)->continue_timer->stop();
}
ev_timer_stop(loop, &settings_timer); ev_timer_stop(loop, &settings_timer);
@ -1695,12 +1694,9 @@ void check_response_header(nghttp2_session *session, Request *req) {
} }
if (req->status / 100 == 1) { if (req->status / 100 == 1) {
if (req->status == 100) { if (req->continue_timer && (req->status == 100)) {
// If the request is waiting for a 100 Continue, complete the handshake. // If the request is waiting for a 100 Continue, complete the handshake.
std::shared_ptr<ContinueTimer> timer = req->continue_timer.lock(); req->continue_timer->dispatch_continue();
if (timer) {
timer->dispatch_continue();
}
} }
req->expect_final_response = true; req->expect_final_response = true;
@ -1708,12 +1704,9 @@ void check_response_header(nghttp2_session *session, Request *req) {
req->res_nva.clear(); req->res_nva.clear();
http2::init_hdidx(req->res_hdidx); http2::init_hdidx(req->res_hdidx);
return; return;
} else { } else if (req->continue_timer) {
// A final response stops any pending Expect/Continue handshake. // A final response stops any pending Expect/Continue handshake.
std::shared_ptr<ContinueTimer> timer = req->continue_timer.lock(); req->continue_timer->stop();
if (timer) {
timer->stop();
}
} }
if (gzip) { if (gzip) {
@ -2008,9 +2001,8 @@ int on_frame_send_callback(nghttp2_session *session,
} }
// If this request is using Expect/Continue, start its ContinueTimer. // If this request is using Expect/Continue, start its ContinueTimer.
std::shared_ptr<ContinueTimer> timer = req->continue_timer.lock(); if (req->continue_timer) {
if (timer) { req->continue_timer->start();
timer->start();
} }
return 0; return 0;
@ -2051,9 +2043,8 @@ int on_stream_close_callback(nghttp2_session *session, int32_t stream_id,
} }
// If this request is using Expect/Continue, stop its ContinueTimer. // If this request is using Expect/Continue, stop its ContinueTimer.
std::shared_ptr<ContinueTimer> timer = req->continue_timer.lock(); if (req->continue_timer) {
if (timer) { req->continue_timer->stop();
timer->stop();
} }
update_html_parser(client, req, nullptr, 0, 1); update_html_parser(client, req, nullptr, 0, 1);

View File

@ -124,7 +124,6 @@ struct ContinueTimer {
void dispatch_continue(); void dispatch_continue();
struct ev_loop *loop; struct ev_loop *loop;
Request *req;
ev_timer timer; ev_timer timer;
}; };
@ -175,8 +174,8 @@ struct Request {
// used for incoming PUSH_PROMISE // used for incoming PUSH_PROMISE
http2::HeaderIndex req_hdidx; http2::HeaderIndex req_hdidx;
bool expect_final_response; bool expect_final_response;
// only alive if this request is using Expect/Continue // only assigned if this request is using Expect/Continue
std::weak_ptr<ContinueTimer> continue_timer; std::unique_ptr<ContinueTimer> continue_timer;
}; };
struct SessionTiming { struct SessionTiming {
@ -285,8 +284,6 @@ struct HttpClient {
Buffer<64_k> wb; Buffer<64_k> wb;
// SETTINGS payload sent as token68 in HTTP Upgrade // SETTINGS payload sent as token68 in HTTP Upgrade
std::array<uint8_t, 128> settings_payload; std::array<uint8_t, 128> settings_payload;
// List of timers for outstanding expect/continue handshakes
std::vector<std::shared_ptr<ContinueTimer>> continue_timers;
enum { ERR_CONNECT_FAIL = -100 }; enum { ERR_CONNECT_FAIL = -100 };
}; };