nghttpx: Fix 1 second delay in HTTP/2 backend connection

This commit is contained in:
Tatsuhiro Tsujikawa 2015-02-20 19:48:35 +09:00
parent 00a83a44b4
commit 4424cf5b65
2 changed files with 17 additions and 40 deletions

View File

@ -112,7 +112,6 @@ void writecb(struct ev_loop *loop, ev_io *w, int revents) {
int rv; int rv;
auto conn = static_cast<Connection *>(w->data); auto conn = static_cast<Connection *>(w->data);
auto http2session = static_cast<Http2Session *>(conn->data); auto http2session = static_cast<Http2Session *>(conn->data);
http2session->clear_write_request();
rv = http2session->do_write(); rv = http2session->do_write();
if (rv != 0) { if (rv != 0) {
http2session->disconnect(http2session->should_hard_fail()); http2session->disconnect(http2session->should_hard_fail());
@ -122,36 +121,13 @@ void writecb(struct ev_loop *loop, ev_io *w, int revents) {
} }
} // namespace } // namespace
namespace {
void wrschedcb(struct ev_loop *loop, ev_prepare *w, int revents) {
auto http2session = static_cast<Http2Session *>(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) Http2Session::Http2Session(struct ev_loop *loop, SSL_CTX *ssl_ctx)
: conn_(loop, -1, nullptr, get_config()->downstream_write_timeout, : conn_(loop, -1, nullptr, get_config()->downstream_write_timeout,
get_config()->downstream_read_timeout, 0, 0, 0, 0, writecb, readcb, get_config()->downstream_read_timeout, 0, 0, 0, 0, writecb, readcb,
timeoutcb, this), timeoutcb, this),
ssl_ctx_(ssl_ctx), session_(nullptr), data_pending_(nullptr), ssl_ctx_(ssl_ctx), session_(nullptr), data_pending_(nullptr),
data_pendinglen_(0), state_(DISCONNECTED), data_pendinglen_(0), state_(DISCONNECTED),
connection_check_state_(CONNECTION_CHECK_NONE), flow_control_(false), connection_check_state_(CONNECTION_CHECK_NONE), flow_control_(false) {
write_requested_(false) {
read_ = write_ = &Http2Session::noop; read_ = write_ = &Http2Session::noop;
on_read_ = on_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.); ev_timer_init(&settings_timer_, settings_timeout_cb, 0., 10.);
settings_timer_.data = this; settings_timer_.data = this;
ev_prepare_init(&wrsched_prep_, &wrschedcb);
wrsched_prep_.data = this;
ev_prepare_start(conn_.loop, &wrsched_prep_);
} }
Http2Session::~Http2Session() { disconnect(); } Http2Session::~Http2Session() { disconnect(); }
@ -202,7 +173,6 @@ int Http2Session::disconnect(bool hard) {
connection_check_state_ = CONNECTION_CHECK_NONE; connection_check_state_ = CONNECTION_CHECK_NONE;
state_ = DISCONNECTED; state_ = DISCONNECTED;
write_requested_ = false;
// Delete all client handler associated to Downstream. When deleting // Delete all client handler associated to Downstream. When deleting
// Http2DownstreamConnection, it calls this object's // Http2DownstreamConnection, it calls this object's
@ -1341,11 +1311,22 @@ int Http2Session::downstream_write() {
return 0; return 0;
} }
void Http2Session::signal_write() { write_requested_ = true; } void Http2Session::signal_write() {
switch (state_) {
void Http2Session::clear_write_request() { write_requested_ = false; } case Http2Session::DISCONNECTED:
if (LOG_ENABLED(INFO)) {
bool Http2Session::write_requested() const { return write_requested_; } 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 { struct ev_loop *Http2Session::get_loop() const {
return conn_.loop; return conn_.loop;

View File

@ -108,8 +108,6 @@ public:
int noop(); int noop();
void signal_write(); void signal_write();
void clear_write_request();
bool write_requested() const;
struct ev_loop *get_loop() const; struct ev_loop *get_loop() const;
@ -178,7 +176,6 @@ private:
Connection conn_; Connection conn_;
ev_timer settings_timer_; ev_timer settings_timer_;
ev_timer connchk_timer_; ev_timer connchk_timer_;
ev_prepare wrsched_prep_;
std::set<Http2DownstreamConnection *> dconns_; std::set<Http2DownstreamConnection *> dconns_;
std::set<StreamData *> streams_; std::set<StreamData *> streams_;
std::function<int(Http2Session &)> read_, write_; std::function<int(Http2Session &)> read_, write_;
@ -193,7 +190,6 @@ private:
int state_; int state_;
int connection_check_state_; int connection_check_state_;
bool flow_control_; bool flow_control_;
bool write_requested_;
WriteBuf wb_; WriteBuf wb_;
ReadBuf rb_; ReadBuf rb_;
}; };