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

fix: 修复 component 的 stop 先于 start 的情况下,内存泄漏的问题 #308

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,7 @@ func StartWithConfig(loadAppConfig func() (*config.AppConfig, error)) (Client, e
log.Debug("init notifySyncConfigServices finished")

//start long poll sync config
configComponent := &notify.ConfigComponent{}
configComponent.SetAppConfig(c.getAppConfig)
configComponent.SetCache(c.cache)
configComponent := notify.NewConfigComponent(c.getAppConfig, c.cache)
go component.StartRefreshConfig(configComponent)
c.configComponent = configComponent

Expand Down
21 changes: 15 additions & 6 deletions component/notify/componet_notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,32 @@ package notify
import (
"time"

"github.com/apolloconfig/agollo/v4/component/log"
"github.com/apolloconfig/agollo/v4/component/remote"
"github.com/apolloconfig/agollo/v4/storage"

"github.com/apolloconfig/agollo/v4/env/config"
"github.com/apolloconfig/agollo/v4/storage"
)

const (
longPollInterval = 2 * time.Second //2s
)

//ConfigComponent 配置组件
// ConfigComponent 配置组件
type ConfigComponent struct {
appConfigFunc func() config.AppConfig
cache *storage.Cache
stopCh chan interface{}
}

// NewConfigComponent .
func NewConfigComponent(appConfigFunc func() config.AppConfig, cache *storage.Cache) *ConfigComponent {
return &ConfigComponent{
appConfigFunc: appConfigFunc,
cache: cache,
stopCh: make(chan interface{}),
}
}

// SetAppConfig nolint
func (c *ConfigComponent) SetAppConfig(appConfigFunc func() config.AppConfig) {
c.appConfigFunc = appConfigFunc
Expand All @@ -47,7 +56,7 @@ func (c *ConfigComponent) SetCache(cache *storage.Cache) {
c.cache = cache
}

//Start 启动配置组件定时器
// Start 启动配置组件定时器
func (c *ConfigComponent) Start() {
if c.stopCh == nil {
c.stopCh = make(chan interface{})
Expand All @@ -56,7 +65,6 @@ func (c *ConfigComponent) Start() {
t2 := time.NewTimer(longPollInterval)
instance := remote.CreateAsyncApolloConfig()
//long poll for sync
loop:
for {
select {
case <-t2.C:
Expand All @@ -66,7 +74,8 @@ loop:
}
t2.Reset(longPollInterval)
case <-c.stopCh:
break loop
log.Debugf("agollo component stop finished")
return
}
}
}
Expand Down
30 changes: 29 additions & 1 deletion component/notify/componet_notify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
package notify

import (
"context"
"encoding/json"
"testing"
"time"

"github.com/apolloconfig/agollo/v4/cluster/roundrobin"
_ "github.com/apolloconfig/agollo/v4/cluster/roundrobin"
Expand Down Expand Up @@ -105,7 +107,7 @@ func getTestAppConfig() *config.AppConfig {

func TestConfigComponent_SetAppConfig_UpdatesAppConfigCorrectly(t *testing.T) {
expectedAppConfig := getTestAppConfig()
c := &ConfigComponent{}
c := NewConfigComponent(nil, nil)
// set appConfigFunc
c.SetAppConfig(func() config.AppConfig {
return *expectedAppConfig
Expand All @@ -120,3 +122,29 @@ func TestConfigComponent_SetAppConfig_UpdatesAppConfigCorrectly(t *testing.T) {
Assert(t, c.appConfigFunc().AppID, Equal("test1"))
Assert(t, c.appConfigFunc().NamespaceName, Equal("application,abc"))
}

func TestConfigComponent_Stop(t *testing.T) {
// 测试 stop 快于 start 情况
c := NewConfigComponent(nil, nil)
c.Stop()

ctx, cancel := context.WithTimeout(context.TODO(), time.Second)
defer cancel()

// 启动开始
done := make(chan struct{})
go func() {
c.Start()
done <- struct{}{}
}()

var isDone bool
select {
case <-ctx.Done():
// 失败
case <-done:
isDone = true
}

Assert(t, isDone, Equal(true))
}
Loading