Skip to content

Commit

Permalink
Wait sometimes until channel manager is empty, before close by reac…
Browse files Browse the repository at this point in the history
…hed `max_requests`. (#3)
  • Loading branch information
limingxinleo authored Apr 20, 2024
1 parent 48e2a56 commit 19dc5ec
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 7 deletions.
25 changes: 19 additions & 6 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
<?php

declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
use PhpCsFixer\Config;
use PhpCsFixer\Finder;

$header = <<<'EOF'
This file is part of Hyperf.
Expand All @@ -9,7 +21,7 @@
@license https://github.com/hyperf/hyperf/blob/master/LICENSE
EOF;

return (new PhpCsFixer\Config())
return (new Config())
->setRiskyAllowed(true)
->setRules([
'@PSR2' => true,
Expand All @@ -23,13 +35,13 @@
'location' => 'after_declare_strict',
],
'array_syntax' => [
'syntax' => 'short'
'syntax' => 'short',
],
'list_syntax' => [
'syntax' => 'short'
'syntax' => 'short',
],
'concat_space' => [
'spacing' => 'one'
'spacing' => 'one',
],
'blank_line_before_statement' => [
'statements' => [
Expand All @@ -38,7 +50,7 @@
],
'general_phpdoc_annotation_remove' => [
'annotations' => [
'author'
'author',
],
],
'ordered_imports' => [
Expand Down Expand Up @@ -85,9 +97,10 @@
'single_quote' => true,
'standardize_not_equals' => true,
'multiline_comment_opening_closing' => true,
'single_line_empty_body' => false,
])
->setFinder(
PhpCsFixer\Finder::create()
Finder::create()
->exclude('vendor')
->in(__DIR__)
)
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"ext-sockets": "*",
"hyperf/coordinator": ">=3.0",
"hyperf/coroutine": ">=3.0",
"multiplex/multiplex": "^1.0"
"multiplex/multiplex": "^1.1"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.0",
Expand Down
21 changes: 21 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class Client implements ClientInterface, HasSerializerInterface
'connect_timeout' => 0.5,
'heartbeat' => 20,
'max_requests' => 0,
'max_wait_close_seconds' => 2,
];

protected ChannelManager $channelManager;
Expand Down Expand Up @@ -107,6 +108,7 @@ public function send(mixed $data): int
$key = spl_object_hash($this);
if (Locker::lock($key)) {
try {
$this->waitUntilChannelManagerEmpty();
$this->close();
} finally {
Locker::unlock($key);
Expand Down Expand Up @@ -342,4 +344,23 @@ protected function loop(): void
}
});
}

protected function waitUntilChannelManagerEmpty(): void
{
$now = microtime(true);
$seconds = $this->config['max_wait_close_seconds'];
while (true) {
if ($this->channelManager->isEmpty()) {
return;
}

if (microtime(true) - $now >= $seconds) {
return;
}

if (CoordinatorManager::until(Constants::WORKER_EXIT)->yield(0.05)) {
return;
}
}
}
}
27 changes: 27 additions & 0 deletions tests/Cases/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Multiplex\Socket\Client;
use Throwable;

use function Hyperf\Coroutine\go;
use function Hyperf\Coroutine\parallel;
use function Hyperf\Tappable\tap;

Expand Down Expand Up @@ -282,4 +283,30 @@ public function testPushToClosedChannel()
$this->assertFalse($chan->push(1));
});
}

public function testWaitUntilChannelManagerEmpty()
{
$this->runInCoroutine(function () {
$client = new Client('127.0.0.1', 9999);
$client->set([
'max_wait_close_seconds' => 0.5,
]);
$ref = new ClassInvoker($client);
$manager = $client->getChannelManager();
$manager->get(1, true);
$now = microtime(true);
$ref->waitUntilChannelManagerEmpty();
$this->assertTrue(microtime(true) - $now > 0.4);
$this->assertTrue(microtime(true) - $now < 0.6);

$now = microtime(true);
go(function () use ($manager) {
usleep(1000 * 100);
$manager->close(1);
});
$ref->waitUntilChannelManagerEmpty();
$this->assertTrue(microtime(true) - $now > 0.08);
$this->assertTrue(microtime(true) - $now < 0.2);
});
}
}

0 comments on commit 19dc5ec

Please sign in to comment.