nghttpx: Share nghttp2_session_callbacks between objects

This commit is contained in:
Tatsuhiro Tsujikawa 2015-02-24 15:21:10 +09:00
parent 8417275368
commit 1a2bccd71c
6 changed files with 77 additions and 53 deletions

View File

@ -65,6 +65,8 @@
#include "shrpx_worker_config.h"
#include "shrpx_worker.h"
#include "shrpx_accept_handler.h"
#include "shrpx_http2_upstream.h"
#include "shrpx_http2_session.h"
#include "util.h"
#include "app_helper.h"
#include "ssl.h"
@ -889,6 +891,10 @@ void fill_default_config() {
mod_config()->padding = 0;
mod_config()->worker_frontend_connections = 0;
mod_config()->http2_upstream_callbacks = create_http2_upstream_callbacks();
mod_config()->http2_downstream_callbacks =
create_http2_downstream_callbacks();
nghttp2_option_new(&mod_config()->http2_option);
nghttp2_option_set_no_auto_window_update(get_config()->http2_option, 1);

View File

@ -247,6 +247,8 @@ struct Config {
std::unique_ptr<char[]> errorlog_file;
FILE *http2_upstream_dump_request_header;
FILE *http2_upstream_dump_response_header;
nghttp2_session_callbacks *http2_upstream_callbacks;
nghttp2_session_callbacks *http2_downstream_callbacks;
nghttp2_option *http2_option;
nghttp2_option *http2_client_option;
char **argv;

View File

@ -1096,6 +1096,45 @@ int on_frame_not_send_callback(nghttp2_session *session,
}
} // namespace
nghttp2_session_callbacks *create_http2_downstream_callbacks() {
int rv;
nghttp2_session_callbacks *callbacks;
rv = nghttp2_session_callbacks_new(&callbacks);
if (rv != 0) {
return nullptr;
}
nghttp2_session_callbacks_set_on_stream_close_callback(
callbacks, on_stream_close_callback);
nghttp2_session_callbacks_set_on_frame_recv_callback(callbacks,
on_frame_recv_callback);
nghttp2_session_callbacks_set_on_data_chunk_recv_callback(
callbacks, on_data_chunk_recv_callback);
nghttp2_session_callbacks_set_on_frame_send_callback(callbacks,
on_frame_send_callback);
nghttp2_session_callbacks_set_on_frame_not_send_callback(
callbacks, on_frame_not_send_callback);
nghttp2_session_callbacks_set_on_header_callback(callbacks,
on_header_callback);
nghttp2_session_callbacks_set_on_begin_headers_callback(
callbacks, on_begin_headers_callback);
if (get_config()->padding) {
nghttp2_session_callbacks_set_select_padding_callback(
callbacks, http::select_padding_callback);
}
return callbacks;
}
int Http2Session::on_connect() {
int rv;
@ -1127,43 +1166,9 @@ int Http2Session::on_connect() {
}
}
nghttp2_session_callbacks *callbacks;
rv = nghttp2_session_callbacks_new(&callbacks);
if (rv != 0) {
return -1;
}
auto callbacks_deleter = defer(nghttp2_session_callbacks_del, callbacks);
nghttp2_session_callbacks_set_on_stream_close_callback(
callbacks, on_stream_close_callback);
nghttp2_session_callbacks_set_on_frame_recv_callback(callbacks,
on_frame_recv_callback);
nghttp2_session_callbacks_set_on_data_chunk_recv_callback(
callbacks, on_data_chunk_recv_callback);
nghttp2_session_callbacks_set_on_frame_send_callback(callbacks,
on_frame_send_callback);
nghttp2_session_callbacks_set_on_frame_not_send_callback(
callbacks, on_frame_not_send_callback);
nghttp2_session_callbacks_set_on_header_callback(callbacks,
on_header_callback);
nghttp2_session_callbacks_set_on_begin_headers_callback(
callbacks, on_begin_headers_callback);
if (get_config()->padding) {
nghttp2_session_callbacks_set_select_padding_callback(
callbacks, http::select_padding_callback);
}
rv = nghttp2_session_client_new2(&session_, callbacks, this,
get_config()->http2_client_option);
rv = nghttp2_session_client_new2(&session_,
get_config()->http2_downstream_callbacks,
this, get_config()->http2_client_option);
if (rv != 0) {
return -1;

View File

@ -194,6 +194,8 @@ private:
ReadBuf rb_;
};
nghttp2_session_callbacks *create_http2_downstream_callbacks();
} // namespace shrpx
#endif // SHRPX_HTTP2_SESSION_H

View File

@ -605,25 +605,15 @@ void Http2Upstream::check_shutdown() {
}
}
Http2Upstream::Http2Upstream(ClientHandler *handler)
: downstream_queue_(
get_config()->http2_proxy
? get_config()->downstream_connections_per_host
: get_config()->downstream_proto == PROTO_HTTP
? get_config()->downstream_connections_per_frontend
: 0,
!get_config()->http2_proxy),
handler_(handler), session_(nullptr), data_pending_(nullptr),
data_pendinglen_(0), shutdown_handled_(false) {
nghttp2_session_callbacks *create_http2_upstream_callbacks() {
int rv;
nghttp2_session_callbacks *callbacks;
rv = nghttp2_session_callbacks_new(&callbacks);
assert(rv == 0);
auto callbacks_deleter = defer(nghttp2_session_callbacks_del, callbacks);
if (rv != 0) {
return nullptr;
}
nghttp2_session_callbacks_set_on_stream_close_callback(
callbacks, on_stream_close_callback);
@ -651,7 +641,24 @@ Http2Upstream::Http2Upstream(ClientHandler *handler)
callbacks, http::select_padding_callback);
}
rv = nghttp2_session_server_new2(&session_, callbacks, this,
return callbacks;
}
Http2Upstream::Http2Upstream(ClientHandler *handler)
: downstream_queue_(
get_config()->http2_proxy
? get_config()->downstream_connections_per_host
: get_config()->downstream_proto == PROTO_HTTP
? get_config()->downstream_connections_per_frontend
: 0,
!get_config()->http2_proxy),
handler_(handler), session_(nullptr), data_pending_(nullptr),
data_pendinglen_(0), shutdown_handled_(false) {
int rv;
rv = nghttp2_session_server_new2(&session_,
get_config()->http2_upstream_callbacks, this,
get_config()->http2_option);
assert(rv == 0);

View File

@ -119,6 +119,8 @@ private:
bool shutdown_handled_;
};
nghttp2_session_callbacks *create_http2_upstream_callbacks();
} // namespace shrpx
#endif // SHRPX_HTTP2_UPSTREAM_H