Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/NathanFreeman/swoole-src
Browse files Browse the repository at this point in the history
…into support_llhttp
  • Loading branch information
NathanFreeman committed Nov 28, 2024
2 parents 0a8f4ae + c086ddf commit a7d6bfc
Show file tree
Hide file tree
Showing 22 changed files with 533 additions and 241 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ jobs:
sudo lcov --list coverage.info

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
uses: codecov/codecov-action@v5
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: ./coverage.info
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ PROJECT(libswoole)
cmake_minimum_required(VERSION 2.8.12)

ENABLE_LANGUAGE(ASM)
set(SWOOLE_VERSION 6.0.0-dev)
set(SWOOLE_VERSION 6.0.0RC1)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -g")
Expand Down
14 changes: 10 additions & 4 deletions config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -953,23 +953,29 @@ EOF
dnl Check should we link to librt

if test "$PHP_IOURING" = "yes" && test "$SW_OS" = "LINUX"; then
PKG_CHECK_MODULES([URING], [liburing])
PKG_CHECK_MODULES([URING], [liburing >= 2.5])
PHP_EVAL_LIBLINE($URING_LIBS, SWOOLE_SHARED_LIBADD)
PHP_EVAL_INCLINE($URING_CFLAGS)
AC_DEFINE(SW_USE_IOURING, 1, [have io_uring])

LINUX_VERSION=`uname -r | cut -d '-' -f 1`
LINUX_MAJOR_VERSION=`echo $LINUX_VERSION | cut -d '.' -f 1`
LINUX_MINIO_VERSION=`echo $LINUX_VERSION | cut -d '.' -f 2`
LINUX_MINOR_VERSION=`echo $LINUX_VERSION | cut -d '.' -f 2`

_PKG_CONFIG(URING_VERSION, [modversion], [liburing])
IOURING_MAJOR_VERSION=`echo $pkg_cv_URING_VERSION | cut -d '.' -f 1`
IOURING_MINOR_VERSION=`echo $pkg_cv_URING_VERSION | cut -d '.' -f 2`

if test $IOURING_MAJOR_VERSION > 2 || (test $IOURING_MAJOR_VERSION = 2 && test $IOURING_MINOR_VERSION >= 6); then
if test $LINUX_MAJOR_VERSION > 6 || (test $LINUX_MAJOR_VERSION = 6 && test $LINUX_MAJOR_VERSION >= 7); then
AC_MSG_CHECKING([checking for io_uring futex feature])
if test $IOURING_MAJOR_VERSION -gt 2 || (test $IOURING_MAJOR_VERSION -eq 2 && test $IOURING_MINOR_VERSION -ge 6); then
if test $LINUX_MAJOR_VERSION -gt 6 || (test $LINUX_MAJOR_VERSION -eq 6 && test $LINUX_MINOR_VERSION -ge 7); then
AC_MSG_RESULT(yes)
AC_DEFINE(HAVE_IOURING_FUTEX, 1, [have io_uring futex feature])
else
AC_MSG_RESULT([no, Linux $LINUX_VERSION is too old])
fi
else
AC_MSG_RESULT([no, liburing $IOURING_MAJOR_VERSION.$IOURING_MINOR_VERSION is too old])
fi
fi

Expand Down
32 changes: 20 additions & 12 deletions ext-src/php_swoole_library.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
+----------------------------------------------------------------------+
*/

/* $Id: 6f6113a57c450c84e00246f2d3c15cf1e7f1f692 */
/* $Id: 1bb86a633f720da45a4d4347f23ff7755c968221 */

#ifndef SWOOLE_LIBRARY_H
#define SWOOLE_LIBRARY_H
Expand Down Expand Up @@ -97,9 +97,11 @@ static const char* swoole_library_source_std_exec =
"\n"
"declare(strict_types=1);\n"
"\n"
"use Swoole\\Coroutine\\System;\n"
"\n"
"function swoole_exec(string $command, &$output = null, &$returnVar = null)\n"
"{\n"
" $result = Swoole\\Coroutine::exec($command);\n"
" $result = System::exec($command);\n"
" if ($result) {\n"
" $outputList = explode(PHP_EOL, $result['output']);\n"
" foreach ($outputList as &$value) {\n"
Expand All @@ -122,7 +124,7 @@ static const char* swoole_library_source_std_exec =
"\n"
"function swoole_shell_exec(string $cmd)\n"
"{\n"
" $result = Swoole\\Coroutine::exec($cmd);\n"
" $result = System::exec($cmd);\n"
" if ($result && $result['output'] !== '') {\n"
" return $result['output'];\n"
" }\n"
Expand Down Expand Up @@ -259,6 +261,10 @@ static const char* swoole_library_source_core_constant =
"\n"
" public const OPTION_IOURING_ENTRIES = 'iouring_entries';\n"
"\n"
" public const OPTION_IOURING_WORKERS = 'iouring_workers';\n"
"\n"
" public const OPTION_IOURING_FLAG = 'iouring_flag';\n"
"\n"
" public const OPTION_ENABLE_SIGNALFD = 'enable_signalfd';\n"
"\n"
" public const OPTION_WAIT_SIGNAL = 'wait_signal';\n"
Expand Down Expand Up @@ -3716,9 +3722,9 @@ static const char* swoole_library_source_core_curl_handler =
"\n"
"namespace Swoole\\Curl;\n"
"\n"
"use Swoole;\n"
"use Swoole\\Constant;\n"
"use Swoole\\Coroutine\\Http\\Client;\n"
"use Swoole\\Coroutine\\System;\n"
"use Swoole\\Curl\\Exception as CurlException;\n"
"use Swoole\\Http\\Status;\n"
"\n"
Expand Down Expand Up @@ -4420,7 +4426,7 @@ static const char* swoole_library_source_core_curl_handler =
" }\n"
"\n"
" if (!filter_var($proxy, FILTER_VALIDATE_IP)) {\n"
" $ip = Swoole\\Coroutine::gethostbyname($proxy, AF_INET, $this->clientOptions['connect_timeout'] ?? -1);\n"
" $ip = System::gethostbyname($proxy, AF_INET, $this->clientOptions['connect_timeout'] ?? -1);\n"
" if (!$ip) {\n"
" $this->setError(CURLE_COULDNT_RESOLVE_PROXY, 'Could not resolve proxy: ' . $proxy);\n"
" return false;\n"
Expand Down Expand Up @@ -9186,6 +9192,7 @@ static const char* swoole_library_source_core_thread_pool =
" private object $running;\n"
"\n"
" private object $queue;\n"
"\n"
" private array $indexes = [];\n"
"\n"
" public function __construct(string $runnableClass, int $threadNum)\n"
Expand All @@ -9197,7 +9204,7 @@ static const char* swoole_library_source_core_thread_pool =
" $this->threadNum = $threadNum;\n"
" }\n"
"\n"
" public function withArguments(array $arguments): static\n"
" public function withArguments(...$arguments): static\n"
" {\n"
" $this->arguments = $arguments;\n"
" return $this;\n"
Expand All @@ -9218,7 +9225,7 @@ static const char* swoole_library_source_core_thread_pool =
" /**\n"
" * @throws \\ReflectionException\n"
" */\n"
" public function start(array $arguments = []): void\n"
" public function start(): void\n"
" {\n"
" if (empty($this->classDefinitionFile) and class_exists($this->runnableClass, false)) {\n"
" $file = (new \\ReflectionClass($this->runnableClass))->getFileName();\n"
Expand Down Expand Up @@ -9283,11 +9290,11 @@ static const char* swoole_library_source_core_thread_pool =
"\n"
" while ($this->running->get()) {\n"
" $threadId = $this->queue->pop(-1);\n"
" $thread = $this->threads[$threadId];\n"
" $index = $this->indexes[$threadId];\n"
" $thread = $this->threads[$threadId];\n"
" $index = $this->indexes[$threadId];\n"
" $thread->join();\n"
" unset($this->threads[$threadId]);\n"
" unset($this->indexes[$threadId]);\n"
" unset($this->threads[$threadId], $this->indexes[$threadId]);\n"
"\n"
" $this->createThread($index);\n"
" }\n"
"\n"
Expand Down Expand Up @@ -9370,12 +9377,13 @@ static const char* swoole_library_source_core_thread_runnable =
"abstract class Runnable\n"
"{\n"
" protected Atomic $running;\n"
"\n"
" protected int $id;\n"
"\n"
" public function __construct($running, $index)\n"
" {\n"
" $this->running = $running;\n"
" $this->id = $index;\n"
" $this->id = $index;\n"
" }\n"
"\n"
" abstract public function run(array $args): void;\n"
Expand Down
2 changes: 1 addition & 1 deletion ext-src/stubs/php_swoole_coroutine_system.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static function readFile(string $filename, int $flag = 0): false|string {
public static function writeFile(string $filename, string $fileContent, int $flags = 0): false|int {}
public static function wait(float $timeout = -1): array|false {}
public static function waitPid(int $pid, float $timeout = -1): array|false {}
public static function waitSignal(int $signo, float $timeout = -1): bool {}
public static function waitSignal(int|array $signals, float $timeout = -1): bool {}
public static function waitEvent(mixed $socket, int $events = SWOOLE_EVENT_READ, float $timeout = -1): int|false {}
}
}
4 changes: 2 additions & 2 deletions ext-src/stubs/php_swoole_coroutine_system_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 8c900b375f6550820f059936b75e7a80beb088f5 */
* Stub hash: 59e0a163279f6b7bf53de6f8f50a8ba4820ccf3b */

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_class_Swoole_Coroutine_System_gethostbyname, 0, 1, MAY_BE_FALSE|MAY_BE_STRING)
ZEND_ARG_TYPE_INFO(0, domain_name, IS_STRING, 0)
Expand Down Expand Up @@ -56,7 +56,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_class_Swoole_Coroutine_System_wa
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Coroutine_System_waitSignal, 0, 1, _IS_BOOL, 0)
ZEND_ARG_TYPE_INFO(0, signo, IS_LONG, 0)
ZEND_ARG_TYPE_MASK(0, signals, MAY_BE_LONG|MAY_BE_ARRAY, NULL)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, timeout, IS_DOUBLE, 0, "-1")
ZEND_END_ARG_INFO()

Expand Down
20 changes: 16 additions & 4 deletions ext-src/swoole_coroutine_system.cc
Original file line number Diff line number Diff line change
Expand Up @@ -335,20 +335,32 @@ PHP_METHOD(swoole_coroutine_system, waitPid) {

PHP_METHOD(swoole_coroutine_system, waitSignal) {
SW_MUST_BE_MAIN_THREAD();
zend_long signo;
zval *zsignals;
double timeout = -1;

ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_LONG(signo)
Z_PARAM_ZVAL(zsignals)
Z_PARAM_OPTIONAL
Z_PARAM_DOUBLE(timeout)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);

if (!System::wait_signal(signo, timeout)) {
std::vector<int> signals;

if (ZVAL_IS_ARRAY(zsignals)) {
zval *item;
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(zsignals), item) {
signals.push_back(zval_get_long(item));
}
ZEND_HASH_FOREACH_END();
} else {
signals.push_back(zval_get_long(zsignals));
}

if (!System::wait_signal(signals, timeout)) {
if (swoole_get_last_error() == EBUSY) {
php_swoole_fatal_error(E_WARNING, "Unable to wait signal, async signal listener has been registered");
} else if (swoole_get_last_error() == EINVAL) {
php_swoole_fatal_error(E_WARNING, "Invalid signal [" ZEND_LONG_FMT "]", signo);
php_swoole_fatal_error(E_WARNING, "Invalid signal in the given list");
}
errno = swoole_get_last_error();
RETURN_FALSE;
Expand Down
45 changes: 15 additions & 30 deletions ext-src/swoole_http_response.cc
Original file line number Diff line number Diff line change
Expand Up @@ -750,17 +750,6 @@ void HttpContext::write(zval *zdata, zval *return_value) {
}

void HttpContext::end(zval *zdata, zval *return_value) {
struct {
char *str;
size_t length;
} http_body;
if (zdata) {
http_body.length = php_swoole_get_send_data(zdata, &http_body.str);
} else {
http_body.length = 0;
http_body.str = nullptr;
}

if (send_chunked) {
if (zdata && Z_STRLEN_P(zdata) > 0) {
zval retval;
Expand All @@ -782,6 +771,9 @@ void HttpContext::end(zval *zdata, zval *return_value) {
}
send_chunked = 0;
} else {
char *data = nullptr;
size_t length = zdata ? php_swoole_get_send_data(zdata, &data) : 0;

String *http_buffer = get_write_buffer();
http_buffer->clear();

Expand Down Expand Up @@ -812,39 +804,32 @@ void HttpContext::end(zval *zdata, zval *return_value) {
}
#endif

build_header(http_buffer, http_body.str, http_body.length);
build_header(http_buffer, data, length);

char *send_body_str;
size_t send_body_len;

if (http_body.length > 0) {
if (length > 0) {
#ifdef SW_HAVE_COMPRESSION
if (content_compressed) {
send_body_str = zlib_buffer->str;
send_body_len = zlib_buffer->length;
} else
#endif
{
send_body_str = http_body.str;
send_body_len = http_body.length;
data = zlib_buffer->str;
length = zlib_buffer->length;
}
#endif
// send twice to reduce memory copy
if (send_body_len < swoole_pagesize()) {
if (http_buffer->append(send_body_str, send_body_len) < 0) {
send_header_ = 0;
RETURN_FALSE;
}
} else {
if (length > SW_HTTP_MAX_APPEND_DATA) {
if (!send(this, http_buffer->str, http_buffer->length)) {
send_header_ = 0;
RETURN_FALSE;
}
if (!send(this, send_body_str, send_body_len)) {
if (!send(this, data, length)) {
end_ = 1;
close(this);
RETURN_FALSE;
}
goto _skip_copy;
} else {
if (http_buffer->append(data, length) < 0) {
send_header_ = 0;
RETURN_FALSE;
}
}
}

Expand Down
Loading

0 comments on commit a7d6bfc

Please sign in to comment.