From 6f65a18bb7a2d68d58aee60128a21062c6329cdc Mon Sep 17 00:00:00 2001 From: Gang Chen <13298548+MoonBall@users.noreply.github.com> Date: Sat, 10 Dec 2022 12:14:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20Close()=20=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E5=81=9C=E6=AD=A2=E8=BD=AE=E8=AF=A2=20(#254)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client.go | 8 ++++++++ component/notify/componet_notify.go | 15 +++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/client.go b/client.go index 43ebabe..a4ed9bb 100644 --- a/client.go +++ b/client.go @@ -75,6 +75,7 @@ type Client interface { RemoveChangeListener(listener storage.ChangeListener) GetChangeListeners() *list.List UseEventDispatch() + Close() } // internalClient apollo 客户端实例 @@ -82,6 +83,7 @@ type internalClient struct { initAppConfigFunc func() (*config.AppConfig, error) appConfig *config.AppConfig cache *storage.Cache + configComponent *notify.ConfigComponent } func (c *internalClient) getAppConfig() config.AppConfig { @@ -135,6 +137,7 @@ func StartWithConfig(loadAppConfig func() (*config.AppConfig, error)) (Client, e configComponent.SetAppConfig(c.getAppConfig) configComponent.SetCache(c.cache) go component.StartRefreshConfig(configComponent) + c.configComponent = configComponent log.Info("agollo start finished ! ") @@ -260,3 +263,8 @@ func (c *internalClient) GetChangeListeners() *list.List { func (c *internalClient) UseEventDispatch() { c.AddChangeListener(storage.UseEventDispatch()) } + +// Close 停止轮询 +func (c *internalClient) Close() { + c.configComponent.Stop() +} diff --git a/component/notify/componet_notify.go b/component/notify/componet_notify.go index ccbc2aa..7d69e95 100644 --- a/component/notify/componet_notify.go +++ b/component/notify/componet_notify.go @@ -34,6 +34,7 @@ const ( type ConfigComponent struct { appConfigFunc func() config.AppConfig cache *storage.Cache + stopCh chan interface{} } // SetAppConfig nolint @@ -48,9 +49,14 @@ func (c *ConfigComponent) SetCache(cache *storage.Cache) { //Start 启动配置组件定时器 func (c *ConfigComponent) Start() { + if c.stopCh == nil { + c.stopCh = make(chan interface{}) + } + t2 := time.NewTimer(longPollInterval) instance := remote.CreateAsyncApolloConfig() //long poll for sync +loop: for { select { case <-t2.C: @@ -59,6 +65,15 @@ func (c *ConfigComponent) Start() { c.cache.UpdateApolloConfig(apolloConfig, c.appConfigFunc) } t2.Reset(longPollInterval) + case <-c.stopCh: + break loop } } } + +// Stop 停止配置组件定时器 +func (c *ConfigComponent) Stop() { + if c.stopCh != nil { + close(c.stopCh) + } +}