Added read/write timeout options for both upstream and downstream

This commit is contained in:
Tatsuhiro Tsujikawa 2012-07-18 00:13:11 +09:00
parent f10848febd
commit c7fe718dcf
6 changed files with 75 additions and 36 deletions

View File

@ -244,22 +244,26 @@ void fill_default_config()
mod_config()->private_key_file = 0; mod_config()->private_key_file = 0;
mod_config()->cert_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_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()->upstream_write_timeout.tv_usec = 0;
mod_config()->spdy_upstream_read_timeout.tv_sec = 600; // Read/Write timeouts for downstream connection
mod_config()->spdy_upstream_read_timeout.tv_usec = 0; mod_config()->downstream_read_timeout.tv_sec = 900;
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;
mod_config()->downstream_read_timeout.tv_usec = 0; 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_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_host = "localhost";
mod_config()->downstream_port = 80; mod_config()->downstream_port = 80;
@ -305,7 +309,8 @@ namespace {
void print_usage(std::ostream& out) void print_usage(std::ostream& out)
{ {
out << "Usage: shrpx [-Dhs] [-b <HOST,PORT>] [-f <HOST,PORT>] [-n <CORES>]\n" out << "Usage: shrpx [-Dhs] [-b <HOST,PORT>] [-f <HOST,PORT>] [-n <CORES>]\n"
<< " [-c <NUM>] [-L <LEVEL>] <PRIVATE_KEY> <CERT>\n" << " [-c <NUM>] [-L <LEVEL>] [OPTIONS...]\n"
<< " <PRIVATE_KEY> <CERT>\n"
<< "\n" << "\n"
<< "A reverse proxy for SPDY/HTTPS.\n" << "A reverse proxy for SPDY/HTTPS.\n"
<< std::endl; << std::endl;
@ -347,6 +352,27 @@ void print_help(std::ostream& out)
<< " --add-x-forwarded-for\n" << " --add-x-forwarded-for\n"
<< " Append X-Forwarded-For header field to the\n" << " Append X-Forwarded-For header field to the\n"
<< " downstream request.\n" << " downstream request.\n"
<< " --frontend-spdy-read-timeout=<SEC>\n"
<< " Specify read timeout for SPDY frontend\n"
<< " connection. Default: "
<< get_config()->spdy_upstream_read_timeout.tv_sec << "\n"
<< " --frontend-read-timeout=<SEC>\n"
<< " Specify read timeout for non-SPDY frontend\n"
<< " connection. Default: "
<< get_config()->upstream_read_timeout.tv_sec << "\n"
<< " --frontend-write-timeout=<SEC>\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=<SEC>\n"
<< " Specify read timeout for backend connection.\n"
<< " Default: "
<< get_config()->downstream_read_timeout.tv_sec << "\n"
<< " --backend-write-timeout=<SEC>\n"
<< " Specify write timeout for backend\n"
<< " connection. Default: "
<< get_config()->downstream_write_timeout.tv_sec << "\n"
<< " -h, --help Print this help.\n" << " -h, --help Print this help.\n"
<< std::endl; << std::endl;
} }
@ -373,7 +399,12 @@ int main(int argc, char **argv)
{"log-level", required_argument, 0, 'L' }, {"log-level", required_argument, 0, 'L' },
{"daemon", no_argument, 0, 'D' }, {"daemon", no_argument, 0, 'D' },
{"spdy-proxy", no_argument, 0, 's' }, {"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' }, {"help", no_argument, 0, 'h' },
{0, 0, 0, 0 } {0, 0, 0, 0 }
}; };
@ -431,6 +462,36 @@ int main(int argc, char **argv)
// --add-x-forwarded-for // --add-x-forwarded-for
mod_config()->add_x_forwarded_for = true; mod_config()->add_x_forwarded_for = true;
break; 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: default:
break; break;
} }

View File

@ -58,10 +58,9 @@ struct Config {
const char *downstream_hostport; const char *downstream_hostport;
sockaddr_union downstream_addr; sockaddr_union downstream_addr;
size_t downstream_addrlen; size_t downstream_addrlen;
timeval spdy_upstream_read_timeout;
timeval upstream_read_timeout; timeval upstream_read_timeout;
timeval upstream_write_timeout; timeval upstream_write_timeout;
timeval spdy_upstream_read_timeout;
timeval spdy_upstream_write_timeout;
timeval downstream_read_timeout; timeval downstream_read_timeout;
timeval downstream_write_timeout; timeval downstream_write_timeout;
timeval downstream_idle_read_timeout; timeval downstream_idle_read_timeout;

View File

@ -536,17 +536,6 @@ int htp_hdrs_completecb(http_parser *htp)
return -1; 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") { if(downstream->get_request_method() == "HEAD") {
// Ignore the response body. HEAD response may contain // Ignore the response body. HEAD response may contain
// Content-Length or Transfer-Encoding: chunked. // Content-Length or Transfer-Encoding: chunked.

View File

@ -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 { namespace {
// Gets called when DownstreamConnection is pooled in ClientHandler. // Gets called when DownstreamConnection is pooled in ClientHandler.
void idle_eventcb(bufferevent *bev, short events, void *arg) void idle_eventcb(bufferevent *bev, short events, void *arg)

View File

@ -47,7 +47,6 @@ public:
ClientHandler* get_client_handler(); ClientHandler* get_client_handler();
Downstream* get_downstream(); Downstream* get_downstream();
void start_waiting_response(); void start_waiting_response();
void set_tunneling_timeout();
private: private:
ClientHandler *client_handler_; ClientHandler *client_handler_;
bufferevent *bev_; bufferevent *bev_;

View File

@ -271,7 +271,7 @@ SpdyUpstream::SpdyUpstream(uint16_t version, ClientHandler *handler)
{ {
//handler->set_bev_cb(spdy_readcb, 0, spdy_eventcb); //handler->set_bev_cb(spdy_readcb, 0, spdy_eventcb);
handler->set_upstream_timeouts(&get_config()->spdy_upstream_read_timeout, 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; spdylay_session_callbacks callbacks;
memset(&callbacks, 0, sizeof(callbacks)); memset(&callbacks, 0, sizeof(callbacks));