mirror of
https://github.com/predis/predis.git
synced 2026-10-13 15:28:20 +00:00
373d30b070
Status response objects are needed mostly to make it possible from the
client perspective to differentiate a status response with the payload
"OK" from a normale bulk reply containing "OK".
The biggest change is for commands returning +OK responses: these were
previously translated to TRUE (bool value), but they are now returned
as instances of Predis\Response\Status. Just to illustrate an example
of the possibilities with this change we will use SET since it is the
most widely used command returning +OK:
$response = $client->set('foo', 'bar');
echo $response; // 'OK'
$response == 'OK'; // TRUE
isset($response->ok); // TRUE
$response == true; // TRUE
$response === true; // FALSE
$response instanceof Predis\Response\ObjectInterface; // TRUE
$response instanceof Predis\Response\Status; // TRUE
For those checking responses returned by commands such as SET or PONG,
the breaking change basically lies in the usage of strict comparison:
doing $response === true will now evaluate to FALSE instead of TRUE.
By default Predis caches common status responses such as OK or QUEUED
to lower the memory usage when using pipelines or transactions.
386 lines
10 KiB
PHP
386 lines
10 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of the Predis package.
|
|
*
|
|
* (c) Daniele Alessandri <suppakilla@gmail.com>
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Predis\Connection;
|
|
|
|
use Predis\NotSupportedException;
|
|
use Predis\Command\CommandInterface;
|
|
use Predis\Response;
|
|
|
|
/**
|
|
* This class provides the implementation of a Predis connection that uses the
|
|
* PHP socket extension for network communication and wraps the phpiredis C
|
|
* extension (PHP bindings for hiredis) to parse the Redis protocol. Everything
|
|
* is highly experimental (even the very same phpiredis since it is quite new),
|
|
* so use it at your own risk.
|
|
*
|
|
* This class is mainly intended to provide an optional low-overhead alternative
|
|
* for processing replies from Redis compared to the standard pure-PHP classes.
|
|
* Differences in speed when dealing with short inline replies are practically
|
|
* nonexistent, the actual speed boost is for long multibulk replies when this
|
|
* protocol processor can parse and return replies very fast.
|
|
*
|
|
* For instructions on how to build and install the phpiredis extension, please
|
|
* consult the repository of the project.
|
|
*
|
|
* The connection parameters supported by this class are:
|
|
*
|
|
* - scheme: it can be either 'tcp' or 'unix'.
|
|
* - host: hostname or IP address of the server.
|
|
* - port: TCP port of the server.
|
|
* - timeout: timeout to perform the connection.
|
|
* - read_write_timeout: timeout of read / write operations.
|
|
*
|
|
* @link http://github.com/nrk/phpiredis
|
|
* @author Daniele Alessandri <suppakilla@gmail.com>
|
|
*/
|
|
class PhpiredisConnection extends AbstractConnection
|
|
{
|
|
private $reader;
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function __construct(ConnectionParametersInterface $parameters)
|
|
{
|
|
$this->assertExtensions();
|
|
|
|
parent::__construct($parameters);
|
|
|
|
$this->reader = $this->createReader();
|
|
}
|
|
|
|
/**
|
|
* Disconnects from the server and destroys the underlying resource and the
|
|
* protocol reader resource when PHP's garbage collector kicks in.
|
|
*/
|
|
public function __destruct()
|
|
{
|
|
phpiredis_reader_destroy($this->reader);
|
|
|
|
parent::__destruct();
|
|
}
|
|
|
|
/**
|
|
* Checks if the socket and phpiredis extensions are loaded in PHP.
|
|
*/
|
|
protected function assertExtensions()
|
|
{
|
|
if (!extension_loaded('sockets')) {
|
|
throw new NotSupportedException(
|
|
'The "sockets" extension is required by this connection backend'
|
|
);
|
|
}
|
|
|
|
if (!extension_loaded('phpiredis')) {
|
|
throw new NotSupportedException(
|
|
'The "phpiredis" extension is required by this connection backend'
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
protected function assertParameters(ConnectionParametersInterface $parameters)
|
|
{
|
|
if (isset($parameters->persistent)) {
|
|
throw new NotSupportedException(
|
|
"Persistent connections are not supported by this connection backend"
|
|
);
|
|
}
|
|
|
|
return parent::assertParameters($parameters);
|
|
}
|
|
|
|
/**
|
|
* Creates a new instance of the protocol reader resource.
|
|
*
|
|
* @return resource
|
|
*/
|
|
private function createReader()
|
|
{
|
|
$reader = phpiredis_reader_create();
|
|
|
|
phpiredis_reader_set_status_handler($reader, $this->getStatusHandler());
|
|
phpiredis_reader_set_error_handler($reader, $this->getErrorHandler());
|
|
|
|
return $reader;
|
|
}
|
|
|
|
/**
|
|
* Returns the underlying protocol reader resource.
|
|
*
|
|
* @return resource
|
|
*/
|
|
protected function getReader()
|
|
{
|
|
return $this->reader;
|
|
}
|
|
|
|
/**
|
|
* Returns the handler used by the protocol reader to handle status replies.
|
|
*
|
|
* @return \Closure
|
|
*/
|
|
private function getStatusHandler()
|
|
{
|
|
return function ($payload) {
|
|
return Response\Status::get($payload);
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Returns the handler used by the protocol reader to handle Redis errors.
|
|
*
|
|
* @return \Closure
|
|
*/
|
|
protected function getErrorHandler()
|
|
{
|
|
return function ($payload) {
|
|
return new Response\Error($payload);
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Helper method used to throw exceptions on socket errors.
|
|
*/
|
|
private function emitSocketError()
|
|
{
|
|
$errno = socket_last_error();
|
|
$errstr = socket_strerror($errno);
|
|
|
|
$this->disconnect();
|
|
|
|
$this->onConnectionError(trim($errstr), $errno);
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
protected function createResource()
|
|
{
|
|
$isUnix = $this->parameters->scheme === 'unix';
|
|
$domain = $isUnix ? AF_UNIX : AF_INET;
|
|
$protocol = $isUnix ? 0 : SOL_TCP;
|
|
|
|
$socket = @call_user_func('socket_create', $domain, SOCK_STREAM, $protocol);
|
|
|
|
if (!is_resource($socket)) {
|
|
$this->emitSocketError();
|
|
}
|
|
|
|
$this->setSocketOptions($socket, $this->parameters);
|
|
|
|
return $socket;
|
|
}
|
|
|
|
/**
|
|
* Sets options on the socket resource from the connection parameters.
|
|
*
|
|
* @param resource $socket Socket resource.
|
|
* @param ConnectionParametersInterface $parameters Parameters used to initialize the connection.
|
|
*/
|
|
private function setSocketOptions($socket, ConnectionParametersInterface $parameters)
|
|
{
|
|
if ($parameters->scheme !== 'tcp') {
|
|
return;
|
|
}
|
|
|
|
if (!socket_set_option($socket, SOL_TCP, TCP_NODELAY, 1)) {
|
|
$this->emitSocketError();
|
|
}
|
|
|
|
if (!socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1)) {
|
|
$this->emitSocketError();
|
|
}
|
|
|
|
if (isset($parameters->read_write_timeout)) {
|
|
$rwtimeout = $parameters->read_write_timeout;
|
|
$timeoutSec = floor($rwtimeout);
|
|
$timeoutUsec = ($rwtimeout - $timeoutSec) * 1000000;
|
|
|
|
$timeout = array(
|
|
'sec' => $timeoutSec,
|
|
'usec' => $timeoutUsec,
|
|
);
|
|
|
|
if (!socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, $timeout)) {
|
|
$this->emitSocketError();
|
|
}
|
|
|
|
if (!socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, $timeout)) {
|
|
$this->emitSocketError();
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Gets the address from the connection parameters.
|
|
*
|
|
* @param ConnectionParametersInterface $parameters Parameters used to initialize the connection.
|
|
* @return string
|
|
*/
|
|
private static function getAddress(ConnectionParametersInterface $parameters)
|
|
{
|
|
if ($parameters->scheme === 'unix') {
|
|
return $parameters->path;
|
|
}
|
|
|
|
$host = $parameters->host;
|
|
|
|
if (ip2long($host) === false) {
|
|
if (($addresses = gethostbynamel($host)) === false) {
|
|
$this->onConnectionError("Cannot resolve the address of $host");
|
|
}
|
|
|
|
return $addresses[array_rand($addresses)];
|
|
}
|
|
|
|
return $host;
|
|
}
|
|
|
|
/**
|
|
* Opens the actual connection to the server with a timeout.
|
|
*
|
|
* @param ConnectionParametersInterface $parameters Parameters used to initialize the connection.
|
|
* @return string
|
|
*/
|
|
private function connectWithTimeout(ConnectionParametersInterface $parameters)
|
|
{
|
|
$host = self::getAddress($parameters);
|
|
$socket = $this->getResource();
|
|
|
|
socket_set_nonblock($socket);
|
|
|
|
if (@socket_connect($socket, $host, $parameters->port) === false) {
|
|
$error = socket_last_error();
|
|
|
|
if ($error != SOCKET_EINPROGRESS && $error != SOCKET_EALREADY) {
|
|
$this->emitSocketError();
|
|
}
|
|
}
|
|
|
|
socket_set_block($socket);
|
|
|
|
$null = null;
|
|
$selectable = array($socket);
|
|
|
|
$timeout = $parameters->timeout;
|
|
$timeoutSecs = floor($timeout);
|
|
$timeoutUSecs = ($timeout - $timeoutSecs) * 1000000;
|
|
|
|
$selected = socket_select($selectable, $selectable, $null, $timeoutSecs, $timeoutUSecs);
|
|
|
|
if ($selected === 2) {
|
|
$this->onConnectionError('Connection refused', SOCKET_ECONNREFUSED);
|
|
}
|
|
if ($selected === 0) {
|
|
$this->onConnectionError('Connection timed out', SOCKET_ETIMEDOUT);
|
|
}
|
|
if ($selected === false) {
|
|
$this->emitSocketError();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function connect()
|
|
{
|
|
parent::connect();
|
|
|
|
$this->connectWithTimeout($this->parameters);
|
|
|
|
if ($this->initCmds) {
|
|
foreach ($this->initCmds as $command) {
|
|
$this->executeCommand($command);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function disconnect()
|
|
{
|
|
if ($this->isConnected()) {
|
|
socket_close($this->getResource());
|
|
parent::disconnect();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
protected function write($buffer)
|
|
{
|
|
$socket = $this->getResource();
|
|
|
|
while (($length = strlen($buffer)) > 0) {
|
|
$written = socket_write($socket, $buffer, $length);
|
|
|
|
if ($length === $written) {
|
|
return;
|
|
}
|
|
|
|
if ($written === false) {
|
|
$this->onConnectionError('Error while writing bytes to the server');
|
|
}
|
|
|
|
$buffer = substr($buffer, $written);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function read()
|
|
{
|
|
$socket = $this->getResource();
|
|
$reader = $this->reader;
|
|
|
|
while (PHPIREDIS_READER_STATE_INCOMPLETE === $state = phpiredis_reader_get_state($reader)) {
|
|
if (@socket_recv($socket, $buffer, 4096, 0) === false || $buffer === '') {
|
|
$this->emitSocketError();
|
|
}
|
|
|
|
phpiredis_reader_feed($reader, $buffer);
|
|
}
|
|
|
|
if ($state === PHPIREDIS_READER_STATE_COMPLETE) {
|
|
return phpiredis_reader_get_reply($reader);
|
|
} else {
|
|
$this->onProtocolError(phpiredis_reader_get_error($reader));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function writeRequest(CommandInterface $command)
|
|
{
|
|
$arguments = $command->getArguments();
|
|
array_unshift($arguments, $command->getId());
|
|
|
|
$this->write(phpiredis_format_command($arguments));
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function __wakeup()
|
|
{
|
|
$this->assertExtensions();
|
|
$this->reader = $this->createReader();
|
|
}
|
|
}
|