From 4bb06ef42d92a2975974fb7077cac03d7a535963 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Sat, 10 Oct 2020 11:33:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Kafka/Kafka.php | 50 ++++++++++++++++++++++++------------------------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/Kafka/Kafka.php b/Kafka/Kafka.php index de6584c6..77133b25 100644 --- a/Kafka/Kafka.php +++ b/Kafka/Kafka.php @@ -38,7 +38,7 @@ class Kafka extends \Snowflake\Process\Process $config->setTopics($kafka['topics']); $this->channel = new Channel(1000); - $this->initCoroutine(); + go([$this, 'listener']); } @@ -59,35 +59,33 @@ class Kafka extends \Snowflake\Process\Process /** - * + * 监听通道数据传递 */ - public function initCoroutine() + protected function listener() { - go(function () { - $group = new WaitGroup(); - while ([$topic, $part, $message] = $this->channel->pop()) { - try { - $namespace = 'App\\Kafka\\' . ucfirst($topic) . 'Consumer'; - if (!class_exists($namespace)) { - return; - } - $class = Snowflake::createObject($namespace); - if (!($class instanceof ConsumerInterface)) { - continue; - } - $group->add(); - go(function () use ($group, $class, $topic, $part, $message) { - defer(function () use ($group) { - $group->done(); - }); - $class->onHandler(new Struct($topic, $part, $message)); - }); - } catch (\Throwable $exception) { - $this->application->error($exception->getMessage()); + $group = new WaitGroup(); + while ([$topic, $part, $message] = $this->channel->pop()) { + try { + $namespace = 'App\\Kafka\\' . ucfirst($topic) . 'Consumer'; + if (!class_exists($namespace)) { + return; } + $class = Snowflake::createObject($namespace); + if (!($class instanceof ConsumerInterface)) { + continue; + } + $group->add(); + go(function () use ($group, $class, $topic, $part, $message) { + defer(function () use ($group) { + $group->done(); + }); + $class->onHandler(new Struct($topic, $part, $message)); + }); + } catch (\Throwable $exception) { + $this->application->error($exception->getMessage()); } - $group->wait(); - }); + } + $group->wait(); }