Kafka\Client::getStream PHP Method

getStream() public method

get broker broker connect
public getStream ( string $host, null $lockKey = null ) : array
$host string
$lockKey null
return array
    public function getStream($host, $lockKey = null)
    {
        if (!$lockKey) {
            $lockKey = uniqid($host);
        }
        list($hostname, $port) = explode(':', $host);
        // find unlock stream
        if (isset(self::$stream[$host])) {
            foreach (self::$stream[$host] as $key => $info) {
                if ($info['locked']) {
                    continue;
                } else {
                    self::$stream[$host][$key]['locked'] = true;
                    $info['stream']->connect();
                    return array('key' => $key, 'stream' => $info['stream']);
                }
            }
        }
        // no idle stream
        $stream = new \Kafka\Socket($hostname, $port, $this->getStreamOption('RecvTimeoutSec'), $this->getStreamOption('RecvTimeoutUsec'), $this->getStreamOption('SendTimeoutSec'), $this->getStreamOption('SendTimeoutUsec'));
        $stream->connect();
        self::$stream[$host][$lockKey] = array('locked' => true, 'stream' => $stream);
        return array('key' => $lockKey, 'stream' => $stream);
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * @param array $topics
  */
 private function loadTopicDetail(array $topics)
 {
     if ($this->client === null) {
         throw new \Kafka\Exception('client was not provided');
     }
     $response = null;
     foreach ($this->hostList as $host) {
         try {
             $response = null;
             $stream = $this->client->getStream($host);
             $conn = $stream['stream'];
             $encoder = new \Kafka\Protocol\Encoder($conn);
             $encoder->metadataRequest($topics);
             $decoder = new \Kafka\Protocol\Decoder($conn);
             $response = $decoder->metadataResponse();
             $this->client->freeStream($stream['key']);
             break;
         } catch (\Kafka\Exception $e) {
             // keep trying
         }
     }
     if ($response) {
         // Merge arrays using "+" operator to preserve key (which are broker IDs)
         // instead of array_merge (which reindex numeric keys)
         $this->brokers = $response['brokers'] + $this->brokers;
         $this->topics = array_merge($response['topics'], $this->topics);
     } else {
         throw new \Kafka\Exception('Could not connect to any kafka brokers');
     }
 }