diff --git a/src/shrpx_http2_upstream.cc b/src/shrpx_http2_upstream.cc index af71e79a..dc2572a5 100644 --- a/src/shrpx_http2_upstream.cc +++ b/src/shrpx_http2_upstream.cc @@ -47,6 +47,7 @@ namespace shrpx { namespace { const size_t OUTBUF_MAX_THRES = 64*1024; +const size_t INBUF_MAX_THRES = 64*1024; } // namespace namespace { @@ -610,8 +611,8 @@ int Http2Upstream::send() auto output = bufferevent_get_output(bev); util::EvbufferBuffer evbbuf(output, buf, sizeof(buf)); for(;;) { - // Check buffer length and return WOULDBLOCK if it is large enough. - if(handler_->get_outbuf_length() + evbbuf.get_buflen() > + // Check buffer length and break if it is large enough. + if(handler_->get_outbuf_length() + evbbuf.get_buflen() >= OUTBUF_MAX_THRES) { break; } @@ -903,7 +904,6 @@ ssize_t downstream_data_read_callback(nghttp2_session *session, { auto downstream = static_cast(source->ptr); auto upstream = static_cast(downstream->get_upstream()); - auto handler = upstream->get_client_handler(); auto body = downstream->get_response_body_buf(); assert(body); @@ -927,16 +927,12 @@ ssize_t downstream_data_read_callback(nghttp2_session *session, (downstream->get_response_rst_stream_error_code())); } } - // Send WINDOW_UPDATE before buffer is empty to avoid delay because - // of RTT. - if(((*data_flags) & NGHTTP2_DATA_FLAG_EOF) == 0 && - handler->get_outbuf_length() + evbuffer_get_length(body) < - OUTBUF_MAX_THRES) { + + if(nread == 0 && ((*data_flags) & NGHTTP2_DATA_FLAG_EOF) == 0) { if(downstream->resume_read(SHRPX_NO_BUFFER) != 0) { return NGHTTP2_ERR_CALLBACK_FAILURE; } - } - if(nread == 0 && ((*data_flags) & NGHTTP2_DATA_FLAG_EOF) == 0) { + return NGHTTP2_ERR_DEFERRED; } return nread; @@ -1105,8 +1101,6 @@ int Http2Upstream::on_downstream_body(Downstream *downstream, const uint8_t *data, size_t len, bool flush) { - auto upstream = downstream->get_upstream(); - auto handler = upstream->get_client_handler(); auto body = downstream->get_response_body_buf(); int rv = evbuffer_add(body, data, len); if(rv != 0) { @@ -1118,9 +1112,7 @@ int Http2Upstream::on_downstream_body(Downstream *downstream, nghttp2_session_resume_data(session_, downstream->get_stream_id()); } - auto outbuflen = handler->get_outbuf_length() + - evbuffer_get_length(body); - if(outbuflen > OUTBUF_MAX_THRES) { + if(evbuffer_get_length(body) >= INBUF_MAX_THRES) { if(!flush) { nghttp2_session_resume_data(session_, downstream->get_stream_id()); } diff --git a/src/shrpx_https_upstream.cc b/src/shrpx_https_upstream.cc index 403beffc..dff44a38 100644 --- a/src/shrpx_https_upstream.cc +++ b/src/shrpx_https_upstream.cc @@ -405,6 +405,8 @@ void https_downstream_readcb(bufferevent *bev, void *ptr) if(downstream->get_response_state() == Downstream::MSG_RESET) { delete upstream->get_client_handler(); } else if(rv == 0) { + auto handler = upstream->get_client_handler(); + if(downstream->get_response_state() == Downstream::MSG_COMPLETE) { if(downstream->get_response_connection_close()) { // Connection close @@ -415,7 +417,6 @@ void https_downstream_readcb(bufferevent *bev, void *ptr) // Keep-alive dconn->detach_downstream(downstream); } - auto handler = upstream->get_client_handler(); if(downstream->get_request_state() == Downstream::MSG_COMPLETE) { if(handler->get_should_close_after_write() && handler->get_outbuf_length() == 0) { @@ -450,8 +451,7 @@ void https_downstream_readcb(bufferevent *bev, void *ptr) } } } else { - if(upstream->get_client_handler()->get_outbuf_length() > - OUTBUF_MAX_THRES) { + if(handler->get_outbuf_length() >= OUTBUF_MAX_THRES) { downstream->pause_read(SHRPX_NO_BUFFER); } } diff --git a/src/shrpx_spdy_upstream.cc b/src/shrpx_spdy_upstream.cc index fb62b1f0..8cf6c763 100644 --- a/src/shrpx_spdy_upstream.cc +++ b/src/shrpx_spdy_upstream.cc @@ -46,6 +46,7 @@ namespace shrpx { namespace { const size_t OUTBUF_MAX_THRES = 64*1024; +const size_t INBUF_MAX_THRES = 64*1024; } // namespace namespace { @@ -58,7 +59,7 @@ ssize_t send_callback(spdylay_session *session, auto handler = upstream->get_client_handler(); // Check buffer length and return WOULDBLOCK if it is large enough. - if(handler->get_outbuf_length() + upstream->sendbuf.get_buflen() > + if(handler->get_outbuf_length() + upstream->sendbuf.get_buflen() >= OUTBUF_MAX_THRES) { return SPDYLAY_ERR_WOULDBLOCK; } @@ -725,7 +726,6 @@ ssize_t spdy_data_read_callback(spdylay_session *session, { auto downstream = static_cast(source->ptr); auto upstream = static_cast(downstream->get_upstream()); - auto handler = upstream->get_client_handler(); auto body = downstream->get_response_body_buf(); assert(body); int nread = evbuffer_remove(body, buf, length); @@ -747,18 +747,15 @@ ssize_t spdy_data_read_callback(spdylay_session *session, (downstream->get_response_rst_stream_error_code())); } } - // Send WINDOW_UPDATE before buffer is empty to avoid delay because - // of RTT. - if(*eof != 1 && - handler->get_outbuf_length() + evbuffer_get_length(body) < - OUTBUF_MAX_THRES) { + + if(nread == 0 && *eof != 1) { if(downstream->resume_read(SHRPX_NO_BUFFER) != 0) { return SPDYLAY_ERR_CALLBACK_FAILURE; } - } - if(nread == 0 && *eof != 1) { + return SPDYLAY_ERR_DEFERRED; } + return nread; } } // namespace @@ -930,7 +927,6 @@ int SpdyUpstream::on_downstream_body(Downstream *downstream, const uint8_t *data, size_t len, bool flush) { - auto upstream = downstream->get_upstream(); auto body = downstream->get_response_body_buf(); int rv = evbuffer_add(body, data, len); if(rv != 0) { @@ -942,9 +938,7 @@ int SpdyUpstream::on_downstream_body(Downstream *downstream, spdylay_session_resume_data(session_, downstream->get_stream_id()); } - auto outbuflen = upstream->get_client_handler()->get_outbuf_length() + - evbuffer_get_length(body); - if(outbuflen > OUTBUF_MAX_THRES) { + if(evbuffer_get_length(body) >= INBUF_MAX_THRES) { if(!flush) { spdylay_session_resume_data(session_, downstream->get_stream_id()); }