Skip to content

Commit

Permalink
feat(coroutines): safe stateful services need to be reset with the or…
Browse files Browse the repository at this point in the history
…iginal sf resetter mechanism
  • Loading branch information
Rastusik committed Feb 6, 2023
1 parent 3661ff1 commit c7dab53
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,25 @@ public function getIterator(): \Traversable
return new \ArrayIterator($this->tags);
}

public function resetOnEachRequest(): bool
{
if (!$this->hasStatefulServiceTag() && !$this->hasSafeStatefulServiceTag()) {
return false;
}

if ($this->hasSafeStatefulServiceTag()) {
return true;
}

$tag = $this->findStatefulServiceTag();

if ($tag->getResetOnEachRequest()) {
return true;
}

return false;
}

/**
* @return array<array<array<string, mixed>>>
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,8 @@ private function reduceServiceResetters(ContainerBuilder $container): void
/** @var class-string $classString */
$classString = $valueDef->getClass();
$tags = new Tags($classString, $valueDef->getTags());
$tag = $tags->findStatefulServiceTag();

if (null === $tag) {
continue;
}

if (!$tag->getResetOnEachRequest()) {
if (!$tags->resetOnEachRequest()) {
continue;
}

Expand Down
4 changes: 4 additions & 0 deletions tests/Feature/SwooleServerCoroutinesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ public function testCoroutinesWithDebugOn(): void
$this->assertStringContainsString('Service2 was proxified.', $response['body']);
$this->assertStringContainsString('Service2 limit is 10.', $response['body']);
$this->assertStringContainsString('Always reset works.', $response['body']);
$this->assertStringContainsString('Safe always reseter is not a proxy.', $response['body']);
$this->assertStringContainsString('Safe Always reset works.', $response['body']);
$this->assertStringContainsString('TmpRepo was proxified.', $response['body']);
$this->assertStringContainsString('TmpRepo limit is 15.', $response['body']);
$this->assertStringContainsString('Connection limit is 12.', $response['body']);
Expand Down Expand Up @@ -138,6 +140,8 @@ public function testCoroutinesWithDebugOff(): void
$this->assertStringContainsString('Service2 was proxified.', $response['body']);
$this->assertStringContainsString('Service2 limit is 10.', $response['body']);
$this->assertStringContainsString('Always reset works.', $response['body']);
$this->assertStringContainsString('Safe always reseter is not a proxy.', $response['body']);
$this->assertStringContainsString('Safe Always reset works.', $response['body']);
$this->assertStringContainsString('TmpRepo was proxified.', $response['body']);
$this->assertStringContainsString('TmpRepo limit is 15.', $response['body']);
$this->assertStringContainsString('Connection limit is 12.', $response['body']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ public function index()
$limitProperty->setAccessible(true);
$limit = $limitProperty->getValue($servicePool);
$alwaysResetWorks = $this->shouldBeProxified->wasDummyReset();
$alwaysResetSafe = $this->shouldBeProxified->getSafeDummy();
/** @phpstan-ignore-next-line */
$safeDummyIsProxy = $alwaysResetSafe instanceof VirtualProxyInterface ? 'IS' : 'is not';
$safeAlwaysResetWorks = $alwaysResetSafe->getWasReset();

$rc2 = new \ReflectionClass(DefaultDummyService::class);
$tmpRepoProperty = $rc2->getProperty('tmpRepository');
Expand All @@ -99,6 +103,8 @@ public function index()
."Checks: {$checks}. "
."Service {$isProxified} proxified. Service2 {$isProxified2} proxified. "
.'Always reset '.($alwaysResetWorks ? 'works' : 'did not work').'. '
."Safe always reseter {$safeDummyIsProxy} a proxy. "
.'Safe Always reset '.($safeAlwaysResetWorks ? 'works' : 'did not work').'. '
."Service2 limit is {$limit}. TmpRepo {$isProxified3} proxified. "
."TmpRepo limit is {$limit2}. "
."Connection limit is {$connlimit}.</body></html>"
Expand Down
22 changes: 22 additions & 0 deletions tests/Fixtures/Symfony/TestBundle/Service/AlwaysResetSafe.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace K911\Swoole\Tests\Fixtures\Symfony\TestBundle\Service;

use Symfony\Contracts\Service\ResetInterface;

final class AlwaysResetSafe implements ResetInterface
{
private bool $wasReset = false;

public function reset(): void
{
$this->wasReset = true;
}

public function getWasReset(): bool
{
return $this->wasReset;
}
}
10 changes: 9 additions & 1 deletion tests/Fixtures/Symfony/TestBundle/Service/ShouldBeProxified.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,21 @@ final class ShouldBeProxified
{
private AlwaysReset $dummy;

public function __construct(AlwaysReset $dummy)
private AlwaysResetSafe $safeDummy;

public function __construct(AlwaysReset $dummy, AlwaysResetSafe $safeDummy)
{
$this->dummy = $dummy;
$this->safeDummy = $safeDummy;
}

public function wasDummyReset(): bool
{
return $this->dummy->getWasReset();
}

public function getSafeDummy(): AlwaysResetSafe
{
return $this->safeDummy;
}
}
4 changes: 4 additions & 0 deletions tests/Fixtures/Symfony/app/config/coroutines/swoole.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,7 @@ services:
K911\Swoole\Tests\Fixtures\Symfony\TestBundle\Service\AlwaysReset:
tags:
- { name: 'swoole_bundle.stateful_service', reset_on_each_request: true }

K911\Swoole\Tests\Fixtures\Symfony\TestBundle\Service\AlwaysResetSafe:
tags:
- { name: 'swoole_bundle.safe_stateful_service' }

0 comments on commit c7dab53

Please sign in to comment.