From 8f7891b9d747c01b5a6ecc05831221816633e75a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= <715557344@qq.com> Date: Mon, 16 Mar 2020 15:30:03 +0800 Subject: [PATCH] Fixed bug that update config everytime. --- .../src/Listener/BootProcessListener.php | 4 +++- .../src/Listener/BootProcessListener.php | 4 +++- .../src/Listener/BootProcessListener.php | 23 +++++++++++++------ 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/config-aliyun-acm/src/Listener/BootProcessListener.php b/src/config-aliyun-acm/src/Listener/BootProcessListener.php index 84f027e50..40bfe8dbc 100644 --- a/src/config-aliyun-acm/src/Listener/BootProcessListener.php +++ b/src/config-aliyun-acm/src/Listener/BootProcessListener.php @@ -67,12 +67,14 @@ class BootProcessListener implements ListenerInterface Coroutine::create(function () { $interval = $this->config->get('aliyun_acm.interval', 5); retry(INF, function () use ($interval) { + $prevConfig = []; while (true) { sleep($interval); $config = $this->client->pull(); - if ($config !== $this->config) { + if ($config !== $prevConfig) { $this->updateConfig($config); } + $prevConfig = $config; } }, $interval * 1000); }); diff --git a/src/config-etcd/src/Listener/BootProcessListener.php b/src/config-etcd/src/Listener/BootProcessListener.php index e7fd5709e..eb27112ea 100644 --- a/src/config-etcd/src/Listener/BootProcessListener.php +++ b/src/config-etcd/src/Listener/BootProcessListener.php @@ -83,12 +83,14 @@ class BootProcessListener implements ListenerInterface Coroutine::create(function () { $interval = $this->config->get('config_etcd.interval', 5); retry(INF, function () use ($interval) { + $prevConfig = []; while (true) { sleep($interval); $config = $this->client->pull(); - if ($config !== $this->config) { + if ($config !== $prevConfig) { $this->updateConfig($config); } + $prevConfig = $config; } }, $interval * 1000); }); diff --git a/src/config-zookeeper/src/Listener/BootProcessListener.php b/src/config-zookeeper/src/Listener/BootProcessListener.php index 9c1078095..7cb8d4f60 100644 --- a/src/config-zookeeper/src/Listener/BootProcessListener.php +++ b/src/config-zookeeper/src/Listener/BootProcessListener.php @@ -62,18 +62,27 @@ class BootProcessListener implements ListenerInterface Coroutine::create(function () { $interval = $this->config->get('zookeeper.interval', 5); retry(INF, function () use ($interval) { + $prevConfig = []; while (true) { - $config = $this->client->pull(); - if ($config !== $this->config) { - foreach ($config ?? [] as $key => $value) { - $this->config->set($key, $value); - $this->logger->debug(sprintf('Config [%s] is updated', $key)); - } - } sleep($interval); + $config = $this->client->pull(); + if ($config !== $prevConfig) { + $this->updateConfig($config); + } + $prevConfig = $config; } }, $interval * 1000); }); } } + + protected function updateConfig(array $config) + { + foreach ($config as $key => $value) { + if (is_string($key)) { + $this->config->set($key, $value); + $this->logger->debug(sprintf('Config [%s] is updated', $key)); + } + } + } }