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

store root dispatcher in a static var #780

Merged
merged 3 commits into from
Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 3 additions & 20 deletions src/API/Common/Event/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,16 @@
namespace OpenTelemetry\API\Common\Event;

use CloudEvents\V1\CloudEventInterface;
use OpenTelemetry\Context\Context;
use OpenTelemetry\Context\ContextKey;

class Dispatcher implements DispatcherInterface
{
private static ?ContextKey $key = null;
private static ?self $root = null;
/** @var array<string, array<int, array<callable>>> */
private array $listeners = [];

public static function getInstance(): self
public static function getRoot(): self
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I chose root because it sounds less singleton-y, since you can in fact have as many dispatchers as you wish. This is just the default one.

{
$key = self::getConstantKeyInstance();

return Context::getCurrent()->get($key) ?? self::createInstance();
return self::$root ??= new self();
}

public function dispatch(CloudEventInterface $event): void
Expand Down Expand Up @@ -47,17 +43,4 @@ private function dispatchEvent(iterable $listeners, CloudEventInterface $event):
$listener($event);
}
}

private static function getConstantKeyInstance(): ContextKey
{
return self::$key ??= new ContextKey(self::class);
}

private static function createInstance(): self
{
$dispatcher = new self();
Context::getCurrent()->with(self::getConstantKeyInstance(), $dispatcher)->activate();

return $dispatcher;
}
}
2 changes: 1 addition & 1 deletion src/API/Common/Event/EmitsEventsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ trait EmitsEventsTrait
{
protected static function emit(CloudEventInterface $event): void
{
Dispatcher::getInstance()->dispatch($event);
Dispatcher::getRoot()->dispatch($event);
}
}
2 changes: 1 addition & 1 deletion tests/Benchmark/EventBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class EventBench

public function __construct()
{
$this->dispatcher = Dispatcher::getInstance();
$this->dispatcher = Dispatcher::getRoot();
$this->listener = function () {
};
$this->event = new CloudEvent(uniqid(), self::class, 'foo');
Expand Down
26 changes: 26 additions & 0 deletions tests/Integration/API/Common/EventContextIntegrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace OpenTelemetry\Tests\Integration\API\Common;

use OpenTelemetry\API\Common\Event\Dispatcher;
use OpenTelemetry\Context\Context;
use PHPUnit\Framework\TestCase;

/**
* @coversNothing
*/
class EventContextIntegrationTest extends TestCase
{
public function test_event_dispatcher_does_not_leak_scope(): void
{
$key = Context::createKey('-');
$scope = Context::getCurrent()->with($key, 1)->activate();
Dispatcher::getRoot();
$result = $scope->detach();

$this->assertSame(0, $result);
$this->assertNull(Context::getCurrent()->get($key));
}
}
17 changes: 3 additions & 14 deletions tests/Unit/API/Common/Event/DispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

use CloudEvents\V1\CloudEventInterface;
use OpenTelemetry\API\Common\Event\Dispatcher;
use OpenTelemetry\Context\Context;
use OpenTelemetry\Context\ContextKey;
use PHPUnit\Framework\TestCase;

/**
Expand All @@ -31,20 +29,11 @@ public function setUp(): void
$this->event->method('getType')->willReturn('foo');
}

public function test_get_instance(): void
public function test_get_root_dispatcher(): void
{
$dispatcher = Dispatcher::getInstance();
$dispatcher = Dispatcher::getRoot();
$this->assertInstanceOf(Dispatcher::class, $dispatcher);
$this->assertSame($dispatcher, Dispatcher::getInstance());
}

public function test_get_instance_from_parent_context(): void
{
$dispatcher = Dispatcher::getInstance();
$this->assertInstanceOf(Dispatcher::class, $dispatcher);
$parent = Context::getCurrent()->with(new ContextKey('foo'), 'bar');
$parent->activate();
$this->assertSame($dispatcher, Dispatcher::getInstance());
$this->assertSame($dispatcher, Dispatcher::getRoot());
}

public function test_add_listener(): void
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/API/Common/Event/EmitsEventsTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function test_emits_event(): void
$event->method('getType')->willReturn('bar');
$called = false;
$class = $this->createInstance();
Dispatcher::getInstance()->listen($event->getType(), function () use (&$called) {
Dispatcher::getRoot()->listen($event->getType(), function () use (&$called) {
$this->assertTrue(true, 'listener was called');
$called = true;
});
Expand Down