-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Copy the server settings before launching the worker threads, fixed #…
- Loading branch information
Showing
10 changed files
with
215 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#!/bin/bash | ||
|
||
changed_files=$(git status --porcelain | grep '^[ M].*\.cc$' | awk '{print $2}') | ||
|
||
if [ -z "$changed_files" ]; then | ||
exit 0 | ||
fi | ||
|
||
for file in $changed_files; do | ||
echo "format $file" | ||
clang-format -i "$file" | ||
done | ||
|
||
echo "done" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
function thread_server_test_eof_client($port) | ||
{ | ||
$cli = new Co\Client(SWOOLE_SOCK_TCP); | ||
$cli->set([ | ||
'open_eof_check' => true, | ||
'package_eof' => "\r\n", | ||
]); | ||
Assert::assert($cli->connect('127.0.0.1', $port, 2)); | ||
$cli->send(json_encode(['type' => 'eof']) . "\r\n"); | ||
Assert::eq($cli->recv(), "EOF\r\n"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
--TEST-- | ||
swoole_thread/server: listen | ||
--SKIPIF-- | ||
<?php | ||
require __DIR__ . '/../../include/skipif.inc'; | ||
skip_if_nts(); | ||
?> | ||
--FILE-- | ||
<?php | ||
require __DIR__ . '/../../include/bootstrap.php'; | ||
require __DIR__ . '/functions.inc'; | ||
|
||
use Swoole\Thread; | ||
|
||
$port = get_constant_port(__FILE__); | ||
|
||
$serv = new Swoole\Server('127.0.0.1', $port + 1, SWOOLE_THREAD); | ||
$port2 = $serv->listen('127.0.0.1', $port, SWOOLE_SOCK_TCP); | ||
$serv->set(array( | ||
'worker_num' => 2, | ||
'log_level' => SWOOLE_LOG_ERROR, | ||
'open_eof_check' => true, | ||
'package_eof' => "\r\n", | ||
'init_arguments' => function () { | ||
global $queue, $atomic; | ||
$queue = new Swoole\Thread\Queue(); | ||
$atomic = new Swoole\Thread\Atomic(1); | ||
return [$queue, $atomic]; | ||
} | ||
)); | ||
$serv->on('WorkerStart', function (Swoole\Server $serv, $workerId) use ($port) { | ||
[$queue, $atomic] = Thread::getArguments(); | ||
if ($workerId == 0) { | ||
$queue->push("begin\n", Thread\Queue::NOTIFY_ALL); | ||
} | ||
}); | ||
$serv->on('receive', function (Swoole\Server $serv, $fd, $rid, $data) { | ||
$json = json_decode(rtrim($data)); | ||
if ($json->type == 'eof') { | ||
$serv->send($fd, "EOF\r\n"); | ||
} | ||
}); | ||
$serv->on('shutdown', function () { | ||
global $queue, $atomic; | ||
echo 'shutdown', PHP_EOL; | ||
Assert::eq($atomic->get(), 0); | ||
}); | ||
$serv->addProcess(new Swoole\Process(function ($process) use ($serv) { | ||
[$queue, $atomic] = Thread::getArguments(); | ||
global $port; | ||
echo $queue->pop(-1); | ||
Co\run(function () use ($port) { | ||
Co::join([ | ||
Co\go(function () use ($port) { | ||
thread_server_test_eof_client($port); | ||
}), | ||
Co\go(function () use ($port) { | ||
thread_server_test_eof_client($port + 1); | ||
}) | ||
]); | ||
}); | ||
$atomic->set(0); | ||
echo "done\n"; | ||
$serv->shutdown(); | ||
})); | ||
$serv->start(); | ||
?> | ||
--EXPECT-- | ||
begin | ||
done | ||
shutdown |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
--TEST-- | ||
swoole_thread/server: setting | ||
--SKIPIF-- | ||
<?php | ||
require __DIR__ . '/../../include/skipif.inc'; | ||
skip_if_nts(); | ||
?> | ||
--FILE-- | ||
<?php | ||
require __DIR__ . '/../../include/bootstrap.php'; | ||
|
||
use Swoole\Thread; | ||
|
||
$port = get_constant_port(__FILE__); | ||
const N = 4; | ||
|
||
$serv = new Swoole\Server('127.0.0.1', $port, SWOOLE_THREAD); | ||
$serv->set(array( | ||
'worker_num' => N, | ||
'log_level' => SWOOLE_LOG_ERROR, | ||
'init_arguments' => function () { | ||
global $queue, $atomic; | ||
$queue = new Swoole\Thread\Queue(); | ||
$atomic = new Swoole\Thread\Atomic(0); | ||
return [$queue, $atomic]; | ||
} | ||
)); | ||
$serv->on('WorkerStart', function (Swoole\Server $serv, $workerId) use ($port) { | ||
[$queue, $atomic] = Thread::getArguments(); | ||
Assert::isArray($serv->setting); | ||
if ($atomic->add(1) == N) { | ||
$queue->push("begin\n", Thread\Queue::NOTIFY_ALL); | ||
} | ||
}); | ||
$serv->on('receive', function (Swoole\Server $serv, $fd, $rid, $data) { | ||
}); | ||
$serv->on('shutdown', function () { | ||
global $queue, $atomic; | ||
echo 'shutdown', PHP_EOL; | ||
Assert::eq($atomic->get(), N); | ||
}); | ||
$serv->addProcess(new Swoole\Process(function ($process) use ($serv) { | ||
[$queue, $atomic] = Thread::getArguments(); | ||
echo $queue->pop(-1); | ||
echo "done\n"; | ||
$serv->shutdown(); | ||
})); | ||
$serv->start(); | ||
?> | ||
--EXPECT-- | ||
begin | ||
done | ||
shutdown |