Skip to content

Commit 224904d

Browse files
authored
Optimize http2 client close (#4957)
* add socket_dtor * fix tests [2] * fix tests [3] * revert * fix 4 * fix tests [5] * fix tests [6] * fix tests [7], optimize hook socket, use std::shared_ptr * fix core tests * fix tests [8] * optimize coroutine_poll * fix tests[9] * fix tests[10]
1 parent 62239db commit 224904d

26 files changed

+724
-475
lines changed

core-tests/src/coroutine/hook.cpp

+133-1
Original file line numberDiff line numberDiff line change
@@ -333,8 +333,9 @@ TEST(coroutine_hook, exists) {
333333
const int fd = 100; // fake fd
334334
ASSERT_EQ(swoole_coroutine_socket_create(fd), 0);
335335
ASSERT_TRUE(swoole_coroutine_socket_exists(fd));
336-
Socket *sock = swoole_coroutine_get_socket_object(fd);
336+
auto sock = swoole_coroutine_get_socket_object(fd);
337337
ASSERT_EQ(sock->get_fd(), fd);
338+
swoole_coroutine_close(fd);
338339
});
339340
}
340341

@@ -477,3 +478,134 @@ TEST(coroutine_hook, socket_close) {
477478
ASSERT_TRUE(results["read"]);
478479
});
479480
}
481+
482+
TEST(coroutine_hook, poll) {
483+
coroutine::run([&](void *arg) {
484+
auto pair = create_socket_pair();
485+
486+
auto buffer = sw_tg_buffer();
487+
buffer->clear();
488+
buffer->append_random_bytes(256 * 1024, false);
489+
490+
std::map<std::string, bool> results;
491+
auto _sock0 = pair.first;
492+
auto _fd0 = _sock0->move_fd();
493+
swoole_coroutine_socket_create(_fd0);
494+
495+
auto _sock1 = pair.second;
496+
auto _fd1 = _sock1->move_fd();
497+
swoole_coroutine_socket_create(_fd1);
498+
499+
Coroutine::create([&](void *) {
500+
ssize_t result;
501+
result = swoole_coroutine_write(_fd0, buffer->value(), buffer->get_length());
502+
ASSERT_GT(result, 0);
503+
System::sleep(0.01);
504+
result = swoole_coroutine_write(_fd1, buffer->value(), 16 * 1024);
505+
ASSERT_GT(result, 0);
506+
});
507+
508+
struct pollfd fds[2];
509+
char buf[4096];
510+
511+
bzero(fds, sizeof(pollfd));
512+
fds[0].fd = _fd0;
513+
fds[0].events = POLLIN;
514+
fds[1].fd = _fd1;
515+
fds[1].events = POLLIN;
516+
517+
ASSERT_EQ(swoole_coroutine_poll(fds, 2, 1000), 1);
518+
ASSERT_TRUE(fds[1].revents & POLLIN);
519+
520+
ssize_t result = swoole_coroutine_read(_fd1, buf, sizeof(buf));
521+
ASSERT_GT(result, 1024);
522+
523+
System::sleep(0.02);
524+
525+
bzero(fds, sizeof(pollfd));
526+
fds[0].fd = _fd0;
527+
fds[0].events = POLLIN;
528+
fds[1].fd = _fd1;
529+
fds[1].events = POLLIN;
530+
531+
ASSERT_EQ(swoole_coroutine_poll(fds, 2, 1000), 2);
532+
ASSERT_TRUE(fds[0].revents & POLLIN);
533+
ASSERT_TRUE(fds[1].revents & POLLIN);
534+
result = swoole_coroutine_read(_fd0, buf, sizeof(buf));
535+
ASSERT_GT(result, 1024);
536+
result = swoole_coroutine_read(_fd1, buf, sizeof(buf));
537+
ASSERT_GT(result, 1024);
538+
539+
System::sleep(0.02);
540+
541+
bzero(fds, sizeof(pollfd));
542+
fds[0].fd = _fd0;
543+
fds[0].events = POLLIN | POLLOUT;
544+
fds[1].fd = _fd1;
545+
fds[1].events = POLLIN | POLLOUT;
546+
547+
ASSERT_EQ(swoole_coroutine_poll(fds, 2, 1000), 2);
548+
ASSERT_TRUE(fds[0].revents & POLLIN);
549+
ASSERT_TRUE(fds[1].revents & POLLIN);
550+
ASSERT_FALSE(fds[0].revents & POLLOUT); // not writable
551+
ASSERT_TRUE(fds[1].revents & POLLOUT);
552+
result = swoole_coroutine_read(_fd0, buf, sizeof(buf));
553+
ASSERT_GT(result, 1024);
554+
result = swoole_coroutine_read(_fd1, buf, sizeof(buf));
555+
ASSERT_GT(result, 1024);
556+
});
557+
}
558+
559+
TEST(coroutine_hook, poll_fake) {
560+
coroutine::run([&](void *arg) {
561+
auto pair = create_socket_pair();
562+
563+
auto buffer = sw_tg_buffer();
564+
buffer->clear();
565+
buffer->append_random_bytes(256 * 1024, false);
566+
567+
std::map<std::string, bool> results;
568+
auto _sock0 = pair.first;
569+
auto _fd0 = _sock0->move_fd();
570+
swoole_coroutine_socket_create(_fd0);
571+
572+
auto _sock1 = pair.second;
573+
auto _fd1 = _sock1->move_fd();
574+
swoole_coroutine_socket_create(_fd1);
575+
576+
Coroutine::create([&](void *) {
577+
ssize_t result;
578+
result = swoole_coroutine_write(_fd0, buffer->value(), buffer->get_length());
579+
ASSERT_GT(result, 0);
580+
System::sleep(0.01);
581+
result = swoole_coroutine_write(_fd1, buffer->value(), 16 * 1024);
582+
ASSERT_GT(result, 0);
583+
});
584+
585+
struct pollfd fds[2];
586+
char buf[4096];
587+
588+
bzero(fds, sizeof(pollfd));
589+
fds[0].fd = _fd1;
590+
fds[0].events = POLLIN;
591+
592+
ASSERT_EQ(swoole_coroutine_poll_fake(fds, 1, 1000), 1);
593+
ASSERT_TRUE(fds[0].revents & POLLIN);
594+
595+
ssize_t result = swoole_coroutine_read(_fd1, buf, sizeof(buf));
596+
ASSERT_GT(result, 1024);
597+
598+
bzero(fds, sizeof(pollfd));
599+
ASSERT_EQ(swoole_coroutine_poll_fake(fds, 2, 1000), -1);
600+
ASSERT_EQ(swoole_get_last_error(), SW_ERROR_INVALID_PARAMS);
601+
602+
System::sleep(0.02);
603+
604+
bzero(fds, sizeof(pollfd));
605+
fds[0].fd = _fd0;
606+
fds[0].events = POLLIN | POLLOUT;
607+
ASSERT_EQ(swoole_coroutine_poll_fake(fds, 1, 1000), 1);
608+
ASSERT_TRUE(fds[0].revents & POLLIN);
609+
ASSERT_TRUE(fds[0].revents & POLLOUT);
610+
});
611+
}

ext-src/php_swoole_cxx.h

+2
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ SW_API bool php_swoole_socket_set_ssl(swoole::coroutine::Socket *sock, zval *zse
9797
#endif
9898
SW_API bool php_swoole_socket_set_protocol(swoole::coroutine::Socket *sock, zval *zset);
9999
SW_API bool php_swoole_socket_set(swoole::coroutine::Socket *cli, zval *zset);
100+
SW_API void php_swoole_socket_set_error_properties(zval *zobject, int code, const char *msg);
101+
SW_API void php_swoole_socket_set_error_properties(zval *zobject, swoole::coroutine::Socket *socket);
100102
#define php_swoole_client_set php_swoole_socket_set
101103
SW_API php_stream *php_swoole_create_stream_from_socket(php_socket_t _fd,
102104
int domain,

0 commit comments

Comments
 (0)