Skip to content

Commit

Permalink
Optimze http2 settings (#4939)
Browse files Browse the repository at this point in the history
* Refactor http2 settings

* revert name

* revert

* fix

* fix 2

* fix 3

* optimize

* format

* [test] fix 3

* [test] fix 4

* fix 5

* fix 6

* [test] optimize

* [test] optimize 2

* fix test

* [test] fix 5
  • Loading branch information
matyhtf authored Dec 30, 2022
1 parent c4f9279 commit 4593180
Show file tree
Hide file tree
Showing 20 changed files with 330 additions and 151 deletions.
11 changes: 10 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,16 @@ set_target_properties(ext-swoole PROPERTIES PREFIX "")
set_target_properties(ext-swoole PROPERTIES OUTPUT_NAME "swoole")
add_dependencies(ext-swoole lib-swoole)

target_link_libraries(ext-swoole swoole)
# find libpq
if (DEFINED libpq_dir)
target_include_directories(ext-swoole BEFORE ${LIBPQ_INCLUDE_DIRS})
else()
find_package(PkgConfig REQUIRED)
pkg_check_modules(LIBPQ REQUIRED libpq)
target_include_directories(ext-swoole PRIVATE ${LIBPQ_INCLUDE_DIRS})
endif()

target_link_libraries(ext-swoole swoole pq)

# install
INSTALL(TARGETS ext-swoole LIBRARY DESTINATION ${PHP_EXTENSION_DIR})
Expand Down
63 changes: 63 additions & 0 deletions core-tests/src/protocol/http2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
+----------------------------------------------------------------------+
| Swoole |
+----------------------------------------------------------------------+
| This source file is subject to version 2.0 of the Apache license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.apache.org/licenses/LICENSE-2.0.html |
| If you did not receive a copy of the Apache2.0 license and are unable|
| to obtain it through the world-wide-web, please send a note to |
| license@swoole.com so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| @link https://www.swoole.com/ |
| @contact team@swoole.com |
| @license https://github.com/swoole/swoole-src/blob/master/LICENSE |
| @Author Tianfeng Han <rango@swoole.com> |
+----------------------------------------------------------------------+
*/

#include "test_core.h"
#include "test_coroutine.h"
#include "redis_client.h"
#include "swoole_http2.h"

using namespace swoole;
using namespace std;

const std::string REDIS_TEST_KEY = "key-swoole";
const std::string REDIS_TEST_VALUE = "value-swoole";

TEST(http2, default_settings) {
ASSERT_EQ(http2::get_default_setting(SW_HTTP2_SETTING_HEADER_TABLE_SIZE), SW_HTTP2_DEFAULT_HEADER_TABLE_SIZE);
ASSERT_EQ(http2::get_default_setting(SW_HTTP2_SETTINGS_ENABLE_PUSH), SW_HTTP2_DEFAULT_ENABLE_PUSH);
ASSERT_EQ(http2::get_default_setting(SW_HTTP2_SETTINGS_MAX_CONCURRENT_STREAMS),
SW_HTTP2_DEFAULT_MAX_CONCURRENT_STREAMS);
ASSERT_EQ(http2::get_default_setting(SW_HTTP2_SETTINGS_INIT_WINDOW_SIZE), SW_HTTP2_DEFAULT_INIT_WINDOW_SIZE);
ASSERT_EQ(http2::get_default_setting(SW_HTTP2_SETTINGS_MAX_FRAME_SIZE), SW_HTTP2_DEFAULT_MAX_FRAME_SIZE);
ASSERT_EQ(http2::get_default_setting(SW_HTTP2_SETTINGS_MAX_HEADER_LIST_SIZE),
SW_HTTP2_DEFAULT_MAX_HEADER_LIST_SIZE);

http2::Settings _settings = {
(uint32_t) swoole_rand(1, 100000),
(uint32_t) swoole_rand(1, 100000),
(uint32_t) swoole_rand(1, 100000),
(uint32_t) swoole_rand(1, 100000),
(uint32_t) swoole_rand(1, 100000),
(uint32_t) swoole_rand(1, 100000),
};

http2::put_default_setting(SW_HTTP2_SETTING_HEADER_TABLE_SIZE, _settings.header_table_size);
http2::put_default_setting(SW_HTTP2_SETTINGS_ENABLE_PUSH, _settings.enable_push);
http2::put_default_setting(SW_HTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, _settings.max_concurrent_streams);
http2::put_default_setting(SW_HTTP2_SETTINGS_INIT_WINDOW_SIZE, _settings.init_window_size);
http2::put_default_setting(SW_HTTP2_SETTINGS_MAX_FRAME_SIZE, _settings.max_frame_size);
http2::put_default_setting(SW_HTTP2_SETTINGS_MAX_HEADER_LIST_SIZE, _settings.max_header_list_size);

ASSERT_EQ(http2::get_default_setting(SW_HTTP2_SETTING_HEADER_TABLE_SIZE), _settings.header_table_size);
ASSERT_EQ(http2::get_default_setting(SW_HTTP2_SETTINGS_ENABLE_PUSH), _settings.enable_push);
ASSERT_EQ(http2::get_default_setting(SW_HTTP2_SETTINGS_MAX_CONCURRENT_STREAMS), _settings.max_concurrent_streams);
ASSERT_EQ(http2::get_default_setting(SW_HTTP2_SETTINGS_INIT_WINDOW_SIZE), _settings.init_window_size);
ASSERT_EQ(http2::get_default_setting(SW_HTTP2_SETTINGS_MAX_FRAME_SIZE), _settings.max_frame_size);
ASSERT_EQ(http2::get_default_setting(SW_HTTP2_SETTINGS_MAX_HEADER_LIST_SIZE), _settings.max_header_list_size);
}
29 changes: 27 additions & 2 deletions ext-src/php_swoole.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ END_EXTERN_C()
#include "swoole_mime_type.h"
#include "swoole_server.h"
#include "swoole_util.h"
#include "swoole_http2.h"

#include <netinet/in.h>
#include <arpa/inet.h>
Expand Down Expand Up @@ -239,6 +240,8 @@ void php_swoole_set_global_option(HashTable *vht) {
sw_logger()->set_level(0);
}
#endif
// [Logger]
// ======================================================================
if (php_swoole_array_get_value(vht, "trace_flags", ztmp)) {
SwooleG.trace_flags = (uint32_t) SW_MAX(0, zval_get_long(ztmp));
}
Expand All @@ -260,10 +263,13 @@ void php_swoole_set_global_option(HashTable *vht) {
if (php_swoole_array_get_value(vht, "display_errors", ztmp)) {
SWOOLE_G(display_errors) = zval_is_true(ztmp);
}
// [DNS]
// ======================================================================
if (php_swoole_array_get_value(vht, "dns_server", ztmp)) {
swoole_set_dns_server(zend::String(ztmp).to_std_string());
}

// [Socket]
// ======================================================================
auto timeout_format = [](zval *v) -> double {
double timeout = zval_get_double(v);
if (timeout <= 0 || timeout > INT_MAX) {
Expand All @@ -272,7 +278,6 @@ void php_swoole_set_global_option(HashTable *vht) {
return timeout;
}
};

if (php_swoole_array_get_value(vht, "socket_dns_timeout", ztmp)) {
Socket::default_dns_timeout = timeout_format(ztmp);
}
Expand All @@ -293,6 +298,26 @@ void php_swoole_set_global_option(HashTable *vht) {
if (php_swoole_array_get_value(vht, "socket_timeout", ztmp)) {
Socket::default_read_timeout = Socket::default_write_timeout = timeout_format(ztmp);
}
// [HTTP2]
// ======================================================================
if (php_swoole_array_get_value(vht, "http2_header_table_size", ztmp)) {
swoole::http2::put_default_setting(SW_HTTP2_SETTING_HEADER_TABLE_SIZE, zval_get_long(ztmp));
}
if (php_swoole_array_get_value(vht, "http2_enable_push", ztmp)) {
swoole::http2::put_default_setting(SW_HTTP2_SETTINGS_ENABLE_PUSH, zval_get_long(ztmp));
}
if (php_swoole_array_get_value(vht, "http2_max_concurrent_streams", ztmp)) {
swoole::http2::put_default_setting(SW_HTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, zval_get_long(ztmp));
}
if (php_swoole_array_get_value(vht, "http2_init_window_size", ztmp)) {
swoole::http2::put_default_setting(SW_HTTP2_SETTINGS_INIT_WINDOW_SIZE, zval_get_long(ztmp));
}
if (php_swoole_array_get_value(vht, "http2_max_frame_size", ztmp)) {
swoole::http2::put_default_setting(SW_HTTP2_SETTINGS_MAX_FRAME_SIZE, zval_get_long(ztmp));
}
if (php_swoole_array_get_value(vht, "http2_max_header_list_size", ztmp)) {
swoole::http2::put_default_setting(SW_HTTP2_SETTINGS_MAX_HEADER_LIST_SIZE, zval_get_long(ztmp));
}
}

void php_swoole_register_rshutdown_callback(swoole::Callback cb, void *private_data) {
Expand Down
4 changes: 4 additions & 0 deletions ext-src/php_swoole_http.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,10 @@ class Session {
http2::Settings local_settings = {};
http2::Settings remote_settings = {};

// flow control
uint32_t remote_window_size;
uint32_t local_window_size;

uint32_t last_stream_id;
bool shutting_down;
bool is_coro;
Expand Down
8 changes: 7 additions & 1 deletion ext-src/php_swoole_library.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
+----------------------------------------------------------------------+
*/

/* $Id: 33dd2054173c9109f327ffa2eaf50310c397d92d */
/* $Id: 70a8531dc7f61e3519e7c115357b01d585acc3f9 */

#ifndef SWOOLE_LIBRARY_H
#define SWOOLE_LIBRARY_H
Expand Down Expand Up @@ -8550,6 +8550,12 @@ static const char* swoole_library_source_core_server_helper =
" 'socket_recv_timeout' => true,\n"
" 'socket_buffer_size' => true,\n"
" 'socket_timeout' => true,\n"
" 'http2_header_table_size' => true,\n"
" 'http2_enable_push' => true,\n"
" 'http2_max_concurrent_streams' => true,\n"
" 'http2_init_window_size' => true,\n"
" 'http2_max_frame_size' => true,\n"
" 'http2_max_header_list_size' => true,\n"
" ];\n"
"\n"
" public const SERVER_OPTIONS = [\n"
Expand Down
Loading

0 comments on commit 4593180

Please sign in to comment.