From c7fe718dcf05db67dfcc772ee66224afd6dfa479 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Wed, 18 Jul 2012 00:13:11 +0900 Subject: [PATCH] Added read/write timeout options for both upstream and downstream --- examples/shrpx.cc | 85 +++++++++++++++++++++---- examples/shrpx_config.h | 3 +- examples/shrpx_downstream.cc | 11 ---- examples/shrpx_downstream_connection.cc | 9 --- examples/shrpx_downstream_connection.h | 1 - examples/shrpx_spdy_upstream.cc | 2 +- 6 files changed, 75 insertions(+), 36 deletions(-) diff --git a/examples/shrpx.cc b/examples/shrpx.cc index 94fb696a..21084df0 100644 --- a/examples/shrpx.cc +++ b/examples/shrpx.cc @@ -244,22 +244,26 @@ void fill_default_config() mod_config()->private_key_file = 0; mod_config()->cert_file = 0; - mod_config()->upstream_read_timeout.tv_sec = 60; + // Read timeout for SPDY upstream connection + mod_config()->spdy_upstream_read_timeout.tv_sec = 180; + mod_config()->spdy_upstream_read_timeout.tv_usec = 0; + + // Read timeout for non-SPDY upstream connection + mod_config()->upstream_read_timeout.tv_sec = 180; mod_config()->upstream_read_timeout.tv_usec = 0; - mod_config()->upstream_write_timeout.tv_sec = 30; + + // Write timeout for SPDY/non-SPDY upstream connection + mod_config()->upstream_write_timeout.tv_sec = 60; mod_config()->upstream_write_timeout.tv_usec = 0; - mod_config()->spdy_upstream_read_timeout.tv_sec = 600; - mod_config()->spdy_upstream_read_timeout.tv_usec = 0; - mod_config()->spdy_upstream_write_timeout.tv_sec = 30; - mod_config()->spdy_upstream_write_timeout.tv_usec = 0; - - mod_config()->downstream_read_timeout.tv_sec = 60; + // Read/Write timeouts for downstream connection + mod_config()->downstream_read_timeout.tv_sec = 900; mod_config()->downstream_read_timeout.tv_usec = 0; - mod_config()->downstream_write_timeout.tv_sec = 30; + mod_config()->downstream_write_timeout.tv_sec = 60; mod_config()->downstream_write_timeout.tv_usec = 0; - mod_config()->downstream_idle_read_timeout.tv_sec = 15; + // Timeout for pooled (idle) connections + mod_config()->downstream_idle_read_timeout.tv_sec = 60; mod_config()->downstream_host = "localhost"; mod_config()->downstream_port = 80; @@ -305,7 +309,8 @@ namespace { void print_usage(std::ostream& out) { out << "Usage: shrpx [-Dhs] [-b ] [-f ] [-n ]\n" - << " [-c ] [-L ] \n" + << " [-c ] [-L ] [OPTIONS...]\n" + << " \n" << "\n" << "A reverse proxy for SPDY/HTTPS.\n" << std::endl; @@ -347,6 +352,27 @@ void print_help(std::ostream& out) << " --add-x-forwarded-for\n" << " Append X-Forwarded-For header field to the\n" << " downstream request.\n" + << " --frontend-spdy-read-timeout=\n" + << " Specify read timeout for SPDY frontend\n" + << " connection. Default: " + << get_config()->spdy_upstream_read_timeout.tv_sec << "\n" + << " --frontend-read-timeout=\n" + << " Specify read timeout for non-SPDY frontend\n" + << " connection. Default: " + << get_config()->upstream_read_timeout.tv_sec << "\n" + << " --frontend-write-timeout=\n" + << " Specify write timeout for both SPDY and\n" + << " non-SPDY frontends.\n" + << " connection. Default: " + << get_config()->upstream_write_timeout.tv_sec << "\n" + << " --backend-read-timeout=\n" + << " Specify read timeout for backend connection.\n" + << " Default: " + << get_config()->downstream_read_timeout.tv_sec << "\n" + << " --backend-write-timeout=\n" + << " Specify write timeout for backend\n" + << " connection. Default: " + << get_config()->downstream_write_timeout.tv_sec << "\n" << " -h, --help Print this help.\n" << std::endl; } @@ -373,7 +399,12 @@ int main(int argc, char **argv) {"log-level", required_argument, 0, 'L' }, {"daemon", no_argument, 0, 'D' }, {"spdy-proxy", no_argument, 0, 's' }, - {"add-x-forwarded-for", no_argument, &flag, 1}, + {"add-x-forwarded-for", no_argument, &flag, 1 }, + {"frontend-spdy-read-timeout", required_argument, &flag, 2 }, + {"frontend-read-timeout", required_argument, &flag, 3 }, + {"frontend-write-timeout", required_argument, &flag, 4 }, + {"backend-read-timeout", required_argument, &flag, 5 }, + {"backend-write-timeout", required_argument, &flag, 6 }, {"help", no_argument, 0, 'h' }, {0, 0, 0, 0 } }; @@ -431,6 +462,36 @@ int main(int argc, char **argv) // --add-x-forwarded-for mod_config()->add_x_forwarded_for = true; break; + case 2: { + // --frontend-spdy-read-timeout + timeval tv = {strtol(optarg, 0, 10), 0}; + mod_config()->spdy_upstream_read_timeout = tv; + break; + } + case 3: { + // --frontend-read-timeout + timeval tv = {strtol(optarg, 0, 10), 0}; + mod_config()->upstream_read_timeout = tv; + break; + } + case 4: { + // --frontend-write-timeout + timeval tv = {strtol(optarg, 0, 10), 0}; + mod_config()->upstream_write_timeout = tv; + break; + } + case 5: { + // --backend-read-timeout + timeval tv = {strtol(optarg, 0, 10), 0}; + mod_config()->downstream_read_timeout = tv; + break; + } + case 6: { + // --backend-write-timeout + timeval tv = {strtol(optarg, 0, 10), 0}; + mod_config()->downstream_write_timeout = tv; + break; + } default: break; } diff --git a/examples/shrpx_config.h b/examples/shrpx_config.h index 26b6d4d9..9042ab59 100644 --- a/examples/shrpx_config.h +++ b/examples/shrpx_config.h @@ -58,10 +58,9 @@ struct Config { const char *downstream_hostport; sockaddr_union downstream_addr; size_t downstream_addrlen; + timeval spdy_upstream_read_timeout; timeval upstream_read_timeout; timeval upstream_write_timeout; - timeval spdy_upstream_read_timeout; - timeval spdy_upstream_write_timeout; timeval downstream_read_timeout; timeval downstream_write_timeout; timeval downstream_idle_read_timeout; diff --git a/examples/shrpx_downstream.cc b/examples/shrpx_downstream.cc index 78aa7f21..78c41dca 100644 --- a/examples/shrpx_downstream.cc +++ b/examples/shrpx_downstream.cc @@ -536,17 +536,6 @@ int htp_hdrs_completecb(http_parser *htp) return -1; } - if(downstream->tunnel_established()) { - downstream->get_downstream_connection()->set_tunneling_timeout(); - // For tunneling, we remove upstream read timeouts. But it seems - // libevent cannot remove timeouts for SSL based bufferevent. Set - // long timeout here as a workaround. - timeval rtv = { 86400, 0 }; - timeval wtv = { 30, 0 }; - downstream->get_upstream()->get_client_handler() - ->set_upstream_timeouts(&rtv, &wtv); - } - if(downstream->get_request_method() == "HEAD") { // Ignore the response body. HEAD response may contain // Content-Length or Transfer-Encoding: chunked. diff --git a/examples/shrpx_downstream_connection.cc b/examples/shrpx_downstream_connection.cc index 3a97af6f..e1d498c1 100644 --- a/examples/shrpx_downstream_connection.cc +++ b/examples/shrpx_downstream_connection.cc @@ -116,15 +116,6 @@ void DownstreamConnection::start_waiting_response() } } -void DownstreamConnection::set_tunneling_timeout() -{ - if(bev_) { - bufferevent_set_timeouts(bev_, - &max_timeout, - &get_config()->downstream_write_timeout); - } -} - namespace { // Gets called when DownstreamConnection is pooled in ClientHandler. void idle_eventcb(bufferevent *bev, short events, void *arg) diff --git a/examples/shrpx_downstream_connection.h b/examples/shrpx_downstream_connection.h index edef7714..76a66c66 100644 --- a/examples/shrpx_downstream_connection.h +++ b/examples/shrpx_downstream_connection.h @@ -47,7 +47,6 @@ public: ClientHandler* get_client_handler(); Downstream* get_downstream(); void start_waiting_response(); - void set_tunneling_timeout(); private: ClientHandler *client_handler_; bufferevent *bev_; diff --git a/examples/shrpx_spdy_upstream.cc b/examples/shrpx_spdy_upstream.cc index 53fe838f..cb5c78ec 100644 --- a/examples/shrpx_spdy_upstream.cc +++ b/examples/shrpx_spdy_upstream.cc @@ -271,7 +271,7 @@ SpdyUpstream::SpdyUpstream(uint16_t version, ClientHandler *handler) { //handler->set_bev_cb(spdy_readcb, 0, spdy_eventcb); handler->set_upstream_timeouts(&get_config()->spdy_upstream_read_timeout, - &get_config()->spdy_upstream_write_timeout); + &get_config()->upstream_write_timeout); spdylay_session_callbacks callbacks; memset(&callbacks, 0, sizeof(callbacks));