From fa7945c6271fb31dcceffb66cd4bf0d8ee1fd249 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sat, 4 Nov 2017 15:55:25 +0900 Subject: [PATCH] nghttpx: Refactor --- src/shrpx_http_downstream_connection.cc | 98 +++++++++++-------------- 1 file changed, 41 insertions(+), 57 deletions(-) diff --git a/src/shrpx_http_downstream_connection.cc b/src/shrpx_http_downstream_connection.cc index 54ee89af..0e063705 100644 --- a/src/shrpx_http_downstream_connection.cc +++ b/src/shrpx_http_downstream_connection.cc @@ -72,62 +72,8 @@ 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); - auto addr = dconn->get_addr(); - auto raddr = dconn->get_raddr(); - - DCLOG(WARN, dconn) << "Connect time out; addr=" - << util::to_numeric_addr(raddr); - - downstream_failure(addr, raddr); - - auto downstream = dconn->get_downstream(); - auto upstream = downstream->get_upstream(); - auto handler = upstream->get_client_handler(); - - downstream->pop_downstream_connection(); - - int rv; - auto ndconn = handler->get_downstream_connection(rv, downstream); - if (ndconn) { - if (downstream->attach_downstream_connection(std::move(ndconn)) == 0 && - downstream->push_request_headers() == 0) { - return; - } - } - - downstream->set_request_state(Downstream::CONNECT_FAIL); - - if (rv == SHRPX_ERR_TLS_REQUIRED) { - rv = upstream->on_downstream_abort_request_with_https_redirect(downstream); - } else { - rv = upstream->on_downstream_abort_request(downstream, 504); - } - - if (rv != 0) { - delete handler; - } -} -} // namespace - -namespace { -void 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(); - auto upstream = downstream->get_upstream(); - auto handler = upstream->get_client_handler(); - - if (upstream->downstream_read(dconn) != 0) { - delete handler; - } -} -} // namespace - -namespace { -void backend_retry(Downstream *downstream) { +void retry_downstream_connection(Downstream *downstream, + unsigned int status_code) { auto upstream = downstream->get_upstream(); auto handler = upstream->get_client_handler(); @@ -154,7 +100,7 @@ void backend_retry(Downstream *downstream) { if (rv == SHRPX_ERR_TLS_REQUIRED) { rv = upstream->on_downstream_abort_request_with_https_redirect(downstream); } else { - rv = upstream->on_downstream_abort_request(downstream, 502); + rv = upstream->on_downstream_abort_request(downstream, status_code); } if (rv != 0) { @@ -163,6 +109,44 @@ void backend_retry(Downstream *downstream) { } } // 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); + auto addr = dconn->get_addr(); + auto raddr = dconn->get_raddr(); + + DCLOG(WARN, dconn) << "Connect time out; addr=" + << util::to_numeric_addr(raddr); + + downstream_failure(addr, raddr); + + auto downstream = dconn->get_downstream(); + + retry_downstream_connection(downstream, 504); +} +} // namespace + +namespace { +void 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(); + auto upstream = downstream->get_upstream(); + auto handler = upstream->get_client_handler(); + + if (upstream->downstream_read(dconn) != 0) { + delete handler; + } +} +} // namespace + +namespace { +void backend_retry(Downstream *downstream) { + retry_downstream_connection(downstream, 502); +} +} // namespace + namespace { void writecb(struct ev_loop *loop, ev_io *w, int revents) { int rv;