mirror of
https://github.com/predis/predis.git
synced 2026-08-30 20:21:31 +00:00
No need for socket_import_stream() to set TCP_NODELAY since PHP 7.1.
This commit is contained in:
@@ -109,14 +109,13 @@ class PhpiredisStreamConnection extends StreamConnection
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function createStreamSocket(ParametersInterface $parameters, $address, $flags, $context = null)
|
||||
protected function createStreamSocket(ParametersInterface $parameters, $address, $flags)
|
||||
{
|
||||
$socket = null;
|
||||
$timeout = (isset($parameters->timeout) ? (float) $parameters->timeout : 5.0);
|
||||
$context = stream_context_create(['socket' => ['tcp_nodelay' => (bool) $parameters->tcp_nodelay]]);
|
||||
|
||||
$resource = @stream_socket_client($address, $errno, $errstr, $timeout, $flags);
|
||||
|
||||
if (!$resource) {
|
||||
if (!$resource = @stream_socket_client($address, $errno, $errstr, $timeout, $flags, $context)) {
|
||||
$this->onConnectionError(trim($errstr), $errno);
|
||||
}
|
||||
|
||||
|
||||
@@ -103,8 +103,9 @@ class StreamConnection extends AbstractConnection
|
||||
protected function createStreamSocket(ParametersInterface $parameters, $address, $flags)
|
||||
{
|
||||
$timeout = (isset($parameters->timeout) ? (float) $parameters->timeout : 5.0);
|
||||
$context = stream_context_create(['socket' => ['tcp_nodelay' => (bool) $parameters->tcp_nodelay]]);
|
||||
|
||||
if (!$resource = @stream_socket_client($address, $errno, $errstr, $timeout, $flags)) {
|
||||
if (!$resource = @stream_socket_client($address, $errno, $errstr, $timeout, $flags, $context)) {
|
||||
$this->onConnectionError(trim($errstr), $errno);
|
||||
}
|
||||
|
||||
@@ -116,11 +117,6 @@ class StreamConnection extends AbstractConnection
|
||||
stream_set_timeout($resource, $timeoutSeconds, $timeoutUSeconds);
|
||||
}
|
||||
|
||||
if (isset($parameters->tcp_nodelay) && function_exists('socket_import_stream')) {
|
||||
$socket = socket_import_stream($resource);
|
||||
socket_set_option($socket, SOL_TCP, TCP_NODELAY, (int) $parameters->tcp_nodelay);
|
||||
}
|
||||
|
||||
return $resource;
|
||||
}
|
||||
|
||||
|
||||
@@ -194,4 +194,46 @@ class PhpiredisStreamConnectionTest extends PredisConnectionTestCase
|
||||
|
||||
$this->assertNotSame($connection1->getResource(), $connection2->getResource());
|
||||
}
|
||||
|
||||
/**
|
||||
* @group connected
|
||||
*/
|
||||
public function testTcpNodelayParameterSetsContextFlagWhenTrue()
|
||||
{
|
||||
$connection = $this->createConnectionWithParams(['tcp_nodelay' => true]);
|
||||
$options = stream_context_get_options($connection->getResource());
|
||||
|
||||
$this->assertIsArray($options);
|
||||
$this->assertArrayHasKey('socket', $options);
|
||||
$this->assertArrayHasKey('tcp_nodelay', $options['socket']);
|
||||
$this->assertTrue($options['socket']['tcp_nodelay']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group connected
|
||||
*/
|
||||
public function testTcpNodelayParameterDoesNotSetContextFlagWhenFalse()
|
||||
{
|
||||
$connection = $this->createConnectionWithParams(['tcp_nodelay' => false]);
|
||||
$options = stream_context_get_options($connection->getResource());
|
||||
|
||||
$this->assertIsArray($options);
|
||||
$this->assertArrayHasKey('socket', $options);
|
||||
$this->assertArrayHasKey('tcp_nodelay', $options['socket']);
|
||||
$this->assertFalse($options['socket']['tcp_nodelay']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group connected
|
||||
*/
|
||||
public function testTcpDelayContextFlagIsNotSetByDefault()
|
||||
{
|
||||
$connection = $this->createConnectionWithParams([]);
|
||||
$options = stream_context_get_options($connection->getResource());
|
||||
|
||||
$this->assertIsArray($options);
|
||||
$this->assertArrayHasKey('socket', $options);
|
||||
$this->assertArrayHasKey('tcp_nodelay', $options['socket']);
|
||||
$this->assertFalse($options['socket']['tcp_nodelay']);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -132,4 +132,46 @@ class StreamConnectionTest extends PredisConnectionTestCase
|
||||
|
||||
$this->assertNotSame($connection1->getResource(), $connection2->getResource());
|
||||
}
|
||||
|
||||
/**
|
||||
* @group connected
|
||||
*/
|
||||
public function testTcpNodelayParameterSetsContextFlagWhenTrue()
|
||||
{
|
||||
$connection = $this->createConnectionWithParams(['tcp_nodelay' => true]);
|
||||
$options = stream_context_get_options($connection->getResource());
|
||||
|
||||
$this->assertIsArray($options);
|
||||
$this->assertArrayHasKey('socket', $options);
|
||||
$this->assertArrayHasKey('tcp_nodelay', $options['socket']);
|
||||
$this->assertTrue($options['socket']['tcp_nodelay']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group connected
|
||||
*/
|
||||
public function testTcpNodelayParameterDoesNotSetContextFlagWhenFalse()
|
||||
{
|
||||
$connection = $this->createConnectionWithParams(['tcp_nodelay' => false]);
|
||||
$options = stream_context_get_options($connection->getResource());
|
||||
|
||||
$this->assertIsArray($options);
|
||||
$this->assertArrayHasKey('socket', $options);
|
||||
$this->assertArrayHasKey('tcp_nodelay', $options['socket']);
|
||||
$this->assertFalse($options['socket']['tcp_nodelay']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group connected
|
||||
*/
|
||||
public function testTcpDelayContextFlagIsNotSetByDefault()
|
||||
{
|
||||
$connection = $this->createConnectionWithParams([]);
|
||||
$options = stream_context_get_options($connection->getResource());
|
||||
|
||||
$this->assertIsArray($options);
|
||||
$this->assertArrayHasKey('socket', $options);
|
||||
$this->assertArrayHasKey('tcp_nodelay', $options['socket']);
|
||||
$this->assertFalse($options['socket']['tcp_nodelay']);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user