From f2a1fadda90fcead2855d70ef49e3c4808985390 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Tue, 24 May 2016 21:24:30 +0900 Subject: [PATCH] nghttpx: Make backend fail if connect operation was timed out --- src/shrpx_http_downstream_connection.cc | 42 +++++++++++++++++++++++-- src/shrpx_http_downstream_connection.h | 4 ++- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/shrpx_http_downstream_connection.cc b/src/shrpx_http_downstream_connection.cc index 8afdce58..3192e8ee 100644 --- a/src/shrpx_http_downstream_connection.cc +++ b/src/shrpx_http_downstream_connection.cc @@ -66,6 +66,31 @@ void timeoutcb(struct ev_loop *loop, ev_timer *w, int revents) { } } // namespace +namespace { +void connect_timeoutcb(struct ev_loop *loop, ev_timer *w, int revents) { + auto conn = static_cast(w->data); + auto dconn = static_cast(conn->data); + + if (LOG_ENABLED(INFO)) { + DCLOG(INFO, dconn) << "Connect time out"; + } + + downstream_failure(dconn->get_addr()); + + auto downstream = dconn->get_downstream(); + auto upstream = downstream->get_upstream(); + auto handler = upstream->get_client_handler(); + auto &resp = downstream->response(); + + // Do this so that dconn is not pooled + resp.connection_close = true; + + if (upstream->downstream_error(dconn, Downstream::EVENT_TIMEOUT) != 0) { + delete handler; + } +} +} // namespace + namespace { void readcb(struct ev_loop *loop, ev_io *w, int revents) { auto conn = static_cast(w->data); @@ -128,7 +153,8 @@ HttpDownstreamConnection::HttpDownstreamConnection(DownstreamAddrGroup *group, : conn_(loop, -1, nullptr, worker->get_mcpool(), get_config()->conn.downstream.timeout.write, get_config()->conn.downstream.timeout.read, {}, {}, connectcb, - readcb, timeoutcb, this, get_config()->tls.dyn_rec.warmup_threshold, + readcb, connect_timeoutcb, this, + get_config()->tls.dyn_rec.warmup_threshold, get_config()->tls.dyn_rec.idle_timeout, PROTO_HTTP1), do_read_(&HttpDownstreamConnection::noop), do_write_(&HttpDownstreamConnection::noop), @@ -920,6 +946,12 @@ int HttpDownstreamConnection::tls_handshake() { } } + auto &connect_blocker = addr_->connect_blocker; + + connect_blocker->on_success(); + + conn_.timeoutcb = timeoutcb; + do_read_ = &HttpDownstreamConnection::read_tls; do_write_ = &HttpDownstreamConnection::write_tls; @@ -1082,8 +1114,6 @@ int HttpDownstreamConnection::connected() { DCLOG(INFO, this) << "Connected to downstream host"; } - connect_blocker->on_success(); - conn_.rlimit.startw(); ev_set_cb(&conn_.wev, writecb); @@ -1097,6 +1127,10 @@ int HttpDownstreamConnection::connected() { return 0; } + connect_blocker->on_success(); + + conn_.timeoutcb = timeoutcb; + do_read_ = &HttpDownstreamConnection::read_clear; do_write_ = &HttpDownstreamConnection::write_clear; @@ -1123,4 +1157,6 @@ HttpDownstreamConnection::get_downstream_addr_group() const { return group_; } +DownstreamAddr *HttpDownstreamConnection::get_addr() const { return addr_; } + } // namespace shrpx diff --git a/src/shrpx_http_downstream_connection.h b/src/shrpx_http_downstream_connection.h index 0199d9cc..3cca894d 100644 --- a/src/shrpx_http_downstream_connection.h +++ b/src/shrpx_http_downstream_connection.h @@ -79,10 +79,12 @@ public: int noop(); + DownstreamAddr *get_addr() const; + private: Connection conn_; std::function do_read_, do_write_, - do_signal_write_; + do_signal_write_; Worker *worker_; // nullptr if TLS is not used. SSL_CTX *ssl_ctx_;