Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize create socket #5293

Merged
merged 3 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions ext-src/php_swoole_http_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,35 +48,35 @@ int swoole_http2_server_goaway(swoole::http::Context *ctx,
static inline void http_server_add_server_array(HashTable *ht, zend_string *key, const char *value) {
zval tmp;
ZVAL_STRING(&tmp, value);
zend_hash_add(ht, key, &tmp);
zend_hash_add_new(ht, key, &tmp);
}

static inline void http_server_add_server_array(HashTable *ht, zend_string *key, const char *value, size_t length) {
zval tmp;
ZVAL_STRINGL(&tmp, value, length);
zend_hash_add(ht, key, &tmp);
zend_hash_add_new(ht, key, &tmp);
}

static inline void http_server_add_server_array(HashTable *ht, zend_string *key, zend_long value) {
zval tmp;
ZVAL_LONG(&tmp, value);
zend_hash_add(ht, key, &tmp);
zend_hash_add_new(ht, key, &tmp);
}

static inline void http_server_add_server_array(HashTable *ht, zend_string *key, double value) {
zval tmp;
ZVAL_DOUBLE(&tmp, value);
zend_hash_add(ht, key, &tmp);
zend_hash_add_new(ht, key, &tmp);
}

static inline void http_server_add_server_array(HashTable *ht, zend_string *key, zend_string *value) {
zval tmp;
ZVAL_STR(&tmp, value);
zend_hash_add(ht, key, &tmp);
zend_hash_add_new(ht, key, &tmp);
}

static inline void http_server_add_server_array(HashTable *ht, zend_string *key, zval *value) {
zend_hash_add(ht, key, value);
zend_hash_add_new(ht, key, value);
}

static inline void http_server_set_object_fd_property(zend_object *object, zend_class_entry *ce, long fd) {
Expand Down
4 changes: 4 additions & 0 deletions ext-src/swoole_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,10 @@ static zend_class_entry *swoole_server_task_result_ce;
static zend_object_handlers swoole_server_task_result_handlers;

static SW_THREAD_LOCAL zval swoole_server_instance;
#ifdef SW_THREAD
static SW_THREAD_LOCAL WorkerFn worker_thread_fn;
static SW_THREAD_LOCAL std::vector<ServerPortProperty *> swoole_server_port_properties;
#endif

static sw_inline ServerObject *server_fetch_object(zend_object *obj) {
return (ServerObject *) ((char *) obj - swoole_server_handlers.offset);
Expand Down Expand Up @@ -2372,6 +2374,7 @@ static PHP_METHOD(swoole_server, set) {
zend_long v = zval_get_long(ztmp);
serv->message_queue_key = SW_MAX(0, SW_MIN(v, INT64_MAX));
}
#ifdef SW_THREAD
// bootstrap
if (php_swoole_array_get_value(vht, "bootstrap", ztmp)) {
zend::object_set(ZEND_THIS, ZEND_STRL("bootstrap"), ztmp);
Expand All @@ -2384,6 +2387,7 @@ static PHP_METHOD(swoole_server, set) {
} else {
ZVAL_NULL(&server_object->init_arguments);
}
#endif

if (serv->task_enable_coroutine &&
(serv->task_ipc_mode == Server::TASK_IPC_MSGQUEUE || serv->task_ipc_mode == Server::TASK_IPC_PREEMPTIVE)) {
Expand Down
2 changes: 2 additions & 0 deletions include/swoole_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,8 @@ struct ListenPort {
void close();
bool import(int sock);
const char *get_protocols();
int create_socket(swoole::Server *server);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已在命名空间中,去掉 swoole::

void close_socket_fd();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议修改为 close_socketcreate_socket 命名对称


#ifdef SW_USE_OPENSSL
bool ssl_create_context(SSLContext *context);
Expand Down
25 changes: 1 addition & 24 deletions src/server/master.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1706,29 +1706,6 @@ int Server::add_systemd_socket() {
return count;
}

static bool Server_create_socket(ListenPort *ls) {
ls->socket = make_socket(
ls->type, ls->is_dgram() ? SW_FD_DGRAM_SERVER : SW_FD_STREAM_SERVER, SW_SOCK_CLOEXEC | SW_SOCK_NONBLOCK);
if (!ls->socket) {
return false;
}
#if defined(SW_SUPPORT_DTLS) && defined(HAVE_KQUEUE)
if (ls->is_dtls()) {
ls->socket->set_reuse_port();
}
#endif

if (ls->socket->bind(ls->host, &ls->port) < 0) {
swoole_set_last_error(errno);
ls->socket->free();
return false;
}

ls->socket->info.assign(ls->type, ls->host, ls->port);

return true;
}

ListenPort *Server::add_port(SocketType type, const char *host, int port) {
if (session_list) {
swoole_error_log(SW_LOG_ERROR, SW_ERROR_WRONG_OPERATION, "must add port before server is created");
Expand Down Expand Up @@ -1786,7 +1763,7 @@ ListenPort *Server::add_port(SocketType type, const char *host, int port) {
}
#endif

if (!Server_create_socket(ls)) {
if (ls->create_socket(this) < 0) {
swoole_set_last_error(errno);
return nullptr;
}
Expand Down
52 changes: 52 additions & 0 deletions src/server/port.cc
Original file line number Diff line number Diff line change
Expand Up @@ -792,4 +792,56 @@ size_t ListenPort::get_connection_num() const {
}
}

int ListenPort::create_socket(Server *server) {
if (socket) {
#if defined(__linux__) && defined(HAVE_REUSEPORT)
if (server->enable_reuse_port) {
close_socket_fd();
} else
#endif
{
return SW_OK;
}
}

socket = swoole::make_socket(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可去掉命名空间

type, is_dgram() ? SW_FD_DGRAM_SERVER : SW_FD_STREAM_SERVER, SW_SOCK_CLOEXEC | SW_SOCK_NONBLOCK);
if (socket == nullptr) {
swoole_set_last_error(errno);
return SW_ERR;
}

#if defined(SW_SUPPORT_DTLS) && defined(HAVE_KQUEUE)
if (ls->is_dtls()) {
socket->set_reuse_port();
}
#endif

#if defined(__linux__) && defined(HAVE_REUSEPORT)
if (server->enable_reuse_port) {
if (socket->set_reuse_port() < 0) {
socket->free();
return SW_ERR;
}
}
#endif

if (socket->bind(host, &port) < 0) {
swoole_set_last_error(errno);
socket->free();
return SW_ERR;
}

socket->info.assign(type, host, port);
return SW_OK;
}

void ListenPort::close_socket_fd() {
if (::close(socket->fd) < 0) {
swoole_sys_warning("close(%d) failed", socket->fd);
}
delete socket;
socket = nullptr;
}

} // namespace swoole
56 changes: 17 additions & 39 deletions src/server/reactor_process.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ static int ReactorProcess_onPipeRead(Reactor *reactor, Event *event);
static int ReactorProcess_onClose(Reactor *reactor, Event *event);
static void ReactorProcess_onTimeout(Timer *timer, TimerNode *tnode);

#ifdef HAVE_REUSEPORT
static int ReactorProcess_reuse_port(ListenPort *ls);
#endif

static bool Server_is_single(Server *serv) {
return (serv->worker_num == 1 && serv->task_worker_num == 0 && serv->max_request == 0 &&
serv->user_worker_list.empty());
Expand All @@ -39,24 +35,19 @@ int Server::start_reactor_processes() {
// listen TCP
if (have_stream_sock == 1) {
for (auto ls : ports) {
if (ls->is_dgram()) {
continue;
}
#ifdef HAVE_REUSEPORT
if (enable_reuse_port) {
if (::close(ls->socket->fd) < 0) {
swoole_sys_warning("close(%d) failed", ls->socket->fd);
}
delete ls->socket;
ls->socket = nullptr;
continue;
} else
if (ls->is_stream()) {
#if defined(__linux__) && defined(HAVE_REUSEPORT)
if (!enable_reuse_port) {
#endif
{
// listen server socket
if (ls->listen() < 0) {
return SW_ERR;
// listen server socket
if (ls->listen() < 0) {
return SW_ERR;
}
#if defined(__linux__) && defined(HAVE_REUSEPORT)
} else {
ls->close_socket_fd();
}
#endif
}
}
}
Expand Down Expand Up @@ -194,12 +185,16 @@ int Server::worker_main_loop(ProcessPool *pool, Worker *worker) {
serv->worker_signal_init();

for (auto ls : serv->ports) {
#ifdef HAVE_REUSEPORT
#if defined(__linux__) and defined(HAVE_REUSEPORT)
if (ls->is_stream() && serv->enable_reuse_port) {
if (ReactorProcess_reuse_port(ls) < 0) {
if (ls->create_socket(serv) < 0) {
swoole_event_free();
return SW_ERR;
}

if (ls->listen() < 0) {
return SW_ERR;
}
}
#endif
if (reactor->add(ls->socket, SW_EVENT_READ) < 0) {
Expand Down Expand Up @@ -350,21 +345,4 @@ static void ReactorProcess_onTimeout(Timer *timer, TimerNode *tnode) {
ReactorProcess_onClose(reactor, &notify_ev);
});
}

#ifdef HAVE_REUSEPORT
static int ReactorProcess_reuse_port(ListenPort *ls) {
ls->socket = swoole::make_socket(
ls->type, ls->is_dgram() ? SW_FD_DGRAM_SERVER : SW_FD_STREAM_SERVER, SW_SOCK_CLOEXEC | SW_SOCK_NONBLOCK);
if (ls->socket->set_reuse_port() < 0) {
ls->socket->free();
return SW_ERR;
}
if (ls->socket->bind(ls->host, &ls->port) < 0) {
ls->socket->free();
return SW_ERR;
}
return ls->listen();
}
#endif

} // namespace swoole
Loading