Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Broker.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use InvalidArgumentException;
use longlang\phpkafka\Client\ClientInterface;
use longlang\phpkafka\Consumer\ConsumerConfig;
use longlang\phpkafka\Exception\NoAliveBrokerException;
use longlang\phpkafka\Producer\ProducerConfig;
use longlang\phpkafka\Protocol\ErrorCode;
use longlang\phpkafka\Protocol\Metadata\MetadataRequest;
Expand Down Expand Up @@ -92,6 +93,11 @@ public function updateBrokers(): void
foreach ($response->getBrokers() as $broker) {
$brokers[$broker->getNodeId()] = $broker->getHost() . ':' . $broker->getPort();
}

if (empty($brokers)) {
throw new NoAliveBrokerException('No brokers are available');
}

$this->setBrokers($brokers);
}

Expand Down
9 changes: 9 additions & 0 deletions src/Exception/NoAliveBrokerException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

所有类应该统一加上declare(strict_types=1);

declare(strict_types=1);

namespace longlang\phpkafka\Exception;

class NoAliveBrokerException extends \Exception
{
}
6 changes: 3 additions & 3 deletions src/Producer/Producer.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@ public function __construct(ProducerConfig $config)
$this->partitioner = new $class();
}

public function send(string $topic, ?string $value, ?string $key = null, array $headers = [], ?int $partitionIndex = null, ?int $brokerId = null): void
public function send(string $topic, ?string $value, ?string $key = null, array $headers = [], ?int $partitionIndex = null): void
{
$message = new ProduceMessage($topic, $value, $key, $headers, $partitionIndex);
$messages = [$message];
$this->sendBatch($messages, $brokerId);
$this->sendBatch($messages);
}

/**
* @param ProduceMessage[] $messages
*/
public function sendBatch(array $messages, ?int $brokerId = null): void
public function sendBatch(array $messages): void
{
$config = $this->config;
$broker = $this->broker;
Expand Down