Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
2 parents 22af4f6 + 0cb716e commit a640dcb
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 3 deletions.
13 changes: 13 additions & 0 deletions src/Cache/CacheClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,19 @@ public function __construct(
}
}

/**
* Close the client and free up all associated resources. NOTE: the client object will not be usable after calling
* this method.
*/
public function close(): void
{
$this->controlClient->close();
foreach ($this->dataClients as $dataClient) {
$dataClient->close();
}
}


/**
* Assigns a LoggerInterface logging object to the client.
*
Expand Down
11 changes: 8 additions & 3 deletions src/Cache/Internal/ControlGrpcManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class ControlGrpcManager
{

public ScsControlClient $client;
private Channel $channel;

public function __construct(ICredentialProvider $authProvider)
{
Expand All @@ -24,14 +25,18 @@ public function __construct(ICredentialProvider $authProvider)
if ($authProvider->getTrustedControlEndpointCertificateName()) {
$channelArgs["grpc.ssl_target_name_override"] = $authProvider->getTrustedControlEndpointCertificateName();
}
$channel = new Channel($endpoint, $channelArgs);
$this->channel = new Channel($endpoint, $channelArgs);
$interceptors = [
new AuthorizationInterceptor($authProvider->getAuthToken()),
new AgentInterceptor(),
];
$channel = Interceptor::intercept($channel, $interceptors);
$interceptedChannel = Interceptor::intercept($this->channel, $interceptors);
$options = [];
$this->client = new ScsControlClient($endpoint, $options, $channel);
$this->client = new ScsControlClient($endpoint, $options, $interceptedChannel);
}

public function close(): void {
$this->channel->close();
}

}
4 changes: 4 additions & 0 deletions src/Cache/Internal/IdleDataClientWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public function getClient(): ScsDataClient {
return $this->client;
}

public function close(): void {
$this->client->close();
}

private function getMilliseconds(): int {
return (int)(gettimeofday(true) * 1000);
}
Expand Down
4 changes: 4 additions & 0 deletions src/Cache/Internal/ScsControlClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public function __construct(ILoggerFactory $loggerFactory, ICredentialProvider $
$this->setLogger($this->loggerFactory->getLogger(get_class($this)));
}

public function close(): void {
$this->grpcManager->close();
}

public function setLogger(LoggerInterface $logger): void
{
$this->logger = $logger;
Expand Down
11 changes: 11 additions & 0 deletions tests/Cache/CacheClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ private function getConfigurationWithDeadline(int $deadline)
return new Configuration($loggerFactory, $transportStrategy);
}

public function testCreateAndCloseClient() {
$client = new CacheClient($this->configuration, $this->authProvider, $this->DEFAULT_TTL_SECONDS);
$response = $client->listCaches();
$this->assertNull($response->asError());
$client->close();
$client = new CacheClient($this->configuration, $this->authProvider, $this->DEFAULT_TTL_SECONDS);
$response = $client->listCaches();
$this->assertNull($response->asError());
$client->close();
}

// Happy path test

public function testCreateSetGetDelete()
Expand Down

0 comments on commit a640dcb

Please sign in to comment.