Skip to content

Commit

Permalink
Optimize server code
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed Sep 12, 2024
1 parent fa56140 commit 44688fc
Show file tree
Hide file tree
Showing 14 changed files with 87 additions and 61 deletions.
23 changes: 15 additions & 8 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
*.loT
*.pid
*.dep
/Debug/*
modules/*
/.deps
/.libs/
/core
/examples/core
/Debug
/Release
/CMakeFiles
/cmake_install.cmake
/CMakeCache.txt
Expand Down Expand Up @@ -80,23 +80,32 @@ cmake-build-debug/
/.vs
/configure.in
/configure.ac

# core-tests
/core-tests/CMakeCache.txt
/core-tests/CMakeFiles/
/core-tests/Makefile
/core-tests/bin/
/core-tests/cmake_install.cmake
/core-tests/samples/Makefile
/core-tests/samples/cmake_install\.cmake
/core-tests/samples/CMakeCache\.txt
/core-tests/samples/bin/
/core-tests/samples/CMakeFiles/
/core-tests/.project
/core-tests/.cproject
/core-tests/compile_commands.json
/core-tests/samples/.project
/core-tests/fuzz/fuzz_results/
/core-tests/fuzz/bin/

/tools/vendor
/tools/composer.lock
/examples/wrapper/CMakeFiles
/examples/wrapper/CMakeCache.txt
/examples/wrapper/Makefile
/examples/wrapper/server
/examples/wrapper/cmake_install.cmake
core-tests/samples/Makefile
core-tests/samples/cmake_install\.cmake
core-tests/samples/CMakeCache\.txt
core-tests/samples/bin/
core-tests/samples/CMakeFiles/
/out
/gcov_result

Expand All @@ -108,6 +117,4 @@ core-tests/samples/CMakeFiles/
/html
/tests/include/lib/vendor/
/tests/include/lib/composer.lock
/core-tests/fuzz/fuzz_results/
/core-tests/fuzz/bin/
/scripts/data
4 changes: 2 additions & 2 deletions core-tests/src/os/process_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ static void test_func(ProcessPool &pool) {
ASSERT_EQ(pool.dispatch_blocking(&data, &worker_id), SW_OK);

pool.running = true;
pool.onTask = [](ProcessPool *pool, EventData *task) -> int {
pool.onTask = [](ProcessPool *pool, Worker *worker, EventData *task) -> int {
pool->running = false;
EXPECT_MEMEQ(task->data, TEST_JPG_MD5SUM, task->info.len);
return 0;
Expand Down Expand Up @@ -93,7 +93,7 @@ TEST(process_pool, shutdown) {
usleep(1);
};

pool.onTask = [](ProcessPool *pool, EventData *task) -> int {
pool.onTask = [](ProcessPool *pool, Worker *worker, EventData *task) -> int {
kill(pool->master_pid, SIGTERM);

return 0;
Expand Down
2 changes: 1 addition & 1 deletion ext-src/php_swoole_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

typedef uint32_t ThreadResourceId;
class ThreadResource;
struct ZendArray;
class ZendArray;

extern zend_class_entry *swoole_thread_ce;
extern zend_class_entry *swoole_thread_error_ce;
Expand Down
3 changes: 2 additions & 1 deletion include/swoole_process_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ struct Worker {
void *ptr2;

ssize_t send_pipe_message(const void *buf, size_t n, int flags);
bool has_exceeded_max_request();

void set_status(enum swWorkerStatus _status) {
status = _status;
Expand Down Expand Up @@ -232,7 +233,7 @@ struct ProcessPool {
uint8_t scheduler_warning;
time_t warning_time;

int (*onTask)(ProcessPool *pool, EventData *task);
int (*onTask)(ProcessPool *pool, Worker *worker, EventData *task);
void (*onWorkerStart)(ProcessPool *pool, Worker *worker);
void (*onMessage)(ProcessPool *pool, RecvData *msg);
void (*onWorkerStop)(ProcessPool *pool, Worker *worker);
Expand Down
8 changes: 8 additions & 0 deletions include/swoole_static_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ class StaticHandler {
return S_ISDIR(file_stat.st_mode);
}

bool is_link() {
return S_ISLNK(file_stat.st_mode);
}

bool is_file() {
return S_ISREG(file_stat.st_mode);
}

size_t get_content_length() {
return content_length;
}
Expand Down
28 changes: 14 additions & 14 deletions src/os/process_pool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -526,11 +526,9 @@ static int ProcessPool_worker_loop_with_task_protocol(ProcessPool *pool, Worker
EventData buf;
} out{};

ssize_t n = 0, ret, worker_task_always = 0;
int task_n = pool->get_max_request();
if (task_n <= 0) {
worker_task_always = 1;
task_n = 1;
ssize_t n = 0, ret;
if (pool->get_max_request() <= 0) {
SwooleWG.run_always = 1;
}

/**
Expand All @@ -544,7 +542,7 @@ static int ProcessPool_worker_loop_with_task_protocol(ProcessPool *pool, Worker
out.mtype = worker->id + 1;
}

while (pool->running && !SwooleWG.shutdown && task_n > 0) {
while (pool->running && !SwooleWG.shutdown) {
/**
* fetch task
*/
Expand Down Expand Up @@ -596,12 +594,7 @@ static int ProcessPool_worker_loop_with_task_protocol(ProcessPool *pool, Worker
continue;
}

/**
* do task
*/
worker->status = SW_WORKER_BUSY;
ret = pool->onTask(pool, &out.buf);
worker->status = SW_WORKER_IDLE;
ret = pool->onTask(pool, worker, &out.buf);

if (pool->use_socket && pool->stream_info_->last_connection) {
int _end = 0;
Expand All @@ -617,8 +610,8 @@ static int ProcessPool_worker_loop_with_task_protocol(ProcessPool *pool, Worker
goto _alarm_handler;
}

if (ret >= 0 && !worker_task_always) {
task_n--;
if (worker->has_exceeded_max_request()) {
break;
}
}
return SW_OK;
Expand Down Expand Up @@ -763,6 +756,10 @@ static int ProcessPool_worker_loop_with_stream_protocol(ProcessPool *pool, Worke
if (SwooleG.signal_alarm) {
goto _alarm_handler;
}

if (worker->has_exceeded_max_request()) {
break;
}
}
return SW_OK;
}
Expand Down Expand Up @@ -803,6 +800,9 @@ static int ProcessPool_worker_loop_with_message_protocol(ProcessPool *pool, Work
swoole_sys_warning("failed to read data from pipe");
return SW_OK;
}
if (worker->has_exceeded_max_request()) {
break;
}
}

return SW_OK;
Expand Down
14 changes: 10 additions & 4 deletions src/server/manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ struct Manager {

void wait(Server *_server);
void add_timeout_killer(Worker *workers, int n);
void terminate_all_worker();

static void signal_handler(int sig);
static void timer_callback(Timer *timer, TimerNode *tnode);
Expand Down Expand Up @@ -405,6 +406,14 @@ void Manager::wait(Server *_server) {
}
}

void Manager::terminate_all_worker() {
// clear the timer
alarm(0);
for (auto i = kill_workers.begin(); i != kill_workers.end(); i++) {
swoole_kill(*i, SIGKILL);
}
}

void Manager::signal_handler(int signo) {
Server *_server = sw_server();
if (!_server || !_server->manager) {
Expand All @@ -428,10 +437,7 @@ void Manager::signal_handler(int signo) {
case SIGALRM:
SwooleG.signal_alarm = 1;
if (manager->force_kill) {
alarm(0);
for (auto i = manager->kill_workers.begin(); i != manager->kill_workers.end(); i++) {
swoole_kill(*i, SIGKILL);
}
manager->terminate_all_worker();
}
break;
default:
Expand Down
2 changes: 1 addition & 1 deletion src/server/port.cc
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ bool ListenPort::ssl_create_context(SSLContext *context) {
context->http_v2 = 1;
}
if (!context->create()) {
swoole_warning("swSSL_get_context() error");
swoole_warning("failed to create ssl content");
return false;
}
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/server/process.cc
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ pid_t Factory::spawn_user_worker(Worker *worker) {
server_->user_worker_map.erase(worker->pid);
}
if (pid < 0) {
swoole_sys_warning("Fork Worker failed");
swoole_sys_warning("failed to spawn the user worker");
return SW_ERR;
}
// child
Expand Down
2 changes: 1 addition & 1 deletion src/server/reactor_process.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ static int ReactorProcess_onClose(Reactor *reactor, Event *event);
static void ReactorProcess_onTimeout(Timer *timer, TimerNode *tnode);

int Server::start_reactor_processes() {
single_thread = 1;
single_thread = true;

// listen TCP
if (have_stream_sock == 1) {
Expand Down
2 changes: 1 addition & 1 deletion src/server/reactor_thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ static inline ReturnCode ReactorThread_verify_ssl_state(Reactor *reactor, Listen
*/
static int ReactorThread_onPacketReceived(Reactor *reactor, Event *event) {
int fd = event->fd;
int ret;
ssize_t ret;

Server *serv = (Server *) reactor->ptr;
Connection *server_sock = serv->get_connection(fd);
Expand Down
20 changes: 10 additions & 10 deletions src/server/static_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ bool StaticHandler::is_modified(const std::string &date_if_modified_since) {
} else if (strptime(date_tmp, SW_HTTP_ASCTIME_DATE, &tm3) != nullptr) {
date_format = SW_HTTP_ASCTIME_DATE;
}
return date_format && mktime(&tm3) - (int) serv->timezone_ >= get_file_mtime();
return date_format && mktime(&tm3) - (time_t) serv->timezone_ >= get_file_mtime();
}

bool StaticHandler::is_modified_range(const std::string &date_range) {
Expand Down Expand Up @@ -167,7 +167,7 @@ bool StaticHandler::hit() {
}
}

if (S_ISLNK(file_stat.st_mode)) {
if (is_link()) {
char buf[PATH_MAX];
ssize_t byte = ::readlink(filename, buf, sizeof(buf) - 1);
if (byte <= 0) {
Expand All @@ -186,11 +186,11 @@ bool StaticHandler::hit() {
return true;
}

if (!swoole::mime_type::exists(filename) && !last) {
if (!mime_type::exists(filename) && !last) {
return false;
}

if (!S_ISREG(file_stat.st_mode)) {
if (!is_file()) {
return false;
}

Expand Down Expand Up @@ -271,23 +271,23 @@ bool StaticHandler::get_dir_files() {
return true;
}

bool StaticHandler::set_filename(const std::string &filename) {
char *p = this->filename + l_filename;
bool StaticHandler::set_filename(const std::string &_filename) {
char *p = filename + l_filename;

if (*p != '/') {
*p = '/';
p += 1;
}

memcpy(p, filename.c_str(), filename.length());
p += filename.length();
memcpy(p, _filename.c_str(), _filename.length());
p += _filename.length();
*p = 0;

if (lstat(this->filename, &file_stat) < 0) {
if (lstat(filename, &file_stat) < 0) {
return false;
}

if (!S_ISREG(file_stat.st_mode)) {
if (!is_file()) {
return false;
}

Expand Down
Loading

0 comments on commit 44688fc

Please sign in to comment.