From e37c7563d391a9e8ffe5dc2783aa0002bb445feb Mon Sep 17 00:00:00 2001 From: Gang Chen Date: Mon, 10 Oct 2022 23:25:59 +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?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client.go | 8 ++++++++ component/notify/componet_notify.go | 12 ++++++++++++ 2 files changed, 20 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..04ca39d 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,12 @@ func (c *ConfigComponent) Start() { c.cache.UpdateApolloConfig(apolloConfig, c.appConfigFunc) } t2.Reset(longPollInterval) + case <-c.stopCh: + break loop } } } + +func (c *ConfigComponent) Stop() { + close(c.stopCh) +}