-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Close in non current worker #2891
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
@@ -1,15 +1,35 @@ | ||
<?php | ||
$http = new swoole_http_server("127.0.0.1", 9501, SWOOLE_BASE); | ||
|
||
use Swoole\Http\Server; | ||
use Swoole\Http\Response; | ||
use Swoole\Http\Request; | ||
|
||
$http = new Server("127.0.0.1", 9501, SWOOLE_BASE); | ||
|
||
$pool = []; | ||
|
||
$http->set([ | ||
'worker_num' => 4, | ||
// 'worker_num' => 4, | ||
'hook_flags' => SWOOLE_HOOK_ALL, | ||
]); | ||
|
||
$http->on('request', function ($request, swoole_http_response $response) { | ||
$response->header('Last-Modified', 'Thu, 18 Jun 2015 10:24:27 GMT'); | ||
$response->header('E-Tag', '55829c5b-17'); | ||
$response->header('Accept-Ranges', 'bytes'); | ||
$response->end("<h1>\nHello Swoole.\n</h1>"); | ||
$http->on('request', function (Request $request, Response $response) use (&$pool) { | ||
// var_dump($request->server['request_uri']); | ||
if ($request->server['request_uri'] == '/') { | ||
$response->header('Last-Modified', 'Thu, 18 Jun 2015 10:24:27 GMT'); | ||
$response->header('E-Tag', '55829c5b-17'); | ||
$response->header('Accept-Ranges', 'bytes'); | ||
$response->end("<h1>\nHello Swoole.\n</h1>"); | ||
} elseif ($request->server['request_uri'] == '/redis') { | ||
$redis = new redis; | ||
$redis->connect('127.0.0.1', 6379); | ||
$value = $redis->get('key'); | ||
$redis->close(); | ||
$pool[] = $redis; | ||
$response->end("<h1>Value=" . $value . "</h1>"); | ||
} elseif ($request->server['request_uri'] == '/redis') { | ||
$response->end("<pre>" . var_export($pool, 1) . "</pre>\n"); | ||
} | ||
}); | ||
|
||
$http->start(); |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,68 @@ | ||
--TEST-- | ||
swoole_server: close in non-current worker | ||
--SKIPIF-- | ||
<?php | ||
require __DIR__ . '/../include/skipif.inc'; | ||
?> | ||
--FILE-- | ||
<?php | ||
require __DIR__ . '/../include/bootstrap.php'; | ||
|
||
use Swoole\Server; | ||
use Swoole\Constant; | ||
|
||
$pm = new SwooleTest\ProcessManager; | ||
|
||
$socket = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_DGRAM, 0); | ||
|
||
$pm->parentFunc = function ($pid) use ($pm) { | ||
go(function () use ($pm) { | ||
$client = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP ); | ||
if (!$client->connect('127.0.0.1', $pm->getFreePort())) { | ||
exit("connect failed\n"); | ||
} | ||
$client->send("close"); | ||
$data = $client->recv(); | ||
Assert::string($data); | ||
Assert::length($data, 0); | ||
echo "DONE\n"; | ||
}); | ||
Swoole\Event::wait(); | ||
$pm->kill(); | ||
|
||
global $socket; | ||
$result[] = fgets($socket[1]); | ||
$result[] = fgets($socket[1]); | ||
|
||
Assert::eq($result[0], $result[1]); | ||
}; | ||
|
||
$pm->childFunc = function () use ($pm) { | ||
$serv = new Server('127.0.0.1', $pm->getFreePort(), SWOOLE_PROCESS); | ||
$serv->set([ | ||
'worker_num' => 2, | ||
'log_file' => TEST_LOG_FILE, | ||
]); | ||
$serv->on("workerStart", function ($serv) use ($pm) { | ||
$pm->wakeup(); | ||
}); | ||
$serv->on('receive', function (Server $serv, $fd, $reactor_id, $data) { | ||
global $socket; | ||
fwrite($socket[0], $serv->worker_id."\n"); | ||
$serv->sendMessage(['close_fd' => $fd, 'worker_id' => $serv->worker_id], 1 - $serv->worker_id); | ||
}); | ||
$serv->on(Constant::EVENT_CLOSE, function (Server $serv, $fd, $reactor_id) { | ||
global $socket; | ||
fwrite($socket[0], $serv->worker_id . "\n"); | ||
}); | ||
$serv->on(Constant::EVENT_PIPE_MESSAGE, function (Server $serv, $workerId, $msg) { | ||
$serv->close($msg['close_fd']); | ||
}); | ||
$serv->start(); | ||
}; | ||
|
||
$pm->childFirst(); | ||
$pm->run(); | ||
?> | ||
--EXPECT-- | ||
DONE |
This file contains hidden or 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,69 @@ | ||
--TEST-- | ||
swoole_server: close in task worker | ||
--SKIPIF-- | ||
<?php | ||
require __DIR__ . '/../include/skipif.inc'; | ||
?> | ||
--FILE-- | ||
<?php | ||
require __DIR__ . '/../include/bootstrap.php'; | ||
|
||
use Swoole\Server; | ||
use Swoole\Constant; | ||
|
||
$pm = new SwooleTest\ProcessManager; | ||
|
||
$socket = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_DGRAM, 0); | ||
|
||
$pm->parentFunc = function ($pid) use ($pm) { | ||
go(function () use ($pm) { | ||
$client = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP ); | ||
if (!$client->connect('127.0.0.1', $pm->getFreePort())) { | ||
exit("connect failed\n"); | ||
} | ||
$client->send("close"); | ||
$data = $client->recv(); | ||
Assert::string($data); | ||
Assert::length($data, 0); | ||
echo "DONE\n"; | ||
}); | ||
Swoole\Event::wait(); | ||
$pm->kill(); | ||
|
||
global $socket; | ||
$result[] = fgets($socket[1]); | ||
$result[] = fgets($socket[1]); | ||
|
||
Assert::eq($result[0], $result[1]); | ||
}; | ||
|
||
$pm->childFunc = function () use ($pm) { | ||
$serv = new Server('127.0.0.1', $pm->getFreePort(), SWOOLE_PROCESS); | ||
$serv->set([ | ||
'worker_num' => 1, | ||
'task_worker_num' => 1, | ||
'log_file' => TEST_LOG_FILE, | ||
]); | ||
$serv->on("workerStart", function ($serv) use ($pm) { | ||
$pm->wakeup(); | ||
}); | ||
$serv->on('receive', function (Server $serv, $fd, $reactor_id, $data) { | ||
global $socket; | ||
fwrite($socket[0], $serv->worker_id . "\n"); | ||
$serv->task(['close_fd' => $fd, 'worker_id' => $serv->worker_id]); | ||
}); | ||
$serv->on(Constant::EVENT_CLOSE, function (Server $serv, $fd, $reactor_id) { | ||
global $socket; | ||
fwrite($socket[0], $serv->worker_id . "\n"); | ||
}); | ||
$serv->on(Constant::EVENT_TASK, function (Server $serv, $task_id, $worker_id, $msg) { | ||
$serv->close($msg['close_fd']); | ||
}); | ||
$serv->start(); | ||
}; | ||
|
||
$pm->childFirst(); | ||
$pm->run(); | ||
?> | ||
--EXPECT-- | ||
DONE |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.