Skip to content
This repository has been archived by the owner on Jan 17, 2022. It is now read-only.

Commit

Permalink
refactor(http-client): Decouple components of Http clients
Browse files Browse the repository at this point in the history
- Extract ApiServerClientFactory from ApiSeverClient
- Create CoroutinePool for coroutine scheduling and error handling
- Improve coroutines handling and testing
  • Loading branch information
k911 committed Apr 26, 2019
1 parent 4284ed3 commit eb60f5b
Show file tree
Hide file tree
Showing 23 changed files with 472 additions and 279 deletions.
53 changes: 22 additions & 31 deletions src/Bridge/Symfony/Bundle/Command/ServerStatusCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
namespace K911\Swoole\Bridge\Symfony\Bundle\Command;

use Assert\Assertion;
use K911\Swoole\Server\Api\ApiServerInterface;
use K911\Swoole\Client\Exception\ClientConnectionErrorException;
use K911\Swoole\Coroutine\CoroutinePool;
use K911\Swoole\Server\Api\ApiServerClientFactory;
use K911\Swoole\Server\Config\Socket;
use K911\Swoole\Server\Config\Sockets;
use Symfony\Component\Console\Command\Command;
Expand All @@ -17,18 +19,17 @@

final class ServerStatusCommand extends Command
{
private $apiServer;
private $apiServerClientFactory;
private $sockets;
private $parameterBag;
private $testing = false;

public function __construct(
Sockets $sockets,
ApiServerInterface $apiServer,
ApiServerClientFactory $apiServerClientFactory,
ParameterBagInterface $parameterBag
) {
$this->apiServer = $apiServer;
$this->sockets = $sockets;
$this->apiServerClientFactory = $apiServerClientFactory;
$this->parameterBag = $parameterBag;

parent::__construct();
Expand All @@ -48,40 +49,35 @@ protected function configure(): void
* {@inheritdoc}
*
* @throws \Assert\AssertionFailedException
* @throws \Throwable
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$exitCode = 0;
$io = new SymfonyStyle($input, $output);

$this->prepareClientConfiguration($input);

$this->goAndWait(function () use ($io): void {
try {
$status = $this->apiServer->status();
$metrics = $this->apiServer->metrics();
} catch (\RuntimeException $runtimeException) {
$io->error('Could not connect to Swoole API Server');

return;
}
$io->success('Fetched status and metrics');
$coroutinePool = CoroutinePool::fromCoroutines(function () use ($io): void {
$status = $this->apiServerClientFactory->newClient()
->status();
$io->success('Fetched status');
$this->showStatus($io, $status);
}, function () use ($io): void {
$metrics = $this->apiServerClientFactory->newClient()
->metrics();
$io->success('Fetched metrics');
$this->showMetrics($io, $metrics);
});

return 0;
}

public function goAndWait(callable $callback): void
{
if ($this->testing) {
$callback();

return;
try {
$coroutinePool->run();
} catch (ClientConnectionErrorException $errorException) {
$io->error('An error occurred while connecting to the API Server. Please verify configuration.');
$exitCode = 1;
}

\go($callback);
\swoole_event_wait();
return $exitCode;
}

private function showStatus(SymfonyStyle $io, array $status): void
Expand Down Expand Up @@ -147,9 +143,4 @@ protected function prepareClientConfiguration(InputInterface $input): void

$this->sockets->changeApiSocket(new Socket($host, (int) $port));
}

public function enableTestMode(): void
{
$this->testing = true;
}
}
2 changes: 0 additions & 2 deletions src/Bridge/Symfony/Bundle/Resources/config/commands.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ services:

'K911\Swoole\Bridge\Symfony\Bundle\Command\ServerStatusCommand':
tags: [ { name: console.command, command: 'swoole:server:status' } ]
arguments:
$apiServer: '@K911\Swoole\Server\Api\ApiServerClient'

'K911\Swoole\Bridge\Symfony\Bundle\Command\ServerStopCommand':
tags: [ { name: console.command, command: 'swoole:server:stop' } ]
Expand Down
3 changes: 3 additions & 0 deletions src/Bridge/Symfony/Bundle/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ services:
'K911\Swoole\Server\LifecycleHandler\ServerManagerStopHandlerInterface':
class: K911\Swoole\Server\LifecycleHandler\NoOpServerManagerStopHandler

'K911\Swoole\Server\Api\ApiServerClientFactory':

'K911\Swoole\Server\Api\ApiServerClient':
factory: 'K911\Swoole\Server\Api\ApiServerClientFactory:newClient'

'K911\Swoole\Server\Api\ApiServerInterface':
class: K911\Swoole\Server\Api\ApiServer
Expand Down
2 changes: 1 addition & 1 deletion src/Client/Exception/ClientConnectionErrorException.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ public static function serverReset(int $errorCode): self

public static function unknown(int $errorCode): self
{
return new self('Unknown', $errorCode);
return new self(\sprintf('Unknown [%d]', $errorCode), $errorCode);
}
}
23 changes: 0 additions & 23 deletions src/Client/Exception/InvalidContentTypeException.php

This file was deleted.

16 changes: 0 additions & 16 deletions src/Client/Exception/InvalidHttpMethodException.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/Client/Exception/MissingContentTypeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
*/
final class MissingContentTypeException extends \InvalidArgumentException
{
public static function create(): self
public static function make(): self
{
return new self(\sprintf('Server response did not contain Content-Type.'));
return new self(\sprintf('Server response did not contain mandatory header "Content-Type".'));
}
}
22 changes: 22 additions & 0 deletions src/Client/Exception/UnsupportedContentTypeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace K911\Swoole\Client\Exception;

/**
* @internal
*/
final class UnsupportedContentTypeException extends \InvalidArgumentException
{
/**
* @param string $contentType
* @param string[] $allowed
*
* @return UnsupportedContentTypeException
*/
public static function forContentType(string $contentType, array $allowed): self
{
return new self(\sprintf('Content-Type "%s" is not supported. Only "%s" are supported.', $contentType, \implode(', ', $allowed)));
}
}
22 changes: 22 additions & 0 deletions src/Client/Exception/UnsupportedHttpMethodException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace K911\Swoole\Client\Exception;

/**
* @internal
*/
final class UnsupportedHttpMethodException extends \InvalidArgumentException
{
/**
* @param string $method
* @param string[] $allowed
*
* @return UnsupportedHttpMethodException
*/
public static function forMethod(string $method, array $allowed): self
{
return new self(\sprintf('Http method "%s" is not supported. Only "%s" are supported.', $method, \implode(', ', $allowed)));
}
}
Loading

0 comments on commit eb60f5b

Please sign in to comment.