Skip to content

Commit

Permalink
添加 Close() 方法停止轮询
Browse files Browse the repository at this point in the history
  • Loading branch information
MoonBall committed Oct 10, 2022
1 parent c9dde04 commit e37c756
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
8 changes: 8 additions & 0 deletions client.go
Expand Up @@ -75,13 +75,15 @@ type Client interface {
RemoveChangeListener(listener storage.ChangeListener)
GetChangeListeners() *list.List
UseEventDispatch()
Close()
}

// internalClient apollo 客户端实例
type internalClient struct {
initAppConfigFunc func() (*config.AppConfig, error)
appConfig *config.AppConfig
cache *storage.Cache
configComponent *notify.ConfigComponent
}

func (c *internalClient) getAppConfig() config.AppConfig {
Expand Down Expand Up @@ -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 ! ")

Expand Down Expand Up @@ -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()
}
12 changes: 12 additions & 0 deletions component/notify/componet_notify.go
Expand Up @@ -34,6 +34,7 @@ const (
type ConfigComponent struct {
appConfigFunc func() config.AppConfig
cache *storage.Cache
stopCh chan interface{}
}

// SetAppConfig nolint
Expand All @@ -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:
Expand All @@ -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)
}

0 comments on commit e37c756

Please sign in to comment.