@@ -115,7 +115,7 @@ public function isWaiting(): bool
115
115
return $ this ->_status === self ::STATUS_WAITING ;
116
116
}
117
117
118
- protected function getConnectionTarget (): array
118
+ public function getConnectionTarget (): array
119
119
{
120
120
$ host = $ this ->uri ->getHost ();
121
121
if (empty ($ host )) {
@@ -128,6 +128,12 @@ protected function getConnectionTarget(): array
128
128
return ['host ' => $ host , 'port ' => $ port , 'ssl ' => $ ssl ];
129
129
}
130
130
131
+ public function shouldRecycleClient ($ client )
132
+ {
133
+ $ connectionInfo = $ this ->getConnectionTarget ();
134
+
135
+ return (!$ client || ($ client ->host !== $ connectionInfo ['host ' ] || $ client ->port !== $ connectionInfo ['port ' ]));
136
+ }
131
137
/** @return null|bool */
132
138
public function getPool ()
133
139
{
@@ -498,24 +504,20 @@ public function exec()
498
504
return $ ret ;
499
505
}
500
506
501
- /** get connection info */
502
- list ($ host , $ port , $ ssl ) = array_values ($ this ->getConnectionTarget ());
503
- if ($ this ->client && ($ this ->client ->host !== $ host || $ this ->client ->port !== $ port )) {
507
+
508
+ if ($ this ->client && ($ this ->shouldRecycleClient ($ this ->client ))) {
504
509
// target maybe changed
505
510
$ this ->tryToRevertClientToPool ();
506
511
}
507
512
if (!$ this ->client ) {
513
+ /** get connection info */
514
+ $ connectionInfo = $ this ->getConnectionTarget ();
508
515
/** create a new coroutine client */
509
516
$ client_pool = ClientPool::getInstance ();
510
- if ($ this ->use_pool && $ client = $ client_pool ->getEx ($ host , $ port )) {
517
+ if ($ this ->use_pool && $ client = $ client_pool ->getEx ($ connectionInfo [ ' host ' ] , $ connectionInfo [ ' port ' ] )) {
511
518
$ this ->client = $ client ;
512
519
} else {
513
- $ options = [
514
- 'host ' => $ host ,
515
- 'port ' => $ port ,
516
- 'ssl ' => $ ssl
517
- ];
518
- $ this ->client = $ client_pool ->createEx ($ options , !$ this ->use_pool );
520
+ $ this ->client = $ client_pool ->createEx ($ connectionInfo , !$ this ->use_pool );
519
521
}
520
522
}
521
523
@@ -748,7 +750,9 @@ public function recv()
748
750
}
749
751
$ this ->client ->body = '' ;
750
752
751
- $ this ->tryToRevertClientToPool ();
753
+ if ($ this ->use_pool ){
754
+ $ this ->tryToRevertClientToPool ();
755
+ }
752
756
753
757
return $ response ;
754
758
}
0 commit comments