11
11
use ProxyManager \Proxy \AccessInterceptorInterface ;
12
12
use Redis ;
13
13
use RedisCluster ;
14
+ use RedisException ;
14
15
use RedisSentinel ;
15
16
use ReflectionClass ;
16
17
use ReflectionMethod ;
18
+ use Relay \Exception as RelayException ;
17
19
use Relay \Relay ;
18
20
use Relay \Sentinel ;
19
21
use Snc \RedisBundle \DependencyInjection \Configuration \RedisDsn ;
@@ -102,19 +104,34 @@ public function create(string $class, array $dsns, array $options, string $alias
102
104
}
103
105
104
106
/**
105
- * @param class-string $class
106
- * @param list<RedisDsn> $dsns
107
- * @param array{service: ?string} $options
107
+ * @param class-string $class
108
+ * @param list<RedisDsn> $dsns
109
+ * @param array{service: ?string, connection_persistent: ?bool, connection_timeout: ?string, read_write_timeout: ?string } $options
108
110
*
109
111
* @return Redis|Relay
110
112
*/
111
113
private function createClientFromSentinel (string $ class , array $ dsns , string $ alias , array $ options , bool $ loggingEnabled )
112
114
{
113
- $ isRelay = is_a ($ class , Sentinel::class, true );
114
- $ sentinelClass = $ isRelay ? Sentinel::class : RedisSentinel::class;
115
+ $ isRelay = is_a ($ class , Sentinel::class, true );
116
+ $ sentinelClass = $ isRelay ? Sentinel::class : RedisSentinel::class;
117
+ $ masterName = $ options ['service ' ];
118
+ $ connectionTimeout = $ options ['connection_timeout ' ] ?? 0 ;
119
+ $ connectionPersistent = $ options ['connection_persistent ' ] ? $ masterName : null ;
120
+ $ readTimeout = $ options ['read_write_timeout ' ] ?? 0 ;
115
121
116
122
foreach ($ dsns as $ dsn ) {
117
- $ address = (new $ sentinelClass ($ dsn ->getHost (), (int ) $ dsn ->getPort ()))->getMasterAddrByName ($ options ['service ' ]);
123
+ try {
124
+ $ address = (new $ sentinelClass (
125
+ $ dsn ->getHost (),
126
+ (int ) $ dsn ->getPort (),
127
+ $ connectionTimeout ,
128
+ $ connectionPersistent ,
129
+ 5 , // retry interval
130
+ $ readTimeout ,
131
+ ))->getMasterAddrByName ($ masterName );
132
+ } catch (RedisException | RelayException $ e ) {
133
+ continue ;
134
+ }
118
135
119
136
if (!$ address ) {
120
137
continue ;
@@ -139,7 +156,7 @@ public function __construct(string $dsn, string $host, int $port)
139
156
throw new InvalidArgumentException (
140
157
sprintf (
141
158
'Failed to retrieve master information from sentinel %s and dsn %s. ' ,
142
- var_export ($ options [ ' service ' ] , true ),
159
+ var_export ($ masterName , true ),
143
160
var_export ($ dsns , true ),
144
161
),
145
162
);
0 commit comments