* Connects the socket by iterating through all the servers in the pool * and trying to find one that works. */
()
| 182 | * and trying to find one that works. |
| 183 | */ |
| 184 | public function open() |
| 185 | { |
| 186 | // Check if we want order randomization |
| 187 | if ($this->randomize_) { |
| 188 | shuffle($this->servers_); |
| 189 | } |
| 190 | |
| 191 | // Count servers to identify the "last" one |
| 192 | $numServers = count($this->servers_); |
| 193 | |
| 194 | for ($i = 0; $i < $numServers; ++$i) { |
| 195 | // This extracts the $host and $port variables |
| 196 | extract($this->servers_[$i]); |
| 197 | |
| 198 | // Check APCu cache for a record of this server being down |
| 199 | $failtimeKey = 'thrift_failtime:' . $host . ':' . $port . '~'; |
| 200 | |
| 201 | // Cache miss? Assume it's OK |
| 202 | $lastFailtime = $this->apcuFetch($failtimeKey); |
| 203 | if ($lastFailtime === false) { |
| 204 | $lastFailtime = 0; |
| 205 | } |
| 206 | |
| 207 | $retryIntervalPassed = false; |
| 208 | |
| 209 | // Cache hit...make sure enough the retry interval has elapsed |
| 210 | if ($lastFailtime > 0) { |
| 211 | $elapsed = time() - $lastFailtime; |
| 212 | if ($elapsed > $this->retryInterval_) { |
| 213 | $retryIntervalPassed = true; |
| 214 | if ($this->debug_) { |
| 215 | call_user_func( |
| 216 | $this->debugHandler_, |
| 217 | 'TSocketPool: retryInterval ' . |
| 218 | '(' . $this->retryInterval_ . ') ' . |
| 219 | 'has passed for host ' . $host . ':' . $port |
| 220 | ); |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | // Only connect if not in the middle of a fail interval, OR if this |
| 226 | // is the LAST server we are trying, just hammer away on it |
| 227 | $isLastServer = false; |
| 228 | if ($this->alwaysTryLast_) { |
| 229 | $isLastServer = ($i == ($numServers - 1)); |
| 230 | } |
| 231 | |
| 232 | if (($lastFailtime === 0) || |
| 233 | ($isLastServer) || |
| 234 | ($lastFailtime > 0 && $retryIntervalPassed)) { |
| 235 | // Set underlying TSocket params to this one |
| 236 | $this->host_ = $host; |
| 237 | $this->port_ = $port; |
| 238 | |
| 239 | // Try up to numRetries_ connections per server |
| 240 | for ($attempt = 0; $attempt < $this->numRetries_; $attempt++) { |
| 241 | try { |