Skip to content

Commit

Permalink
move connectionId to shared configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Tymek committed Jan 23, 2025
1 parent 98b5492 commit 0550c86
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 35 deletions.
17 changes: 3 additions & 14 deletions src/Client/DefaultRegistrationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,18 @@
use Unleash\Client\Enum\CacheKey;
use Unleash\Client\Helper\StringStream;
use Unleash\Client\Helper\Url;
use Unleash\Client\Helper\Uuid;
use Unleash\Client\Strategy\StrategyHandler;
use Unleash\Client\Unleash;

final class DefaultRegistrationService implements RegistrationService
{
private string $sdkName;

private string $sdkVersion;

private string $connectionId;

public function __construct(
private readonly ClientInterface $httpClient,
private readonly RequestFactoryInterface $requestFactory,
private readonly UnleashConfiguration $configuration,
?string $sdkName = null,
?string $sdkVersion = null,
?string $connectionId = null,
private ?string $sdkName = Unleash::SDK_NAME,
private ?string $sdkVersion = Unleash::SDK_VERSION,
) {
$this->sdkName = $sdkName ?? Unleash::SDK_NAME;
$this->sdkVersion = $sdkVersion ?? Unleash::SDK_VERSION;
$this->connectionId = $connectionId ?? Uuid::v4();
}

/**
Expand Down Expand Up @@ -78,7 +67,7 @@ public function register(iterable $strategyHandlers): bool
$request = $request
->withHeader('x-unleash-appname', $this->configuration->getAppName())
->withHeader('x-unleash-sdk', $this->sdkName . ':' . $this->sdkVersion)
->withHeader('x-unleash-connection-id', $this->connectionId);
->withHeader('x-unleash-connection-id', $this->configuration->getConnectionId());

try {
$response = $this->httpClient->sendRequest($request);
Expand Down
17 changes: 17 additions & 0 deletions src/Configuration/UnleashConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@
use Unleash\Client\Bootstrap\EmptyBootstrapProvider;
use Unleash\Client\ContextProvider\DefaultUnleashContextProvider;
use Unleash\Client\ContextProvider\UnleashContextProvider;
use Unleash\Client\Enum\CacheKey;
use Unleash\Client\Helper\Url;
use Unleash\Client\Helper\Uuid;
use Unleash\Client\Metrics\DefaultMetricsBucketSerializer;
use Unleash\Client\Metrics\MetricsBucketSerializer;

final class UnleashConfiguration
{
private string $connectionId;

/**
* @param array<string,string> $headers
*/
Expand All @@ -45,6 +49,7 @@ public function __construct(
private MetricsBucketSerializer $metricsBucketSerializer = new DefaultMetricsBucketSerializer(),
) {
$this->contextProvider ??= new DefaultUnleashContextProvider();
$this->connectionId = Uuid::v4();
}

public function getCache(): CacheInterface
Expand Down Expand Up @@ -298,4 +303,16 @@ public function setMetricsBucketSerializer(MetricsBucketSerializer $metricsBucke

return $this;
}

public function getConnectionId(): string
{
$cachedConnectionId = $this->getCache()->get(CacheKey::CONNECTION_ID);

return $cachedConnectionId ?? $this->connectionId;

Check failure on line 311 in src/Configuration/UnleashConfiguration.php

View workflow job for this annotation

GitHub Actions / Static analysis (8.3)

Method Unleash\Client\Configuration\UnleashConfiguration::getConnectionId() should return string but returns mixed.
}

public function updateCachedConnectionId(): void
{
$this->getCache()->set(CacheKey::CONNECTION_ID, $this->connectionId);
}
}
2 changes: 2 additions & 0 deletions src/Enum/CacheKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ final class CacheKey
public const string REGISTRATION = 'unleash.client.metrics.registration';

public const string FEATURES_RESPONSE = 'unleash.client.feature.response';

public const string CONNECTION_ID = 'unleash.client.connection.id';
}
21 changes: 6 additions & 15 deletions src/Repository/DefaultUnleashRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
use Unleash\Client\Exception\HttpResponseException;
use Unleash\Client\Exception\InvalidValueException;
use Unleash\Client\Helper\Url;
use Unleash\Client\Helper\Uuid;
use Unleash\Client\Unleash;

/**
Expand Down Expand Up @@ -85,23 +84,13 @@
*/
final readonly class DefaultUnleashRepository implements UnleashRepository
{
private string $sdkName;

private string $sdkVersion;

private string $connectionId;

public function __construct(
private ClientInterface $httpClient,
private RequestFactoryInterface $requestFactory,
private UnleashConfiguration $configuration,
?string $sdkName = null,
?string $sdkVersion = null,
?string $connectionId = null,
private string $sdkName = Unleash::SDK_NAME,
private string $sdkVersion = Unleash::SDK_VERSION,
) {
$this->sdkName = $sdkName ?? Unleash::SDK_NAME;
$this->sdkVersion = $sdkVersion ?? Unleash::SDK_VERSION;
$this->connectionId = $connectionId ?? Uuid::v4();
}

/**
Expand Down Expand Up @@ -171,7 +160,9 @@ private function getCachedFeatures(): ?array
private function setCache(array $features): void
{
$cache = $this->configuration->getCache();
$cache->set(CacheKey::FEATURES, $features, $this->configuration->getTtl());
$ttl = $this->configuration->getTtl();
$cache->set(CacheKey::FEATURES, $features, $ttl);
$this->configuration->updateCachedConnectionId();
}

/**
Expand Down Expand Up @@ -535,7 +526,7 @@ private function fetchFeatures(): array
$request = $request
->withHeader('x-unleash-appname', $this->configuration->getAppName())
->withHeader('x-unleash-sdk', $this->sdkName . ':' . $this->sdkVersion)
->withHeader('x-unleash-connection-id', $this->connectionId);
->withHeader('x-unleash-connection-id', $this->configuration->getConnectionId());

try {
$response = $this->httpClient->sendRequest($request);
Expand Down
6 changes: 0 additions & 6 deletions src/UnleashBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,9 +415,6 @@ public function build(): Unleash
$dependencyContainer->getHttpClient(),
$dependencyContainer->getRequestFactory(),
$dependencyContainer->getConfiguration(),
Unleash::SDK_NAME,
Unleash::SDK_VERSION,
$this->connectionId
);
$metricsHandler = $this->metricsHandler ?? new DefaultMetricsHandler($metricsSender, $dependencyContainer->getConfiguration());
$variantHandler = $this->variantHandler ?? new DefaultVariantHandler(new MurmurHashCalculator());
Expand Down Expand Up @@ -687,9 +684,6 @@ private function createRepository(UnleashBuilderContainer $dependencyContainer):
$dependencyContainer->getHttpClient(),
$dependencyContainer->getRequestFactory(),
$dependencyContainer->getConfiguration(),
Unleash::SDK_NAME,
Unleash::SDK_VERSION,
$this->connectionId
);
}
}

0 comments on commit 0550c86

Please sign in to comment.