Skip to content
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
11 changes: 11 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
private const int DEFAULT_CONNECTION_TIMEOUT = 10;
private const int DEFAULT_PING_INTERVAL = 10000;
private const int DEFAULT_MAX_PINGS = 5;
private const string DEFAULT_CLIENT_NAME = 'thesis/nats';

/** @var non-empty-string */
public string $version;
Expand All @@ -29,6 +30,7 @@
* @param ?positive-int $ping in milliseconds
* @param positive-int $maxPings the maximum number of pings that we have not received a response to, after which the connection to the server will be closed
* @param ?non-empty-string $jetStreamDomain
* @param non-empty-string $clientName
*/
public function __construct(
public array $urls = [self::DEFAULT_URL],
Expand All @@ -48,6 +50,7 @@ public function __construct(
public ?int $ping = self::DEFAULT_PING_INTERVAL,
public int $maxPings = self::DEFAULT_MAX_PINGS,
public ?string $jetStreamDomain = null,
public string $clientName = self::DEFAULT_CLIENT_NAME,
) {
$this->version = '0.1.x'; // TODO: replace with actual version.
}
Expand Down Expand Up @@ -151,6 +154,11 @@ public static function fromURI(#[\SensitiveParameter] string $uri): self
$nkey = $query['nkey'];
}

$clientName = self::DEFAULT_CLIENT_NAME;
if (isset($query['client_name']) && \is_string($query['client_name']) && $query['client_name'] !== '') {
$clientName = $query['client_name'];
}

return new self(
urls: $urls,
verbose: $verbose,
Expand All @@ -165,6 +173,7 @@ public static function fromURI(#[\SensitiveParameter] string $uri): self
ping: $ping,
maxPings: $maxPings,
jetStreamDomain: $jetStreamDomain,
clientName: $clientName,
);
}

Expand All @@ -183,6 +192,7 @@ public static function fromURI(#[\SensitiveParameter] string $uri): self
* ping?: positive-int,
* max_pings?: positive-int,
* jetstream_domain?: non-empty-string,
* client_name?: non-empty-string,
* } $options
*/
public static function fromArray(#[\SensitiveParameter] array $options): self
Expand All @@ -201,6 +211,7 @@ public static function fromArray(#[\SensitiveParameter] array $options): self
ping: $options['ping'] ?? self::DEFAULT_PING_INTERVAL,
maxPings: $options['max_pings'] ?? self::DEFAULT_MAX_PINGS,
jetStreamDomain: $options['jetstream_domain'] ?? null,
clientName: $options['client_name'] ?? self::DEFAULT_CLIENT_NAME,
);
}
}
2 changes: 1 addition & 1 deletion src/Internal/Connection/SocketConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function startup(): void
verbose: $this->config->verbose,
pedantic: $this->config->pedantic,
tlsRequired: false,
name: 'thesis/nats',
name: $this->config->clientName,
version: $this->config->version,
user: $this->config->user,
pass: $this->config->password,
Expand Down
12 changes: 12 additions & 0 deletions tests/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ final class ConfigTest extends TestCase
),
],
)]
#[TestWith(
[
'tcp://admin:secret@127.0.0.1:4222?verbose=false&pedantic=true&client_name=prod',
new Config(
verbose: false,
pedantic: true,
user: 'admin',
password: 'secret',
clientName: 'prod',
),
],
)]
#[TestWith(
[
'tcp://admin:secret@127.0.0.1:4222,127.0.0.1:4223?verbose=false&pedantic=true',
Expand Down