From 8a59ce6d37471ec7a437d4700cabd98c55115b1e Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Tue, 3 Sep 2019 00:28:21 +0900 Subject: [PATCH] nghttpx: Reconnect h1 backend if it lost connection before sending headers --- src/shrpx_http_downstream_connection.cc | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/shrpx_http_downstream_connection.cc b/src/shrpx_http_downstream_connection.cc index 23442bef..a3a67824 100644 --- a/src/shrpx_http_downstream_connection.cc +++ b/src/shrpx_http_downstream_connection.cc @@ -158,6 +158,16 @@ void backend_retry(Downstream *downstream) { } } // namespace +namespace { +void retry_readcb(struct ev_loop *loop, ev_io *w, int revents) { + auto conn = static_cast(w->data); + auto dconn = static_cast(conn->data); + auto downstream = dconn->get_downstream(); + + retry_downstream_connection(downstream, 502); +} +} // namespace + namespace { void writecb(struct ev_loop *loop, ev_io *w, int revents) { int rv; @@ -197,7 +207,7 @@ HttpDownstreamConnection::HttpDownstreamConnection( struct ev_loop *loop, Worker *worker) : conn_(loop, -1, nullptr, worker->get_mcpool(), group->shared_addr->timeout.write, group->shared_addr->timeout.read, - {}, {}, connectcb, readcb, connect_timeoutcb, this, + {}, {}, connectcb, retry_readcb, connect_timeoutcb, this, get_config()->tls.dyn_rec.warmup_threshold, get_config()->tls.dyn_rec.idle_timeout, Proto::HTTP1), on_read_(&HttpDownstreamConnection::noop), @@ -433,7 +443,10 @@ int HttpDownstreamConnection::initiate_connection() { conn_.again_rt(group_->shared_addr->timeout.read); } - ev_set_cb(&conn_.rev, readcb); + // downstream host might close connection while waiting for + // request header fields. If it happens, establish new + // connection. + ev_set_cb(&conn_.rev, retry_readcb); on_write_ = &HttpDownstreamConnection::write_first; first_write_done_ = false; @@ -1166,6 +1179,8 @@ int HttpDownstreamConnection::write_first() { return SHRPX_ERR_RETRY; } + ev_set_cb(&conn_.rev, readcb); + if (conn_.tls.ssl) { on_write_ = &HttpDownstreamConnection::write_tls; } else {