Skip to content

Commit 0a5e7a4

Browse files
authored
Merge pull request #63 from cheprasov/connection_timeout
v180 Added config for connection
2 parents 93fd646 + a1b33c0 commit 0a5e7a4

File tree

8 files changed

+73
-12
lines changed

8 files changed

+73
-12
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ php:
66
- '5.6'
77
- '7.0'
88
- '7.1'
9+
- '7.2'
910
- hhvm
1011

1112
matrix:

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
## CHANGELOG
22

3+
### v1.8.0 (2018-03-08)
4+
- Added configuration for connection: timeout & flags.
5+
36
### v1.7.2 (2017-08-19)
47
- Fixed bug of ClientFactory with default client version
58

README.md

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT)
22
[![Latest Stable Version](https://poser.pugx.org/cheprasov/php-redis-client/v/stable)](https://packagist.org/packages/cheprasov/php-redis-client)
33
[![Total Downloads](https://poser.pugx.org/cheprasov/php-redis-client/downloads)](https://packagist.org/packages/cheprasov/php-redis-client)
4-
# RedisClient v1.7.2 for PHP >= 5.5
4+
# RedisClient v1.8.0 for PHP >= 5.5
55

66
## About
77
RedisClient is a fast, fully-functional and user-friendly client for Redis, optimized for performance. RedisClient supports the latest versions of Redis starting from __2.6__ to __4.0__
@@ -17,7 +17,7 @@ RedisClient is a fast, fully-functional and user-friendly client for Redis, opti
1717
- Easy to use with IDE, client has PHPDocs for all supported versions.
1818
- By default, the client works with the latest stable version of Redis (4.0).
1919
- The client was tested on the next latest versions of Redis: 2.6.17, 2.8.24, 3.0.7, 3.2.8, 4.0.8.
20-
- Also, the client was tested on PHP 5.5, 5.6, 7.0, 7.1, HHVM.
20+
- Also, the client was tested on PHP 5.5, 5.6, 7.0, 7.1, 7.2, HHVM.
2121

2222
## Usage
2323

@@ -29,8 +29,23 @@ $config = [
2929
'server' => '127.0.0.1:6379',
3030

3131
// Optional. Default = 1
32+
// The timeout for reading/writing data over the socket
3233
'timeout' => 2,
3334

35+
// Optional. Default = null
36+
// See more here: http://php.net/manual/en/function.stream-socket-client.php
37+
'connection' => [
38+
// Optional. Default = ini_get("default_socket_timeout")
39+
// The timeout only applies while making connecting the socket
40+
'timeout' => 2,
41+
42+
// Optional. Default = STREAM_CLIENT_CONNECT
43+
// Bitmask field which may be set to any combination of connection flags.
44+
// Currently the select of connection flags is limited to STREAM_CLIENT_CONNECT (default),
45+
// STREAM_CLIENT_ASYNC_CONNECT and STREAM_CLIENT_PERSISTENT.
46+
'flags' => STREAM_CLIENT_CONNECT
47+
],
48+
3449
// Optional. Specify version to avoid some unexpected errors.
3550
'version' => '3.2.8',
3651

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cheprasov/php-redis-client",
3-
"version": "1.7.2",
3+
"version": "1.8.0",
44
"description": "Php client for Redis. It is a fast, fully-functional and user-friendly client for Redis, optimized for performance. RedisClient supports the latest versions of Redis starting from 2.6 to 4.0",
55
"homepage": "http://github.com/cheprasov/php-redis-client",
66
"minimum-stability": "stable",

src/RedisClient/Client/AbstractRedisClient.php

+7-5
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@
2626

2727
abstract class AbstractRedisClient {
2828

29-
const VERSION = '1.7.2';
29+
const VERSION = '1.8.0';
3030

31-
const CONFIG_SERVER = 'server';
32-
const CONFIG_TIMEOUT = 'timeout';
31+
const CONFIG_SERVER = 'server';
32+
const CONFIG_TIMEOUT = 'timeout';
3333
const CONFIG_DATABASE = 'database';
3434
const CONFIG_PASSWORD = 'password';
35-
const CONFIG_CLUSTER = 'cluster';
36-
const CONFIG_VERSION = 'version';
35+
const CONFIG_CLUSTER = 'cluster';
36+
const CONFIG_VERSION = 'version';
37+
const CONFIG_CONNECTION = 'connection';
3738

3839
/**
3940
* Default configuration
@@ -50,6 +51,7 @@ abstract class AbstractRedisClient {
5051
'init_on_error_moved' => false,
5152
'timeout_on_error_tryagain' => 0.05,
5253
],
54+
self::CONFIG_CONNECTION => null,
5355
];
5456

5557
/**

src/RedisClient/Connection/ConnectionFactory.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ class ConnectionFactory {
1515
/**
1616
* @param string $server
1717
* @param int $timeout
18+
* @param array|null $connection
1819
* @param callable $onConnect
1920
* @return StreamConnection
2021
*/
21-
public static function createStreamConnection($server, $timeout, $onConnect = null) {
22-
$Connection = new StreamConnection($server, $timeout);
22+
public static function createStreamConnection($server, $timeout, $connection = null, $onConnect = null) {
23+
$Connection = new StreamConnection($server, $timeout, $connection);
2324
if ($onConnect) {
2425
$Connection->onConnect($onConnect);
2526
}

src/RedisClient/Connection/StreamConnection.php

+40-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ class StreamConnection implements ConnectionInterface {
2929
*/
3030
protected $timeout;
3131

32+
/**
33+
* @var int|float
34+
*/
35+
protected $connection_timeout;
36+
37+
/**
38+
* @var int
39+
*/
40+
protected $connection_flags;
41+
3242
/**
3343
* @var callable
3444
*/
@@ -37,10 +47,12 @@ class StreamConnection implements ConnectionInterface {
3747
/**
3848
* @param string $server
3949
* @param int|float|null $timeout
50+
* @param array|null $connection
4051
*/
41-
public function __construct($server, $timeout = null) {
52+
public function __construct($server, $timeout = null, array $connection = null) {
4253
$this->setServer($server);
4354
$this->setTimeout($timeout);
55+
$this->setConnection($connection);
4456
}
4557

4658
/**
@@ -61,6 +73,23 @@ protected function setTimeout($timeout = null) {
6173
$this->timeout = $timeout ? ceil($timeout * 1000000) : null;
6274
}
6375

76+
/**
77+
* @param null|array $connection
78+
*/
79+
protected function setConnection(array $connection = null) {
80+
if (isset($connection['timeout'])) {
81+
$this->connection_timeout = $connection['timeout'];
82+
} else {
83+
$this->connection_timeout = ini_get('default_socket_timeout');
84+
}
85+
86+
if (isset($connection['flags'])) {
87+
$this->connection_flags = $connection['flags'];
88+
} else {
89+
$this->connection_flags = STREAM_CLIENT_CONNECT;
90+
}
91+
}
92+
6493
/**
6594
*
6695
*/
@@ -92,7 +121,16 @@ protected function getResource() {
92121
if (!$this->resource) {
93122
$errno = null;
94123
$errstr = null;
95-
if (!$this->resource = stream_socket_client($this->server, $errno, $errstr)) {
124+
125+
$this->resource = stream_socket_client(
126+
$this->server,
127+
$errno,
128+
$errstr,
129+
$this->connection_timeout,
130+
$this->connection_flags
131+
);
132+
133+
if (!$this->resource) {
96134
throw new ConnectionException('Unable to connect to '. $this->server . ' ('. $errstr .')');
97135
}
98136
if (isset($this->timeout)) {

src/RedisClient/Protocol/ProtocolFactory.php

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public static function createRedisProtocol(AbstractRedisClient $RedisClient, $co
3434
ConnectionFactory::createStreamConnection(
3535
$config[AbstractRedisClient::CONFIG_SERVER],
3636
$config[AbstractRedisClient::CONFIG_TIMEOUT],
37+
$config[AbstractRedisClient::CONFIG_CONNECTION],
3738
$onConnect
3839
)
3940
);

0 commit comments

Comments
 (0)