Add nghttp2_session_request_allowed() API function

This function is useful for the client application to know that there
is a chance that request can be sent.  If this function returns 0,
there is zero chance to make a request.

This commit also set error_code passed to
nghttp2_on_stream_close_callback to NGHTTP2_REFUSED_STREAM if request
is not sent.
This commit is contained in:
Tatsuhiro Tsujikawa 2015-10-29 22:49:27 +09:00
parent ad395f0603
commit 1c37abc0dd
4 changed files with 32 additions and 0 deletions

View File

@ -105,6 +105,7 @@ APIDOCS= \
nghttp2_session_mem_recv.rst \
nghttp2_session_mem_send.rst \
nghttp2_session_recv.rst \
nghttp2_session_request_allowed.rst \
nghttp2_session_resume_data.rst \
nghttp2_session_send.rst \
nghttp2_session_server_new.rst \

View File

@ -3479,6 +3479,23 @@ NGHTTP2_EXTERN int nghttp2_submit_goaway(nghttp2_session *session,
NGHTTP2_EXTERN int32_t
nghttp2_session_get_last_proc_stream_id(nghttp2_session *session);
/**
* @function
*
* Returns nonzero if new request can be sent from local endpoint.
*
* This function return 0 if request is not allowed for this session.
* There are several reasons why request is not allowed. Some of the
* reasons are: session is server; stream ID has been spent; GOAWAY
* has been sent or received.
*
* The application can call `nghttp2_submit_request()` without
* consulting this function. In that case, `nghttp2_submit_request()`
* may return error. Or, request is failed to sent, and
* :type:`nghttp2_on_stream_close_callback` is called.
*/
NGHTTP2_EXTERN int nghttp2_session_request_allowed(nghttp2_session *session);
/**
* @function
*

View File

@ -1322,6 +1322,12 @@ static int session_predicate_for_stream_send(nghttp2_session *session,
return 0;
}
int nghttp2_session_request_allowed(nghttp2_session *session) {
return !session->server && session->next_stream_id <= INT32_MAX &&
(session->goaway_flags &
(NGHTTP2_GOAWAY_TERM_ON_SEND | NGHTTP2_GOAWAY_RECV)) == 0;
}
/*
* This function checks request HEADERS frame, which opens stream, can
* be sent at this time.
@ -2685,6 +2691,10 @@ static ssize_t nghttp2_session_mem_send_internal(nghttp2_session *session,
opened_stream_id = item->frame.hd.stream_id;
if (item->aux_data.headers.canceled) {
error_code = item->aux_data.headers.error_code;
} else {
/* Set error_code to REFUSED_STREAM so that application
can send request again. */
error_code = NGHTTP2_REFUSED_STREAM;
}
}
break;

View File

@ -210,6 +210,10 @@ void Http2Session::on_connect() {
}
int Http2Session::submit_request(RequestStat *req_stat) {
if (nghttp2_session_request_allowed(session_) == 0) {
return -1;
}
auto config = client_->worker->config;
auto &nva = config->nva[client_->reqidx++];