Skip to content

Commit

Permalink
service response types
Browse files Browse the repository at this point in the history
  • Loading branch information
nekufa committed Dec 24, 2024
1 parent 4ba5548 commit 7522f8d
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 122 deletions.
31 changes: 31 additions & 0 deletions src/Service/Response/Info.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Basis\Nats\Service\Response;

use Basis\Nats\Service\ServiceEndpoint;

class Info
{
public string $type = 'io.nats.micro.v1.info_response';
public array $endpoints;

public function __construct(
public string $name,
public string $id,
public string $version,
public string $description,
/** @var array<ServiceEndpoint> */
array $endpoints,
) {
$this->endpoints = array_map(self::collect(...), $endpoints);
}

public static function collect(ServiceEndpoint $endpoint): array
{
return [
'name' => $endpoint->getName(),
'subject' => $endpoint->getSubject(),
'queue_group' => $endpoint->getQueueGroup(),
];
}
}
15 changes: 15 additions & 0 deletions src/Service/Response/Ping.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Basis\Nats\Service\Response;

class Ping
{
public string $type = 'io.nats.micro.v1.ping_response';

public function __construct(
public string $name,
public string $id,
public string $version,
) {
}
}
36 changes: 36 additions & 0 deletions src/Service/Response/Stats.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Basis\Nats\Service\Response;

use Basis\Nats\Service\ServiceEndpoint;

class Stats
{
public string $type = 'io.nats.micro.v1.stats_response';
public array $endpoints;

public function __construct(
public string $name,
public string $id,
public string $version,
public string $started,
/** @var array<ServiceEndpoint> */
array $endpoints,
) {
$this->endpoints = array_values(array_map(self::collect(...), $endpoints));
}

public static function collect(ServiceEndpoint $endpoint): array
{
return [
'name' => $endpoint->getName(),
'subject' => $endpoint->getSubject(),
'queue_group' => $endpoint->getQueueGroup(),
'num_requests' => $endpoint->getNumRequests(),
'num_errors' => $endpoint->getNumErrors(),
'last_error' => $endpoint->getLastError(),
'processing_time' => $endpoint->getProcessingTime(),
'average_processing_time' => $endpoint->getAverageProcessingTime(),
];
}
}
116 changes: 28 additions & 88 deletions src/Service/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,35 @@
namespace Basis\Nats\Service;

use Basis\Nats\Client;
use Basis\Nats\Message\Payload;
use Basis\Nats\Service\Response\Info;
use Basis\Nats\Service\Response\Ping;
use Basis\Nats\Service\Response\Stats;

class Service
{
public Client $client;

private string $id;

private string $name;

private string $description = '';

private string $version;

private string $started;

/** @var array<ServiceEndpoint> */
private array $endpoints = [];

private array $groups = [];

private array $subscriptions = [];

public function __construct(
Client $client,
string $name,
string $description = 'Default Description',
string $version = '0.0.1'
public Client $client,
private string $name,
private string $description = 'Default Description',
private string $version = '0.0.1'
) {
$this->client = $client;
$this->id = $this->generateId();
$this->name = $name;
$this->description = $description;
$this->version = $version;
$this->started = date("Y-m-d\TH:i:s.v\Z");

// Register the service verbs to listen for
$this->registerVerbs();
}

private function generateId(): string
{
$characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';

$charactersLength = strlen($characters);

$randomString = "";

for ($i = 0; $i < 22; $i++) {
Expand All @@ -59,59 +43,29 @@ private function generateId(): string

private function ping(): array
{
return [
'type' => 'io.nats.micro.v1.ping_response',
'name' => $this->name,
'id' => $this->id,
'version' => $this->version,
];
return (array) new Ping($this->name, $this->id, $this->version);
}

private function info(): array
{
$endpoints = [];

foreach ($this->endpoints as $endpoint) {
$endpoints[] = [
'name' => $endpoint->getName(),
'subject' => $endpoint->getSubject(),
'queue_group' => $endpoint->getQueueGroup(),
];
}

return [
'type' => 'io.nats.micro.v1.info_response',
'name' => $this->name,
'id' => $this->id,
'version' => $this->version,
'description' => $this->description,
'endpoints' => $endpoints
];
return (array) new Info(
name: $this->name,
id: $this->id,
version: $this->version,
description: $this->description,
endpoints: $this->endpoints,
);
}

private function stats(): array
{
return [
'type' => 'io.nats.micro.v1.stats_response',
'name' => $this->name,
'id' => $this->id,
'version' => $this->version,
'endpoints' => array_reduce($this->endpoints, function ($carry, ServiceEndpoint $endpoint) {
$carry[] = [
'name' => $endpoint->getName(),
'subject' => $endpoint->getSubject(),
'queue_group' => $endpoint->getQueueGroup(),
'num_requests' => $endpoint->getNumRequests(),
'num_errors' => $endpoint->getNumErrors(),
'last_error' => $endpoint->getLastError(),
'processing_time' => $endpoint->getProcessingTime(),
'average_processing_time' => $endpoint->getAverageProcessingTime(),
];

return $carry;
}, []),
'started' => $this->started,
];
return (array) new Stats(
name: $this->name,
id: $this->id,
version: $this->version,
started: $this->started,
endpoints: $this->endpoints,
);
}

public function addGroup(string $name): ServiceGroup
Expand Down Expand Up @@ -143,13 +97,7 @@ public function addEndpoint(
throw new \LogicException("Endpoint $name already is defined");
}

$this->endpoints[$name] = new ServiceEndpoint(
$this,
$name,
$subject,
$endpointHandler,
$queue_group
);
$this->endpoints[$name] = new ServiceEndpoint($this, $name, $subject, $endpointHandler, $queue_group);
}

public function reset(): void
Expand All @@ -165,15 +113,9 @@ function (ServiceEndpoint $endpoint) {
private function registerVerbs(): void
{
$verbs = [
'PING' => function (Payload $payload) {
return $this->ping();
},
'INFO' => function (Payload $payload) {
return $this->info();
},
'STATS' => function (Payload $payload) {
return $this->stats();
},
'PING' => $this->ping(...),
'INFO' => $this->info(...),
'STATS' => $this->stats(...),
];

foreach ($verbs as $verb => $handler) {
Expand Down Expand Up @@ -218,9 +160,7 @@ private function controlSubject(string $verb, string $name, string $id): string

public function run(): void
{
$this->client
->logger
->info("$this->name is ready to accept connections\n");
$this->client->logger->info("$this->name is ready to accept connections\n");

while (true) {
try {
Expand Down
10 changes: 3 additions & 7 deletions src/Service/ServiceGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,8 @@ public function addEndpoint(
$subject = $this->name . '.' . $options['subject'];
}

$this->service->addEndpoint(
$name,
$serviceHandler,
[
'subject' => $subject,
]
);
$this->service->addEndpoint($name, $serviceHandler, [
'subject' => $subject,
]);
}
}
Loading

0 comments on commit 7522f8d

Please sign in to comment.