Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加 Close() 方法停止轮询 #254

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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()
}
15 changes: 15 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,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)
}
}