Skip to content

Commit

Permalink
Force closing a persistent connection via StreamConnection::close()
Browse files Browse the repository at this point in the history
This is necessary in cases of "out-of-sync" errors or when re-authentication
is required. How to test/reproduce:

// test_auth.php
<?php

// Run php -S localhost:8000 test_auth.php,
// then open the http://localhost:8000/?close=0 page
// and then http://localhost:8000/?close=1
// (authentication must be initiated for both requests)

declare(strict_types=1);

use Tarantool\Client\Client;
use Tarantool\Client\Middleware\AuthenticationMiddleware;

require __DIR__.'/vendor/autoload.php';

$client = Client::fromDsn('tcp://localhost:3301?persistent=true');
$client = $client->withMiddleware(new AuthenticationMiddleware('guest'));

if (isset($_REQUEST['close']) && $_REQUEST['close']) {
    $client->getHandler()->getConnection()->close();
}

$client->ping();
  • Loading branch information
rybakit committed Sep 10, 2020
1 parent c409cc4 commit ee6bc94
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Connection/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ interface Connection
public function open() : Greeting;

/**
* Closes an opened connection.
* Closes an opened connection (including a persistent one).
*/
public function close() : void;

Expand Down
6 changes: 6 additions & 0 deletions src/Connection/StreamConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ public function open() : Greeting

public function close() : void
{
// To close a previously opened persistent connection,
// we need to obtain its stream handler first
if (!$this->stream && $this->options['persistent']) {
$this->open();
}

if ($this->stream) {
/** @psalm-suppress InvalidPropertyAssignmentValue */
\fclose($this->stream);
Expand Down

0 comments on commit ee6bc94

Please sign in to comment.