Skip to content

Commit

Permalink
experiment: multiple backends
Browse files Browse the repository at this point in the history
  • Loading branch information
mcharytoniuk committed May 27, 2024
1 parent 87eba4d commit bd55c6a
Show file tree
Hide file tree
Showing 25 changed files with 89 additions and 14 deletions.
2 changes: 2 additions & 0 deletions src/AmpServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
use Amp\Http\Server\RequestHandler;
use Amp\Http\Server\Response;
use Amp\Http\Server\SocketHttpServer;
use Distantmagic\Resonance\Attribute\RequiresBackendDriver;
use Distantmagic\Resonance\Attribute\Singleton;
use Distantmagic\Resonance\PsrMessage\AmpServerRequest;
use Distantmagic\Resonance\PsrMessage\ServerResponse;
use Psr\Log\LoggerInterface;

use function Amp\trapSignal;

#[RequiresBackendDriver(BackendDriver::Amp)]
#[Singleton]
readonly class AmpServer implements RequestHandler
{
Expand Down
15 changes: 15 additions & 0 deletions src/Attribute/RequiresBackendDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Distantmagic\Resonance\Attribute;

use Attribute;
use Distantmagic\Resonance\Attribute as BaseAttribute;
use Distantmagic\Resonance\BackendDriver;

#[Attribute(Attribute::TARGET_CLASS)]
final readonly class RequiresBackendDriver extends BaseAttribute
{
public function __construct(public BackendDriver $backendDriver) {}

Check failure on line 14 in src/Attribute/RequiresBackendDriver.php

View workflow job for this annotation

GitHub Actions / psalm

PossiblyUnusedProperty

src/Attribute/RequiresBackendDriver.php:14:54: PossiblyUnusedProperty: Cannot find any references to property Distantmagic\Resonance\Attribute\RequiresBackendDriver::$backendDriver (see https://psalm.dev/149)
}
11 changes: 11 additions & 0 deletions src/BackendDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Distantmagic\Resonance;

enum BackendDriver
{
case Amp;
case Swoole;
}
3 changes: 3 additions & 0 deletions src/Command/GrpcGenerate.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
namespace Distantmagic\Resonance\Command;

use Distantmagic\Resonance\Attribute\ConsoleCommand;
use Distantmagic\Resonance\Attribute\RequiresBackendDriver;
use Distantmagic\Resonance\Attribute\RequiresPhpExtension;
use Distantmagic\Resonance\BackendDriver;
use Distantmagic\Resonance\Command;
use Distantmagic\Resonance\CoroutineCommand;
use Distantmagic\Resonance\CoroutineDriverInterface;
Expand All @@ -21,6 +23,7 @@
name: 'grpc:generate',
description: 'Generate GRPC stubs'
)]
#[RequiresBackendDriver(BackendDriver::Swoole)]
#[RequiresPhpExtension('grpc')]
#[RequiresPhpExtension('protobuf')]
final class GrpcGenerate extends CoroutineCommand
Expand Down
3 changes: 3 additions & 0 deletions src/Command/Watch.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

use Distantmagic\Resonance\ApplicationConfiguration;
use Distantmagic\Resonance\Attribute\ConsoleCommand;
use Distantmagic\Resonance\Attribute\RequiresBackendDriver;
use Distantmagic\Resonance\Attribute\RequiresPhpExtension;
use Distantmagic\Resonance\BackendDriver;
use Distantmagic\Resonance\Command;
use Distantmagic\Resonance\InotifyIterator;
use Psr\Log\LoggerInterface;
Expand All @@ -19,6 +21,7 @@
name: 'watch:command',
description: 'Watch project files for changes'
)]
#[RequiresBackendDriver(BackendDriver::Swoole)]
#[RequiresPhpExtension('inotify')]
final class Watch extends Command
{
Expand Down
2 changes: 1 addition & 1 deletion src/DependencyInjectionContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ private function makeSingleton(string $className, DependencyStack $stack): objec
if ($potentialSingletonProvider instanceof SingletonProviderInterface) {
if ($potentialSingletonProvider instanceof RegisterableInterface && !$potentialSingletonProvider->shouldRegister()) {
throw new DependencyInjectionContainerException(sprintf(
'"%s" service provider refused to register',
'"%s" service provider refused to register. No other candidates available.',
$potentialSingletonProvider::class,
));
}
Expand Down
2 changes: 2 additions & 0 deletions src/DoctrineEntityManagerWeakStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Distantmagic\Resonance;

use Distantmagic\Resonance\Attribute\RequiresBackendDriver;
use Distantmagic\Resonance\Attribute\Singleton;
use Distantmagic\Resonance\Attribute\WebSocketAware;
use Doctrine\ORM\EntityManagerInterface;
Expand All @@ -13,6 +14,7 @@
use Swoole\Coroutine\Context;
use WeakMap;

#[RequiresBackendDriver(BackendDriver::Swoole)]
#[Singleton(collection: SingletonCollection::WebSocketAware)]
#[WebSocketAware]
readonly class DoctrineEntityManagerWeakStore
Expand Down
2 changes: 2 additions & 0 deletions src/LlamaCppClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
namespace Distantmagic\Resonance;

use CurlHandle;
use Distantmagic\Resonance\Attribute\RequiresBackendDriver;
use Distantmagic\Resonance\Attribute\RequiresPhpExtension;
use Distantmagic\Resonance\Attribute\Singleton;
use Generator;
use Psr\Log\LoggerInterface;
use RuntimeException;
use Swoole\Coroutine\Channel;

#[RequiresBackendDriver(BackendDriver::Swoole)]
#[RequiresPhpExtension('curl')]
#[Singleton(provides: LlamaCppClientInterface::class)]
readonly class LlamaCppClient implements LlamaCppClientInterface
Expand Down
2 changes: 2 additions & 0 deletions src/ObservableTaskTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Distantmagic\Resonance;

use DateTimeImmutable;
use Distantmagic\Resonance\Attribute\RequiresBackendDriver;
use Distantmagic\Resonance\Attribute\Singleton;
use Generator;
use IteratorAggregate;
Expand All @@ -14,6 +15,7 @@
/**
* @template-implements IteratorAggregate<non-empty-string,ObservableTaskTableRow>
*/
#[RequiresBackendDriver(BackendDriver::Swoole)]
#[Singleton]
readonly class ObservableTaskTable implements IteratorAggregate
{
Expand Down
2 changes: 2 additions & 0 deletions src/PsrSwooleResponder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

namespace Distantmagic\Resonance;

use Distantmagic\Resonance\Attribute\RequiresBackendDriver;
use Distantmagic\Resonance\Attribute\Singleton;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Swoole\Http\Response;

#[RequiresBackendDriver(BackendDriver::Swoole)]
#[Singleton]
readonly class PsrSwooleResponder
{
Expand Down
2 changes: 1 addition & 1 deletion src/RedisConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
public Redis $redis;

public function __construct(
private RedisConnectionPoolRepository $redisConnectionPoolRepository,
private RedisConnectionPoolRepositoryInterface $redisConnectionPoolRepository,
string $redisPrefix,
private string $connectionPoolName = 'default',
) {
Expand Down
2 changes: 1 addition & 1 deletion src/RedisConnectionPoolRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Redis;
use Swoole\Database\RedisPool;

readonly class RedisConnectionPoolRepository
readonly class RedisConnectionPoolRepository implements RedisConnectionPoolRepositoryInterface
{
/**
* @var Map<string,RedisPool>
Expand Down
14 changes: 14 additions & 0 deletions src/RedisConnectionPoolRepositoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Distantmagic\Resonance;

use Redis;

interface RedisConnectionPoolRepositoryInterface
{
public function getConnection(string $name): Redis;

public function putConnection(string $name, Redis $redis): void;
}
2 changes: 2 additions & 0 deletions src/ServerPipeMessageDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

namespace Distantmagic\Resonance;

use Distantmagic\Resonance\Attribute\RequiresBackendDriver;
use Distantmagic\Resonance\Attribute\Singleton;
use RuntimeException;
use Swoole\Server;

#[RequiresBackendDriver(BackendDriver::Swoole)]
#[Singleton]
readonly class ServerPipeMessageDispatcher
{
Expand Down
2 changes: 2 additions & 0 deletions src/ServerTaskHandlerDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

namespace Distantmagic\Resonance;

use Distantmagic\Resonance\Attribute\RequiresBackendDriver;
use Distantmagic\Resonance\Attribute\Singleton;
use Psr\Log\LoggerInterface;
use Swoole\Server;
use Swoole\Server\Task;
use Symfony\Component\Messenger\Envelope;

#[RequiresBackendDriver(BackendDriver::Swoole)]
#[Singleton]
readonly class ServerTaskHandlerDispatcher
{
Expand Down
2 changes: 1 addition & 1 deletion src/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

public function __construct(
private RedisConfiguration $redisConfiguration,
private RedisConnectionPoolRepository $redisConnectionPoolRepository,
private RedisConnectionPoolRepositoryInterface $redisConnectionPoolRepository,
private SerializerInterface $serializer,
private SessionConfiguration $sessionConfiguration,
public string $id,
Expand Down
2 changes: 1 addition & 1 deletion src/SessionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
public function __construct(
private CookieManager $cookieManager,
private RedisConfiguration $redisConfiguration,
private RedisConnectionPoolRepository $redisConnectionPoolRepository,
private RedisConnectionPoolRepositoryInterface $redisConnectionPoolRepository,
private SecureIdentifierGenerator $secureIdentifierGenerator,
private SerializerInterface $serializer,
private SessionConfiguration $sessionConfiguration,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

namespace Distantmagic\Resonance\SingletonProvider;

use Distantmagic\Resonance\Attribute\RequiresBackendDriver;
use Distantmagic\Resonance\Attribute\RequiresPhpExtension;
use Distantmagic\Resonance\Attribute\Singleton;
use Distantmagic\Resonance\BackendDriver;
use Distantmagic\Resonance\DatabaseConfiguration;
use Distantmagic\Resonance\DatabaseConnectionPoolRepository;
use Distantmagic\Resonance\PHPProjectFiles;
Expand All @@ -17,6 +19,7 @@
/**
* @template-extends SingletonProvider<DatabaseConnectionPoolRepository>
*/
#[RequiresBackendDriver(BackendDriver::Swoole)]
#[RequiresPhpExtension('pdo')]
#[Singleton(provides: DatabaseConnectionPoolRepository::class)]
final readonly class DatabaseConnectionPoolRepositoryProvider extends SingletonProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,32 @@

namespace Distantmagic\Resonance\SingletonProvider;

use Distantmagic\Resonance\Attribute\RequiresBackendDriver;
use Distantmagic\Resonance\Attribute\RequiresPhpExtension;
use Distantmagic\Resonance\Attribute\Singleton;
use Distantmagic\Resonance\BackendDriver;
use Distantmagic\Resonance\PHPProjectFiles;
use Distantmagic\Resonance\RedisConfiguration;
use Distantmagic\Resonance\RedisConnectionPoolRepository;
use Distantmagic\Resonance\RedisConnectionPoolRepositoryInterface;
use Distantmagic\Resonance\SingletonContainer;
use Distantmagic\Resonance\SingletonProvider;
use Swoole\Database\RedisConfig;
use Swoole\Database\RedisPool;

/**
* @template-extends SingletonProvider<RedisConnectionPoolRepository>
* @template-extends SingletonProvider<RedisConnectionPoolRepositoryInterface>
*/
#[RequiresBackendDriver(BackendDriver::Swoole)]
#[RequiresPhpExtension('redis')]
#[Singleton(provides: RedisConnectionPoolRepository::class)]
final readonly class RedisConnectionPoolRepositoryProvider extends SingletonProvider
#[Singleton(provides: RedisConnectionPoolRepositoryInterface::class)]
final readonly class SwooleRedisConnectionPoolRepositoryProvider extends SingletonProvider
{
public function __construct(
private RedisConfiguration $databaseConfiguration,
) {}

public function provide(SingletonContainer $singletons, PHPProjectFiles $phpProjectFiles): RedisConnectionPoolRepository
public function provide(SingletonContainer $singletons, PHPProjectFiles $phpProjectFiles): RedisConnectionPoolRepositoryInterface
{
$redisConnectionPoolRepository = new RedisConnectionPoolRepository();

Expand All @@ -44,9 +48,4 @@ public function provide(SingletonContainer $singletons, PHPProjectFiles $phpProj

return $redisConnectionPoolRepository;
}

public function shouldRegister(): bool
{
return false;
}
}
2 changes: 2 additions & 0 deletions src/SwooleServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Distantmagic\Resonance;

use Distantmagic\Resonance\Attribute\GrantsFeature;
use Distantmagic\Resonance\Attribute\RequiresBackendDriver;
use Distantmagic\Resonance\Attribute\Singleton;
use Distantmagic\Resonance\Event\HttpServerBeforeStop;
use Distantmagic\Resonance\Event\HttpServerStarted;
Expand All @@ -19,6 +20,7 @@
use Throwable;

#[GrantsFeature(Feature::SwooleTaskServer)]
#[RequiresBackendDriver(BackendDriver::Swoole)]
#[Singleton]
readonly class SwooleServer
{
Expand Down
2 changes: 2 additions & 0 deletions src/SwooleTaskServerMessageBroker.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

namespace Distantmagic\Resonance;

use Distantmagic\Resonance\Attribute\RequiresBackendDriver;
use Distantmagic\Resonance\Attribute\Singleton;
use Ds\Set;
use Swoole\Server;
use Symfony\Component\Messenger\Envelope;

#[RequiresBackendDriver(BackendDriver::Swoole)]
#[Singleton]
readonly class SwooleTaskServerMessageBroker
{
Expand Down
2 changes: 2 additions & 0 deletions src/TickTimerScheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

namespace Distantmagic\Resonance;

use Distantmagic\Resonance\Attribute\RequiresBackendDriver;
use Distantmagic\Resonance\Attribute\Singleton;
use RuntimeException;
use Swoole\Timer;

#[RequiresBackendDriver(BackendDriver::Swoole)]
#[Singleton]
class TickTimerScheduler
{
Expand Down
3 changes: 3 additions & 0 deletions src/WebSocketProtocolController/JsonRPCProtocolController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

use Distantmagic\Resonance\Attribute\ControlsWebSocketProtocol;
use Distantmagic\Resonance\Attribute\GrantsFeature;
use Distantmagic\Resonance\Attribute\RequiresBackendDriver;
use Distantmagic\Resonance\Attribute\Singleton;
use Distantmagic\Resonance\AuthenticatedUserStoreAggregate;
use Distantmagic\Resonance\BackendDriver;
use Distantmagic\Resonance\ConstraintResultErrorMessage;
use Distantmagic\Resonance\CSRFManager;
use Distantmagic\Resonance\Feature;
Expand Down Expand Up @@ -37,6 +39,7 @@

#[ControlsWebSocketProtocol(WebSocketProtocol::JsonRPC)]
#[GrantsFeature(Feature::WebSocket)]
#[RequiresBackendDriver(BackendDriver::Swoole)]
#[Singleton(collection: SingletonCollection::WebSocketProtocolController)]
final readonly class JsonRPCProtocolController extends WebSocketProtocolController
{
Expand Down
2 changes: 2 additions & 0 deletions src/WebSocketServerConnectionTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Distantmagic\Resonance;

use Distantmagic\Resonance\Attribute\GrantsFeature;
use Distantmagic\Resonance\Attribute\RequiresBackendDriver;
use Distantmagic\Resonance\Attribute\Singleton;
use Generator;
use IteratorAggregate;
Expand All @@ -15,6 +16,7 @@
* @template-implements IteratorAggregate<int,int>
*/
#[GrantsFeature(Feature::WebSocket)]
#[RequiresBackendDriver(BackendDriver::Swoole)]
#[Singleton]
readonly class WebSocketServerConnectionTable implements IteratorAggregate
{
Expand Down
2 changes: 2 additions & 0 deletions src/WebSocketServerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Distantmagic\Resonance\Attribute\GrantsFeature;
use Distantmagic\Resonance\Attribute\HandlesServerPipeMessage;
use Distantmagic\Resonance\Attribute\RequiresBackendDriver;
use Distantmagic\Resonance\Attribute\Singleton;
use Distantmagic\Resonance\PsrMessage\SwooleServerRequest;
use Distantmagic\Resonance\ServerPipeMessage\CloseWebSocketConnection;
Expand All @@ -23,6 +24,7 @@
*/
#[GrantsFeature(Feature::WebSocket)]
#[HandlesServerPipeMessage(CloseWebSocketConnection::class)]
#[RequiresBackendDriver(BackendDriver::Swoole)]
#[Singleton(collection: SingletonCollection::ServerPipeMessageHandler)]
final readonly class WebSocketServerController implements ServerPipeMessageHandlerInterface
{
Expand Down

0 comments on commit bd55c6a

Please sign in to comment.