Skip to content

Commit cc6afa5

Browse files
committed
Optimize Worker::init() & Worker::set_max_request()
1 parent 3b5c682 commit cc6afa5

File tree

6 files changed

+16
-20
lines changed

6 files changed

+16
-20
lines changed

core-tests/src/os/process_pool.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ static void test_func(ProcessPool &pool) {
2424

2525
pool.running = true;
2626
pool.ptr = &rmem;
27-
SwooleWG.run_always = true;
2827
pool.main_loop(&pool, pool.get_worker(0));
2928
pool.destroy();
3029
}

core-tests/src/server/server.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,6 @@ TEST(server, task_worker) {
558558
ASSERT_EQ(serv.create_task_workers(), SW_OK);
559559

560560
thread t1([&serv]() {
561-
SwooleWG.run_always = true;
562561
serv.gs->task_workers.running = 1;
563562
serv.gs->task_workers.main_loop(&serv.gs->task_workers, &serv.gs->task_workers.workers[0]);
564563
EXPECT_EQ(serv.get_tasking_num(), 0);

include/swoole_process_pool.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ struct ProcessPool;
103103
struct Worker;
104104

105105
struct WorkerGlobal {
106-
bool run_always;
107106
bool shutdown;
108107
bool running;
109108
uint32_t max_request;
@@ -163,7 +162,11 @@ struct Worker {
163162
bool has_exceeded_max_request();
164163
void set_max_request(uint32_t max_request, uint32_t max_request_grace);
165164
void report_error(const ExitStatus &exit_status);
166-
void start();
165+
/**
166+
* Init global state for worker process.
167+
* Must be called after the process is spawned and before the main loop is executed.
168+
*/
169+
void init();
167170
void shutdown();
168171
bool is_shutdown();
169172
bool is_running();

src/os/process_pool.cc

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -452,11 +452,11 @@ pid_t ProcessPool::spawn(Worker *worker) {
452452
switch (pid) {
453453
// child
454454
case 0:
455+
worker->init();
455456
worker->pid = SwooleG.pid;
456457
swoole_set_process_type(SW_PROCESS_WORKER);
457458
swoole_set_process_id(worker->id);
458459
SwooleWG.worker = worker;
459-
SwooleWG.run_always = true;
460460
if (async) {
461461
if (swoole_event_init(SW_EVENTLOOP_WAIT_EXIT) < 0) {
462462
exit(254);
@@ -987,11 +987,7 @@ void ProcessPool::destroy() {
987987
sw_mem_pool()->free(workers);
988988
}
989989

990-
bool Worker::has_exceeded_max_request() {
991-
return !SwooleWG.run_always && request_count >= SwooleWG.max_request;
992-
}
993-
994-
void Worker::start() {
990+
void Worker::init() {
995991
start_time = ::time(nullptr);
996992
request_count = 0;
997993
set_status_to_idle();
@@ -1000,15 +996,14 @@ void Worker::start() {
1000996
}
1001997

1002998
void Worker::set_max_request(uint32_t max_request, uint32_t max_request_grace) {
1003-
if (max_request < 1) {
1004-
SwooleWG.run_always = true;
1005-
} else {
1006-
SwooleWG.run_always = false;
1007-
SwooleWG.max_request = max_request;
1008-
if (max_request_grace > 0) {
1009-
SwooleWG.max_request += swoole_system_random(1, max_request_grace);
1010-
}
999+
if (max_request > 0 && max_request_grace > 0) {
1000+
max_request += swoole_system_random(1, max_request_grace);
10111001
}
1002+
SwooleWG.max_request = max_request;
1003+
}
1004+
1005+
bool Worker::has_exceeded_max_request() {
1006+
return SwooleWG.max_request > 0 && request_count >= SwooleWG.max_request;
10121007
}
10131008

10141009
void Worker::shutdown() {

src/server/master.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ void Server::destroy_worker(Worker *worker) {
593593
* [Worker]
594594
*/
595595
void Server::init_event_worker(Worker *worker) {
596-
worker->start();
596+
worker->init();
597597
worker->set_max_request(max_request, max_request_grace);
598598
}
599599

src/server/task_worker.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ static void TaskWorker_onStart(ProcessPool *pool, Worker *worker) {
259259
TaskWorker_signal_init(pool);
260260
serv->worker_start_callback(worker);
261261

262-
worker->start();
262+
worker->init();
263263
worker->set_max_request(pool->max_request, pool->max_request_grace);
264264
}
265265

0 commit comments

Comments
 (0)