From 4424cf5b65b6dcf0ac618905401b70274f8ce0c4 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Fri, 20 Feb 2015 19:48:35 +0900 Subject: [PATCH] nghttpx: Fix 1 second delay in HTTP/2 backend connection --- src/shrpx_http2_session.cc | 53 ++++++++++++-------------------------- src/shrpx_http2_session.h | 4 --- 2 files changed, 17 insertions(+), 40 deletions(-) diff --git a/src/shrpx_http2_session.cc b/src/shrpx_http2_session.cc index 2d08e7f6..89a33e51 100644 --- a/src/shrpx_http2_session.cc +++ b/src/shrpx_http2_session.cc @@ -112,7 +112,6 @@ void writecb(struct ev_loop *loop, ev_io *w, int revents) { int rv; auto conn = static_cast(w->data); auto http2session = static_cast(conn->data); - http2session->clear_write_request(); rv = http2session->do_write(); if (rv != 0) { http2session->disconnect(http2session->should_hard_fail()); @@ -122,36 +121,13 @@ void writecb(struct ev_loop *loop, ev_io *w, int revents) { } } // namespace -namespace { -void wrschedcb(struct ev_loop *loop, ev_prepare *w, int revents) { - auto http2session = static_cast(w->data); - if (!http2session->write_requested()) { - return; - } - http2session->clear_write_request(); - switch (http2session->get_state()) { - case Http2Session::DISCONNECTED: - LOG(INFO) << "wrschedcb start connect"; - if (http2session->initiate_connection() != 0) { - SSLOG(FATAL, http2session) << "Could not initiate backend connection"; - http2session->disconnect(true); - } - break; - case Http2Session::CONNECTED: - writecb(loop, http2session->get_wev(), revents); - break; - } -} -} // namespace - Http2Session::Http2Session(struct ev_loop *loop, SSL_CTX *ssl_ctx) : conn_(loop, -1, nullptr, get_config()->downstream_write_timeout, get_config()->downstream_read_timeout, 0, 0, 0, 0, writecb, readcb, timeoutcb, this), ssl_ctx_(ssl_ctx), session_(nullptr), data_pending_(nullptr), data_pendinglen_(0), state_(DISCONNECTED), - connection_check_state_(CONNECTION_CHECK_NONE), flow_control_(false), - write_requested_(false) { + connection_check_state_(CONNECTION_CHECK_NONE), flow_control_(false) { read_ = write_ = &Http2Session::noop; on_read_ = on_write_ = &Http2Session::noop; @@ -166,11 +142,6 @@ Http2Session::Http2Session(struct ev_loop *loop, SSL_CTX *ssl_ctx) ev_timer_init(&settings_timer_, settings_timeout_cb, 0., 10.); settings_timer_.data = this; - - ev_prepare_init(&wrsched_prep_, &wrschedcb); - wrsched_prep_.data = this; - - ev_prepare_start(conn_.loop, &wrsched_prep_); } Http2Session::~Http2Session() { disconnect(); } @@ -202,7 +173,6 @@ int Http2Session::disconnect(bool hard) { connection_check_state_ = CONNECTION_CHECK_NONE; state_ = DISCONNECTED; - write_requested_ = false; // Delete all client handler associated to Downstream. When deleting // Http2DownstreamConnection, it calls this object's @@ -1341,11 +1311,22 @@ int Http2Session::downstream_write() { return 0; } -void Http2Session::signal_write() { write_requested_ = true; } - -void Http2Session::clear_write_request() { write_requested_ = false; } - -bool Http2Session::write_requested() const { return write_requested_; } +void Http2Session::signal_write() { + switch (state_) { + case Http2Session::DISCONNECTED: + if (LOG_ENABLED(INFO)) { + LOG(INFO) << "Start connecting to backend server"; + } + if (initiate_connection() != 0) { + SSLOG(FATAL, this) << "Could not initiate backend connection"; + disconnect(true); + } + break; + case Http2Session::CONNECTED: + conn_.wlimit.startw(); + break; + } +} struct ev_loop *Http2Session::get_loop() const { return conn_.loop; diff --git a/src/shrpx_http2_session.h b/src/shrpx_http2_session.h index f1a288a0..1161c679 100644 --- a/src/shrpx_http2_session.h +++ b/src/shrpx_http2_session.h @@ -108,8 +108,6 @@ public: int noop(); void signal_write(); - void clear_write_request(); - bool write_requested() const; struct ev_loop *get_loop() const; @@ -178,7 +176,6 @@ private: Connection conn_; ev_timer settings_timer_; ev_timer connchk_timer_; - ev_prepare wrsched_prep_; std::set dconns_; std::set streams_; std::function read_, write_; @@ -193,7 +190,6 @@ private: int state_; int connection_check_state_; bool flow_control_; - bool write_requested_; WriteBuf wb_; ReadBuf rb_; };