Skip to content

Commit

Permalink
nghttpx: Add --backend-connect-timeout option
Browse files Browse the repository at this point in the history
  • Loading branch information
tatsuhiro-t committed Oct 10, 2016
1 parent 7549341 commit 00a8c37
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 4 deletions.
1 change: 1 addition & 0 deletions gennghttpxfun.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@
"backend-http2-decoder-dynamic-table-size",
"ecdh-curves",
"tls-sct-dir",
"backend-connect-timeout",
]

LOGVARS = [
Expand Down
13 changes: 13 additions & 0 deletions src/shrpx.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1442,6 +1442,7 @@ void fill_default_config(Config *config) {
timeoutconf.write = 30_s;
// Timeout for pooled (idle) connections
timeoutconf.idle_read = 2_s;
timeoutconf.connect = 30_s;
timeoutconf.max_backoff = 120_s;
}

Expand Down Expand Up @@ -1787,6 +1788,11 @@ void print_help(std::ostream &out) {
Specify write timeout for backend connection.
Default: )"
<< util::duration_str(config->conn.downstream->timeout.write) << R"(
--backend-connect-timeout=<DURATION>
Specify timeout before establishing TCP connection to
backend.
Default: )"
<< util::duration_str(config->conn.downstream->timeout.connect) << R"(
--backend-keep-alive-timeout=<DURATION>
Specify keep-alive timeout for backend connection.
Default: )"
Expand Down Expand Up @@ -2956,6 +2962,8 @@ int main(int argc, char **argv) {
required_argument, &flag, 139},
{SHRPX_OPT_ECDH_CURVES.c_str(), required_argument, &flag, 140},
{SHRPX_OPT_TLS_SCT_DIR.c_str(), required_argument, &flag, 141},
{SHRPX_OPT_BACKEND_CONNECT_TIMEOUT.c_str(), required_argument, &flag,
142},
{nullptr, 0, nullptr, 0}};

int option_index = 0;
Expand Down Expand Up @@ -3624,6 +3632,11 @@ int main(int argc, char **argv) {
// --tls-sct-dir
cmdcfgs.emplace_back(SHRPX_OPT_TLS_SCT_DIR, StringRef{optarg});
break;
case 142:
// --backend-connect-timeout
cmdcfgs.emplace_back(SHRPX_OPT_BACKEND_CONNECT_TIMEOUT,
StringRef{optarg});
break;
default:
break;
}
Expand Down
8 changes: 8 additions & 0 deletions src/shrpx_config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1657,6 +1657,11 @@ int option_lookup_token(const char *name, size_t namelen) {
return SHRPX_OPTID_BACKEND_RESPONSE_BUFFER;
}
break;
case 't':
if (util::strieq_l("backend-connect-timeou", name, 22)) {
return SHRPX_OPTID_BACKEND_CONNECT_TIMEOUT;
}
break;
}
break;
case 24:
Expand Down Expand Up @@ -2162,6 +2167,9 @@ int parse_config(Config *config, int optid, const StringRef &opt,
return parse_duration(&config->conn.downstream->timeout.read, opt, optarg);
case SHRPX_OPTID_BACKEND_WRITE_TIMEOUT:
return parse_duration(&config->conn.downstream->timeout.write, opt, optarg);
case SHRPX_OPTID_BACKEND_CONNECT_TIMEOUT:
return parse_duration(&config->conn.downstream->timeout.connect, opt,
optarg);
case SHRPX_OPTID_STREAM_READ_TIMEOUT:
return parse_duration(&config->http2.timeout.stream_read, opt, optarg);
case SHRPX_OPTID_STREAM_WRITE_TIMEOUT:
Expand Down
4 changes: 4 additions & 0 deletions src/shrpx_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,8 @@ constexpr auto SHRPX_OPT_BACKEND_HTTP2_DECODER_DYNAMIC_TABLE_SIZE =
StringRef::from_lit("backend-http2-decoder-dynamic-table-size");
constexpr auto SHRPX_OPT_ECDH_CURVES = StringRef::from_lit("ecdh-curves");
constexpr auto SHRPX_OPT_TLS_SCT_DIR = StringRef::from_lit("tls-sct-dir");
constexpr auto SHRPX_OPT_BACKEND_CONNECT_TIMEOUT =
StringRef::from_lit("backend-connect-timeout");

constexpr size_t SHRPX_OBFUSCATED_NODE_LENGTH = 8;

Expand Down Expand Up @@ -718,6 +720,7 @@ struct DownstreamConfig {
ev_tstamp read;
ev_tstamp write;
ev_tstamp idle_read;
ev_tstamp connect;
// The maximum backoff while checking health check for offline
// backend or while detaching failed backend from load balancing
// group temporarily.
Expand Down Expand Up @@ -850,6 +853,7 @@ enum {
SHRPX_OPTID_API_MAX_REQUEST_BODY,
SHRPX_OPTID_BACKEND,
SHRPX_OPTID_BACKEND_ADDRESS_FAMILY,
SHRPX_OPTID_BACKEND_CONNECT_TIMEOUT,
SHRPX_OPTID_BACKEND_CONNECTIONS_PER_FRONTEND,
SHRPX_OPTID_BACKEND_CONNECTIONS_PER_HOST,
SHRPX_OPTID_BACKEND_HTTP_PROXY_URI,
Expand Down
13 changes: 11 additions & 2 deletions src/shrpx_http2_session.cc
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,8 @@ int Http2Session::initiate_connection() {
}
}

auto &downstreamconf = *get_config()->conn.downstream;

const auto &proxy = get_config()->downstream_http_proxy;
if (!proxy.host.empty() && state_ == DISCONNECTED) {
if (LOG_ENABLED(INFO)) {
Expand Down Expand Up @@ -351,7 +353,7 @@ int Http2Session::initiate_connection() {

conn_.wlimit.startw();

// TODO we should have timeout for connection establishment
conn_.wt.repeat = downstreamconf.timeout.connect;
ev_timer_again(conn_.loop, &conn_.wt);

write_ = &Http2Session::connected;
Expand Down Expand Up @@ -484,7 +486,8 @@ int Http2Session::initiate_connection() {
if (state_ != CONNECTED) {
state_ = CONNECTING;
conn_.wlimit.startw();
// TODO we should have timeout for connection establishment

conn_.wt.repeat = downstreamconf.timeout.connect;
ev_timer_again(conn_.loop, &conn_.wt);
} else {
conn_.rlimit.startw();
Expand Down Expand Up @@ -1857,6 +1860,12 @@ int Http2Session::connected() {
SSLOG(INFO, this) << "Connection established";
}

auto &downstreamconf = *get_config()->conn.downstream;

// Reset timeout for write. Previously, we set timeout for connect.
conn_.wt.repeat = downstreamconf.timeout.write;
ev_timer_again(conn_.loop, &conn_.wt);

conn_.rlimit.startw();

read_ = &Http2Session::read_clear;
Expand Down
8 changes: 7 additions & 1 deletion src/shrpx_http_downstream_connection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ int HttpDownstreamConnection::attach_downstream(Downstream *downstream) {
break;
}

// TODO we should have timeout for connection establishment
conn_.wt.repeat = downstreamconf.timeout.connect;
ev_timer_again(conn_.loop, &conn_.wt);
} else {
// we may set read timer cb to idle_timeoutcb. Reset again.
Expand Down Expand Up @@ -1178,6 +1178,12 @@ int HttpDownstreamConnection::connected() {
DCLOG(INFO, this) << "Connected to downstream host";
}

auto &downstreamconf = *get_config()->conn.downstream;

// Reset timeout for write. Previously, we set timeout for connect.
conn_.wt.repeat = downstreamconf.timeout.write;
ev_timer_again(conn_.loop, &conn_.wt);

conn_.rlimit.startw();

ev_set_cb(&conn_.wev, writecb);
Expand Down
10 changes: 9 additions & 1 deletion src/shrpx_live_check.cc
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,9 @@ int LiveCheck::initiate_connection() {

conn_.wlimit.startw();

// TODO we should have timeout for connection establishment
auto &downstreamconf = *get_config()->conn.downstream;

conn_.wt.repeat = downstreamconf.timeout.connect;
ev_timer_again(conn_.loop, &conn_.wt);

return 0;
Expand All @@ -274,6 +276,12 @@ int LiveCheck::connected() {
LOG(INFO) << "Connection established";
}

auto &downstreamconf = *get_config()->conn.downstream;

// Reset timeout for write. Previously, we set timeout for connect.
conn_.wt.repeat = downstreamconf.timeout.write;
ev_timer_again(conn_.loop, &conn_.wt);

conn_.rlimit.startw();

if (conn_.tls.ssl) {
Expand Down

0 comments on commit 00a8c37

Please sign in to comment.