nghttpx: Return error when downstream HTTP/1 connection attempt failed

This commit is contained in:
Tatsuhiro Tsujikawa 2015-01-20 23:26:09 +09:00
parent ca7288ae38
commit b9f41e34ef
3 changed files with 46 additions and 1 deletions

View File

@ -69,6 +69,25 @@ func TestH1H1MultipleRequestCL(t *testing.T) {
}
}
func TestH1H1ConnectFailure(t *testing.T) {
st := newServerTester(nil, t, noopHandler)
defer st.Close()
// shutdown backend server to simulate backend connect failure
st.ts.Close()
res, err := st.http1(requestParam{
name: "TestH1H1ConnectFailure",
})
if err != nil {
t.Fatalf("Error st.http2() = %v", err)
}
want := 502
if got := res.status; got != want {
t.Errorf("status: %v; want %v", got, want)
}
}
func TestH2H1PlainGET(t *testing.T) {
st := newServerTester(nil, t, noopHandler)
defer st.Close()
@ -305,6 +324,25 @@ func TestH2H1InvalidRequestCL(t *testing.T) {
}
}
func TestH2H1ConnectFailure(t *testing.T) {
st := newServerTester(nil, t, noopHandler)
defer st.Close()
// shutdown backend server to simulate backend connect failure
st.ts.Close()
res, err := st.http2(requestParam{
name: "TestH2H1ConnectFailure",
})
if err != nil {
t.Fatalf("Error st.http2() = %v", err)
}
want := 502
if got := res.status; got != want {
t.Errorf("status: %v; want %v", got, want)
}
}
func TestH2H2MultipleResponseCL(t *testing.T) {
st := newServerTester([]string{"--http2-bridge"}, t, func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("content-length", "1")

View File

@ -95,7 +95,9 @@ void connectcb(struct ev_loop *loop, ev_io *w, int revents) {
auto upstream = downstream->get_upstream();
auto handler = upstream->get_client_handler();
if (dconn->on_connect() != 0) {
delete handler;
if (upstream->downstream_error(dconn, Downstream::EVENT_ERROR) != 0) {
delete handler;
}
return;
}
writecb(loop, w, revents);
@ -747,6 +749,9 @@ end:
int HttpDownstreamConnection::on_connect() {
if (!util::check_socket_connected(fd_)) {
if (LOG_ENABLED(INFO)) {
DLOG(INFO, this) << "downstream connect failed";
}
return -1;
}

View File

@ -560,6 +560,8 @@ int HttpsUpstream::downstream_error(DownstreamConnection *dconn, int events) {
return -1;
}
handler_->set_should_close_after_write(true);
unsigned int status;
if (events & Downstream::EVENT_TIMEOUT) {
status = 504;