Allow configure heartbeat_interval

This commit is contained in:
2021-01-23 02:16:29 +03:00
parent e821f04ebe
commit 9f95dff2ec
2 changed files with 22 additions and 8 deletions

View File

@ -22,16 +22,21 @@ use const PHP_EOL;
*/ */
abstract class Connection abstract class Connection
{ {
public ?Socket $socket = null; private string $address;
protected LoggerInterface $logger; private ?Socket $socket = null;
private bool $closed = false; private bool $closed = false;
private string $address; private LoggerInterface $logger;
/** /**
* @var array{client_id: string, hostname: string, user_agent: string} * @var array{
* client_id: string,
* hostname: string,
* user_agent: string,
* heartbeat_interval: int|null,
* }
*/ */
private array $features; private array $features;
@ -41,6 +46,7 @@ abstract class Connection
string $clientId = null, string $clientId = null,
string $hostname = null, string $hostname = null,
string $userAgent = null, string $userAgent = null,
int $heartbeatInterval = null,
) { ) {
$this->address = $address; $this->address = $address;
@ -48,6 +54,7 @@ abstract class Connection
'client_id' => $clientId ?? '', 'client_id' => $clientId ?? '',
'hostname' => $hostname ?? (static fn (mixed $host): string => \is_string($host) ? $host : '')(gethostname()), 'hostname' => $hostname ?? (static fn (mixed $host): string => \is_string($host) ? $host : '')(gethostname()),
'user_agent' => $userAgent ?? 'nsqphp/'.InstalledVersions::getPrettyVersion('nsq/nsq'), 'user_agent' => $userAgent ?? 'nsqphp/'.InstalledVersions::getPrettyVersion('nsq/nsq'),
'heartbeat_interval' => $heartbeatInterval,
]; ];
$this->logger = $logger ?? new NullLogger(); $this->logger = $logger ?? new NullLogger();

View File

@ -2,11 +2,11 @@
declare(strict_types=1); declare(strict_types=1);
use Nsq\Message;
use Nsq\Consumer; use Nsq\Consumer;
use Nsq\Subscriber;
use Nsq\Producer;
use Nsq\Exception; use Nsq\Exception;
use Nsq\Message;
use Nsq\Producer;
use Nsq\Subscriber;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
final class NsqTest extends TestCase final class NsqTest extends TestCase
@ -16,7 +16,14 @@ final class NsqTest extends TestCase
$producer = new Producer('tcp://localhost:4150'); $producer = new Producer('tcp://localhost:4150');
$producer->pub(__FUNCTION__, __FUNCTION__); $producer->pub(__FUNCTION__, __FUNCTION__);
$consumer = new Consumer('tcp://localhost:4150'); $consumer = new Consumer(
'tcp://localhost:4150',
null,
null,
null,
null,
1000,
);
$subscriber = new Subscriber($consumer); $subscriber = new Subscriber($consumer);
$generator = $subscriber->subscribe(__FUNCTION__, __FUNCTION__, 1); $generator = $subscriber->subscribe(__FUNCTION__, __FUNCTION__, 1);