@@ -39,6 +39,7 @@ class Connection
3939 'tls ' => false ,
4040 'redeliver_timeout ' => 3600 , // Timeout before redeliver messages still in pending state (seconds)
4141 'claim_interval ' => 60000 , // Interval by which pending/abandoned messages should be checked
42+ 'lazy ' => false ,
4243 ];
4344
4445 private $ connection ;
@@ -61,23 +62,42 @@ public function __construct(array $configuration, array $connectionCredentials =
6162 throw new LogicException ('The redis transport requires php-redis 4.3.0 or higher. ' );
6263 }
6364
64- $ this -> connection = $ redis ?: new \ Redis () ;
65- $ this -> connection -> connect ( $ connectionCredentials [ ' host ' ] ?? ' 127.0.0.1 ' , $ connectionCredentials ['port ' ] ?? 6379 ) ;
66- $ this -> connection -> setOption (\Redis:: OPT_SERIALIZER , $ redisOptions ['serializer ' ] ?? \Redis::SERIALIZER_PHP ) ;
67-
65+ $ host = $ connectionCredentials [ ' host ' ] ?? ' 127.0.0.1 ' ;
66+ $ port = $ connectionCredentials ['port ' ] ?? 6379 ;
67+ $ serializer = $ redisOptions ['serializer ' ] ?? \Redis::SERIALIZER_PHP ;
68+ $ dbIndex = $ configuration [ ' dbindex ' ] ?? self :: DEFAULT_OPTIONS [ ' dbindex ' ];
6869 $ auth = $ connectionCredentials ['auth ' ] ?? null ;
6970 if ('' === $ auth ) {
7071 $ auth = null ;
7172 }
7273
73- if (null !== $ auth && !$ this ->connection ->auth ($ auth )) {
74- throw new InvalidArgumentException ('Redis connection failed: ' .$ this ->connection ->getLastError ());
74+ $ initializer = static function ($ redis ) use ($ host , $ port , $ auth , $ serializer , $ dbIndex ) {
75+ $ redis ->connect ($ host , $ port );
76+ $ redis ->setOption (\Redis::OPT_SERIALIZER , $ serializer );
77+
78+ if (null !== $ auth && !$ redis ->auth ($ auth )) {
79+ throw new InvalidArgumentException ('Redis connection failed: ' .$ redis ->getLastError ());
80+ }
81+
82+ if ($ dbIndex && !$ redis ->select ($ dbIndex )) {
83+ throw new InvalidArgumentException ('Redis connection failed: ' .$ redis ->getLastError ());
84+ }
85+
86+ return true ;
87+ };
88+
89+ if (null === $ redis ) {
90+ $ redis = new \Redis ();
7591 }
7692
77- if (($ dbIndex = $ configuration ['dbindex ' ] ?? self ::DEFAULT_OPTIONS ['dbindex ' ]) && !$ this ->connection ->select ($ dbIndex )) {
78- throw new InvalidArgumentException ('Redis connection failed: ' .$ this ->connection ->getLastError ());
93+ if ($ configuration ['lazy ' ] ?? self ::DEFAULT_OPTIONS ['lazy ' ]) {
94+ $ redis = new RedisProxy ($ redis , $ initializer );
95+ } else {
96+ $ initializer ($ redis );
7997 }
8098
99+ $ this ->connection = $ redis ;
100+
81101 foreach (['stream ' , 'group ' , 'consumer ' ] as $ key ) {
82102 if (isset ($ configuration [$ key ]) && '' === $ configuration [$ key ]) {
83103 throw new InvalidArgumentException (sprintf ('"%s" should be configured, got an empty string. ' , $ key ));
@@ -165,6 +185,7 @@ public static function fromDsn(string $dsn, array $redisOptions = [], \Redis $re
165185 'stream ' => $ redisOptions ['stream ' ] ?? null ,
166186 'group ' => $ redisOptions ['group ' ] ?? null ,
167187 'consumer ' => $ redisOptions ['consumer ' ] ?? null ,
188+ 'lazy ' => $ redisOptions ['lazy ' ] ?? self ::DEFAULT_OPTIONS ['lazy ' ],
168189 'auto_setup ' => $ autoSetup ,
169190 'stream_max_entries ' => $ maxEntries ,
170191 'delete_after_ack ' => $ deleteAfterAck ,
0 commit comments