Skip to content

Commit

Permalink
Merge pull request #130 from PandaLIU-1111/ping
Browse files Browse the repository at this point in the history
Added ping
  • Loading branch information
kedacoder authored Mar 12, 2024
2 parents c524b03 + 5fddd97 commit f7e581a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/redis-subscriber/src/CommandInvoker.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ class CommandInvoker
*/
protected $messageChannel;

/**
* @var Channel
*/
private $pingChannel;

/**
* CommandInvoker constructor.
* @param Connection $connection
Expand All @@ -41,6 +46,7 @@ public function __construct(Connection $connection)
{
$this->connection = $connection;
$this->resultChannel = new Channel();
$this->pingChannel = new Channel();
$this->messageChannel = new Channel(100);
Coroutine::create(function () use ($connection) {
$this->receive($connection);
Expand Down Expand Up @@ -106,6 +112,12 @@ public function receive(Connection $connection)
$buffer = null;
continue;
}

if ($type == 'pong' && count($buffer) == 5) {
$this->pingChannel->push('pong');
$buffer = null;
continue;
}
}
}

Expand Down Expand Up @@ -153,6 +165,18 @@ public function interrupt()
return true;
}

/**
* Ping
* @param int $timeout
* @return string
* @throws \Swoole\Exception
*/
public function ping(int $timeout = 1)
{
$this->connection->send(Resp::build('ping'));
return $this->pingChannel->pop($timeout);
}

/**
* Print error
* @param \Throwable $ex
Expand Down
10 changes: 10 additions & 0 deletions src/redis-subscriber/src/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,14 @@ public function close()
$this->commandInvoker->interrupt();
}

/**
* Ping
* @param int $timeout
* @throws \Swoole\Exception
*/
public function ping(int $timeout = 1)
{
return $this->commandInvoker->ping($timeout);
}

}

0 comments on commit f7e581a

Please sign in to comment.