This commit is contained in:
2020-10-09 17:57:28 +08:00
parent bbc77f69bc
commit deaaf9ab86
3 changed files with 47 additions and 49 deletions
+3 -3
View File
@@ -186,9 +186,9 @@ class Broker
if ($host && $port) { if ($host && $port) {
try { try {
$socket = $this->getSocket($host, $port, $modeSync); $socket = $this->getSocket($host, $port, $modeSync);
// if (!$modeSync) { if (!$modeSync) {
// $socket->SetonReadable($this->process); $socket->SetonReadable($this->process);
// } }
$socket->connect(); $socket->connect();
$this->{$type}[$key] = $socket; $this->{$type}[$key] = $socket;
return $socket; return $socket;
-1
View File
@@ -238,7 +238,6 @@ class Process
$broker = \Kafka\Broker::getInstance(); $broker = \Kafka\Broker::getInstance();
foreach ($brokerHost as $host) { foreach ($brokerHost as $host) {
$socket = $broker->getMetaConnect($host); $socket = $broker->getMetaConnect($host);
var_dump($socket);
if ($socket) { if ($socket) {
$params = \Kafka\ConsumerConfig::getInstance()->getTopics(); $params = \Kafka\ConsumerConfig::getInstance()->getTopics();
$this->debug('Start sync metadata request params:' . json_encode($params)); $this->debug('Start sync metadata request params:' . json_encode($params));
+43 -44
View File
@@ -15,16 +15,17 @@
namespace Kafka; namespace Kafka;
/** /**
* +------------------------------------------------------------------------------ +------------------------------------------------------------------------------
* Kafka broker socket * Kafka broker socket
* +------------------------------------------------------------------------------ +------------------------------------------------------------------------------
* *
* @package * @package
* @version $_SWANBR_VERSION_$ * @version $_SWANBR_VERSION_$
* @copyright Copyleft * @copyright Copyleft
* @author $_SWANBR_AUTHOR_$ * @author $_SWANBR_AUTHOR_$
* +------------------------------------------------------------------------------ +------------------------------------------------------------------------------
*/ */
class Socket class Socket
{ {
// {{{ consts // {{{ consts
@@ -77,7 +78,7 @@ class Socket
/** /**
* Stream resource * Stream resource
* *
* @var \Swoole\Coroutine\Socket * @var mixed
* @access private * @access private
*/ */
private $stream = null; private $stream = null;
@@ -227,43 +228,41 @@ class Socket
throw new \Kafka\Exception('Cannot open without port.'); throw new \Kafka\Exception('Cannot open without port.');
} }
$this->stream = new \Swoole\Coroutine\Socket(AF_INET, SOCK_STREAM);; $this->stream = @fsockopen(
if (!$this->stream->connect(
$this->host, $this->host,
$this->port, $this->port,
$errno,
$errstr,
$this->sendTimeoutSec + ($this->sendTimeoutUsec / 1000000) $this->sendTimeoutSec + ($this->sendTimeoutUsec / 1000000)
)) { );
if ($this->stream == false) {
$error = 'Could not connect to ' $error = 'Could not connect to '
. $this->host . ':' . $this->port . $this->host . ':' . $this->port
. ' (' . $this->stream->errMsg . ' [' . $this->stream->errCode . '])'; . ' ('.$errstr.' ['.$errno.'])';
throw new \Kafka\Exception($error); throw new \Kafka\Exception($error);
} }
// stream_set_blocking($this->stream->getpeername(), 0); stream_set_blocking($this->stream, 0);
// stream_set_read_buffer($this->stream, 0); stream_set_read_buffer($this->stream, 0);
// $this->readWatcher = \Amp\onReadable($this->stream, function () { $this->readWatcher = \Amp\onReadable($this->stream, function () {
// do { do {
// try { if(!$this->isSocketDead($this->stream)){
// if (!$this->isSocketDead($this->stream)) { $newData = @fread($this->stream, self::READ_MAX_LEN);
// $newData = $this->stream->recv(self::READ_MAX_LEN); }else{
// } else { $this->reconnect();
// $this->reconnect(); return;
// return; }
// } if ($newData) {
// if ($newData) { $this->read($newData);
// $this->read($newData); }
// } } while ($newData);
// } catch (\Exception $exception) { });
// var_dump($exception->getMessage());
// } $this->writeWatcher = \Amp\onWritable($this->stream, function () {
// } while ($newData); $this->write();
// }); }, array('enable' => false)); // <-- let's initialize the watcher as "disabled"
//
// $this->writeWatcher = \Amp\onWritable($this->stream, function () {
// $this->write();
// }, array('enable' => false)); // <-- let's initialize the watcher as "disabled"
} }
// }}} // }}}
@@ -299,7 +298,6 @@ class Socket
// {{{ public function SetOnReadable() // {{{ public function SetOnReadable()
public $onReadable; public $onReadable;
/** /**
* set on readable callback function * set on readable callback function
* *
@@ -324,7 +322,9 @@ class Socket
{ {
\Amp\cancel($this->readWatcher); \Amp\cancel($this->readWatcher);
\Amp\cancel($this->writeWatcher); \Amp\cancel($this->writeWatcher);
$this->stream->close(); if (is_resource($this->stream)) {
fclose($this->stream);
}
$this->readBuffer = ''; $this->readBuffer = '';
$this->writeBuffer = ''; $this->writeBuffer = '';
$this->readNeedLength = 0; $this->readNeedLength = 0;
@@ -397,13 +397,15 @@ class Socket
$this->writeBuffer .= $data; $this->writeBuffer .= $data;
} }
$bytesToWrite = strlen($this->writeBuffer); $bytesToWrite = strlen($this->writeBuffer);
$bytesWritten = $this->stream->send($this->writeBuffer); $bytesWritten = @fwrite($this->stream, $this->writeBuffer);
// if ($bytesToWrite === $bytesWritten) { if ($bytesToWrite === $bytesWritten) {
// \Amp\disable($this->writeWatcher); \Amp\disable($this->writeWatcher);
// } elseif ($bytesWritten >= 0) { } elseif ($bytesWritten >= 0) {
// \Amp\enable($this->writeWatcher); \Amp\enable($this->writeWatcher);
// } } elseif ($this->isSocketDead($this->stream)) {
$this->reconnect();
}
$this->writeBuffer = substr($this->writeBuffer, $bytesWritten); $this->writeBuffer = substr($this->writeBuffer, $bytesWritten);
} }
@@ -417,10 +419,7 @@ class Socket
*/ */
protected function isSocketDead() protected function isSocketDead()
{ {
if (!($this->stream instanceof \Swoole\Coroutine\Socket)) { return !is_resource($this->stream) || @feof($this->stream);
return true;
}
return $this->stream->checkLiveness();
} }
// }}} // }}}