Skip to content
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

Removed timeout property of Hyperf\Utils\Coroutine\Concurrent #602

Merged
merged 3 commits into from Sep 23, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 4 additions & 19 deletions src/utils/src/Coroutine/Concurrent.php
Expand Up @@ -15,7 +15,7 @@
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\ExceptionHandler\Formatter\FormatterInterface;
use Hyperf\Utils\ApplicationContext;
use Hyperf\Utils\Coroutine;
use Swoole\Coroutine;
use Swoole\Coroutine\Channel;

/**
Expand All @@ -31,21 +31,15 @@ class Concurrent
*/
protected $channel;

/**
* @var float
*/
protected $timeout;

/**
* @var int
*/
protected $limit;

public function __construct(int $limit, float $timeout = 10.0)
public function __construct(int $limit)
{
$this->limit = $limit;
$this->channel = new Channel($limit);
$this->timeout = $timeout;
}

public function __call($name, $arguments)
Expand All @@ -70,18 +64,9 @@ public function getRunningCoroutineCount(): int
return $this->getLength();
}

public function getTimeout(): float
{
return $this->timeout;
}

public function create(callable $callable): void
{
while (true) {
if ($this->channel->push(true, $this->getTimeout())) {
break;
}
}
$this->channel->push(true);

Coroutine::create(function () use ($callable) {
try {
Expand All @@ -96,7 +81,7 @@ public function create(callable $callable): void
}
}
} finally {
$this->channel->pop($this->getTimeout());
$this->channel->pop();
}
});
}
Expand Down
14 changes: 14 additions & 0 deletions src/utils/tests/Coroutine/ConcurrentTest.php
Expand Up @@ -36,6 +36,7 @@ public function testConcurrent()
$this->assertSame($limit, $concurrent->getLimit());
$this->assertTrue($concurrent->isEmpty());
$this->assertFalse($concurrent->isFull());

$count = 0;
for ($i = 0; $i < 15; ++$i) {
$concurrent->create(function () use (&$count) {
Expand All @@ -49,12 +50,19 @@ public function testConcurrent()
$this->assertSame($limit, $concurrent->getRunningCoroutineCount());
$this->assertSame($limit, $concurrent->getLength());
$this->assertSame($limit, $concurrent->length());

while (! $concurrent->isEmpty()) {
Coroutine::sleep(0.1);
}

$this->assertSame(15, $count);
}

public function testException()
{
$con = new Concurrent(10, 1);
$count = 0;

for ($i = 0; $i < 15; ++$i) {
$con->create(function () use (&$count) {
Coroutine::sleep(0.1);
Expand All @@ -65,6 +73,12 @@ public function testException()

$this->assertSame(5, $count);
$this->assertSame(10, $con->getRunningCoroutineCount());

while (! $con->isEmpty()) {
Coroutine::sleep(0.1);
}

$this->assertSame(15, $count);
}

protected function getContainer()
Expand Down