I'm using Swoole PHP Kafka version 1.1.4 to publish events in a Laravel(v5.8.0) worker.
Everything runs smoothly for some time, but after some time I'm getting this exception while publishing events
AMQP: Failed to Process data with exception: longlang\phpkafka\Exception\SocketException: Could not write 659 bytes to stream in /home/shard/hiver/web/web/vendor/longlang/phpkafka/src/Socket/StreamSocket.php:123
My Kafka Client
use longlang\phpkafka\Exception\ConnectionException;
use longlang\phpkafka\Exception\KafkaErrorException;
use longlang\phpkafka\Producer\Producer;
use longlang\phpkafka\Producer\ProducerConfig;
class KafkaClientManager
{
public static function getInstance()
{
$brokers = explode(',', config('kafka_broker_url'));
$config = new ProducerConfig();
$config->setBootstrapServer($brokers[0]);
$config->setUpdateBrokers(true);
$config->setAcks(-1);
$producer = null;
try {
$producer = new Producer($config);
} catch (ConnectionException $ce) {
// when current broker cannot be connected to, try connecting to a different one
$config->setBootstrapServer($brokers[1]);
$producer = new Producer($config);
}
return $producer;
}
public static function sendMessage($topicName, $messageBody, $partitionKey = null)
{
$producer = app('kafka');
try {
$producer->send($topicName, $messageBody, $partitionKey);
} catch (KafkaErrorException | FatalThrowableError $e) {
// when current broker cannot be connected to, try connecting to a different one
$producer = self::getInstance();
$producer->send($topicName, $messageBody, $partitionKey);
}
}
}
Note : KafkaClient is working fine when I'm using it web controller
I'm able to publish an event via php artisan tinker
, hence removing the suspicion on Kafka broker.
On analysing the PHP Kafka code, I'm able to find that this exception is raised when socket is not writable, but no reason is provided as to why its is not writable
// wait for stream to become available for writing
$writable = $this->select([$this->socket], $timeout, false);
if (false === $writable) {
$this->close();
throw new SocketException('Could not write ' . $bytesToWrite . ' bytes to stream');
}