/** * Create connector object * @param array $config * @return TTransport */ private static function getConnector($config) { $class = isset($config['class']) ? $config['class'] : ''; $param = isset($config['param']) ? $config['param'] : array(); switch ($class) { case 'THttpClient': if (!isset($param['host'])) { throw new Exception('Bad Thrift transport config'); } $host = $param['host']; $port = isset($param['port']) ? $param['port'] : 80; $uri = isset($param['uri']) ? $param['uri'] : ''; $scheme = isset($param['scheme']) ? $param['scheme'] : 'http'; $timeout = isset($param['timeout']) ? $param['timeout'] : null; $connector = new THttpClient($url, $port, $uri, $scheme); $connector->setTimeoutSecs($timeout); $parameters = sprintf('host = "%s", port = %d, uri = "%s", scheme = "%s", timeout = %d', $host, $port, $uri, $scheme, $timeout); break; case 'TMemoryBuffer': $buf = isset($param['buf']) ? $param['buf'] : ''; $connector = new TMemoryBuffer($buf); $parameters = sprintf('buf = "%s"', $buf); break; case 'TPhpStream': if (!isset($param['mode'])) { throw new Exception('Bad Thrift transport config'); } $mode = $param['mode']; $connector = new TPhpStream($mode); $parameters = sprintf('mode = %d', $mode); break; case 'TSocket': $host = isset($param['host']) ? $param['host'] : 'localhost'; $port = isset($param['port']) ? $param['port'] : 9090; $persist = isset($param['persist']) ? $param['persist'] : false; $send_timeout = isset($param['send_timeout']) ? $param['send_timeout'] : 100; $recv_timeout = isset($param['recv_timeout']) ? $param['recv_timeout'] : 750; $connector = new TSocket($host, $port, $persist); $connector->setSendTimeout($send_timeout); $connector->setRecvTimeout($recv_timeout); $parameters = sprintf('host = "%s", port = %d, persist = %s, send_timeout = %d, recv_timeout = %d', $host, $port, $persist ? 'true' : 'false', $send_timeout, $recv_timeout); break; case 'TSocketPool': $hosts = isset($param['hosts']) ? $param['hosts'] : array('localhost'); $ports = isset($param['ports']) ? $param['ports'] : array(9090); $persist = isset($param['persist']) ? $param['persist'] : false; $send_timeout = isset($param['send_timeout']) ? $param['send_timeout'] : 100; $recv_timeout = isset($param['recv_timeout']) ? $param['recv_timeout'] : 750; $connector = new TSocketPool($hosts, $ports, $persist); $connector->setSendTimeout($send_timeout); $connector->setRecvTimeout($recv_timeout); $parameters = sprintf('hosts = ("%s"), ports = (%d), persist = %s, send_timeout = %d, recv_timeout = %d', implode('","', $hosts), implode('","', $ports), $persist ? 'true' : 'false', $send_timeout, $recv_timeout); break; default: throw new Exception('Unknown connector: ' . $class); } sfContext::getInstance()->getLogger()->info(sprintf('{sfThriftPlugin}Create %s connector with parameters: %s', $class, $parameters)); return $connector; }
public function flush() { if (\hacklib_cast_as_boolean($this->randomize_)) { shuffle($this->servers_); } foreach ($this->servers_ as $server) { $this->host_ = $server[0]; $this->port_ = $server[1]; $j = $this->numTries_; while ($j > 0) { try { parent::flush(); return; } catch (TTransportException $e) { if (\hacklib_cast_as_boolean($this->debug_)) { call_user_func($this->debugHandler_, $e->getMessage()); } --$j; } } } $this->host_ = ""; $this->port_ = 0; $error = "THttpClientPool: Could not connect to any of the servers " . "in the pool"; throw new TTransportException($error, TTransportException::NOT_OPEN); }
protected function getThriftClient($clientClass, $url) { $parts = parse_url($url); if (!isset($parts['port'])) { if ($parts['scheme'] === 'https') { $parts['port'] = 443; } else { $parts['port'] = 80; } } $httpClient = new \THttpClient($parts['host'], $parts['port'], $parts['path'], $parts['scheme']); $httpClient->addHeaders(array('User-Agent' => $this->userAgentId . ' / ' . $this->getSdkVersion() . '; PHP / ' . phpversion())); $thriftProtocol = new \TBinaryProtocol($httpClient); return new $clientClass($thriftProtocol, $thriftProtocol); }
/** * Trys to open a connection and send the actual HTTP request to the first * available server in the pool. */ public function flush() { if ($this->randomize_) { shuffle($this->servers_); } foreach ($this->servers_ as $server) { $this->host_ = $server['host']; $this->port_ = $server['port']; $j = $this->numTries_; while ($j > 0) { try { parent::flush(); return; } catch (TTransportException $e) { if ($this->debug_) { call_user_func($this->debugHandler_, $e->getMessage()); } --$j; } } } $this->host_ = null; $this->port_ = null; $error = 'THttpClientPool: Could not connect to any of the servers ' . 'in the pool'; throw new TTransportException($error, TTransportException::NOT_OPEN); }