From 5fc2869973d66ee0399c65eeacf9127129aa499f Mon Sep 17 00:00:00 2001 From: cloverstd Date: Sat, 9 Jan 2021 10:23:14 +0800 Subject: [PATCH 1/2] Add notificationId for change event --- storage/change_event.go | 6 ++++-- storage/change_event_test.go | 2 +- storage/repository.go | 12 ++++++++---- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/storage/change_event.go b/storage/change_event.go index 6bdecaf0..9e9f8e41 100644 --- a/storage/change_event.go +++ b/storage/change_event.go @@ -37,7 +37,8 @@ type ConfigChangeType int //config change event type baseChangeEvent struct { - Namespace string + Namespace string + NotificationID int64 } //config change event @@ -84,10 +85,11 @@ func createDeletedConfigChange(oldValue interface{}) *ConfigChange { } //base on changeList create Change event -func createConfigChangeEvent(changes map[string]*ConfigChange, nameSpace string) *ChangeEvent { +func createConfigChangeEvent(changes map[string]*ConfigChange, nameSpace string, notificationID int64) *ChangeEvent { c := &ChangeEvent{ Changes: changes, } c.Namespace = nameSpace + c.NotificationID = notificationID return c } diff --git a/storage/change_event_test.go b/storage/change_event_test.go index b79f8de9..cfa24e00 100644 --- a/storage/change_event_test.go +++ b/storage/change_event_test.go @@ -93,7 +93,7 @@ func TestCreateConfigChangeEvent(t *testing.T) { addConfig := createAddConfigChange("new") changes := make(map[string]*ConfigChange) changes["add"] = addConfig - event := createConfigChangeEvent(changes, "ns") + event := createConfigChangeEvent(changes, "ns", 0) Assert(t, event, NotNilVal()) Assert(t, len(event.Changes), Equal(1)) Assert(t, event.Namespace, Equal("ns")) diff --git a/storage/repository.go b/storage/repository.go index 60ada437..6ffb6a9f 100644 --- a/storage/repository.go +++ b/storage/repository.go @@ -20,12 +20,13 @@ package storage import ( "container/list" "fmt" - "github.com/zouyx/agollo/v4/env/config" "reflect" "strconv" "sync" "sync/atomic" + "github.com/zouyx/agollo/v4/env/config" + "github.com/zouyx/agollo/v4/agcache" "github.com/zouyx/agollo/v4/component/log" "github.com/zouyx/agollo/v4/extension" @@ -232,12 +233,14 @@ func (c *Cache) UpdateApolloConfig(apolloConfig *config.ApolloConfig, appConfigF //get change list changeList := c.UpdateApolloConfigCache(apolloConfig.Configurations, configCacheExpireTime, apolloConfig.NamespaceName) + notify := appConfig.GetNotificationsMap().GetNotify(apolloConfig.NamespaceName) + //push all newest changes - c.pushNewestChanges(apolloConfig.NamespaceName, apolloConfig.Configurations) + c.pushNewestChanges(apolloConfig.NamespaceName, apolloConfig.Configurations, notify) if len(changeList) > 0 { //create config change event base on change list - event := createConfigChangeEvent(changeList, apolloConfig.NamespaceName) + event := createConfigChangeEvent(changeList, apolloConfig.NamespaceName, notify) //push change event to channel c.pushChangeEvent(event) @@ -375,11 +378,12 @@ func (c *Cache) pushChangeEvent(event *ChangeEvent) { }) } -func (c *Cache) pushNewestChanges(namespace string, configuration map[string]interface{}) { +func (c *Cache) pushNewestChanges(namespace string, configuration map[string]interface{}, notificationID int64) { e := &FullChangeEvent{ Changes: configuration, } e.Namespace = namespace + e.NotificationID = notificationID c.pushChange(func(listener ChangeListener) { go listener.OnNewestChange(e) }) From 5705b8fca2f271dd86c849ed24be1a218ddcf8dc Mon Sep 17 00:00:00 2001 From: cloverstd Date: Sat, 9 Jan 2021 10:23:14 +0800 Subject: [PATCH 2/2] Add notificationId for change event --- storage/change_event.go | 6 ++++-- storage/change_event_test.go | 2 +- storage/repository.go | 12 ++++++++---- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/storage/change_event.go b/storage/change_event.go index 6bdecaf0..9e9f8e41 100644 --- a/storage/change_event.go +++ b/storage/change_event.go @@ -37,7 +37,8 @@ type ConfigChangeType int //config change event type baseChangeEvent struct { - Namespace string + Namespace string + NotificationID int64 } //config change event @@ -84,10 +85,11 @@ func createDeletedConfigChange(oldValue interface{}) *ConfigChange { } //base on changeList create Change event -func createConfigChangeEvent(changes map[string]*ConfigChange, nameSpace string) *ChangeEvent { +func createConfigChangeEvent(changes map[string]*ConfigChange, nameSpace string, notificationID int64) *ChangeEvent { c := &ChangeEvent{ Changes: changes, } c.Namespace = nameSpace + c.NotificationID = notificationID return c } diff --git a/storage/change_event_test.go b/storage/change_event_test.go index b79f8de9..cfa24e00 100644 --- a/storage/change_event_test.go +++ b/storage/change_event_test.go @@ -93,7 +93,7 @@ func TestCreateConfigChangeEvent(t *testing.T) { addConfig := createAddConfigChange("new") changes := make(map[string]*ConfigChange) changes["add"] = addConfig - event := createConfigChangeEvent(changes, "ns") + event := createConfigChangeEvent(changes, "ns", 0) Assert(t, event, NotNilVal()) Assert(t, len(event.Changes), Equal(1)) Assert(t, event.Namespace, Equal("ns")) diff --git a/storage/repository.go b/storage/repository.go index 60ada437..6ffb6a9f 100644 --- a/storage/repository.go +++ b/storage/repository.go @@ -20,12 +20,13 @@ package storage import ( "container/list" "fmt" - "github.com/zouyx/agollo/v4/env/config" "reflect" "strconv" "sync" "sync/atomic" + "github.com/zouyx/agollo/v4/env/config" + "github.com/zouyx/agollo/v4/agcache" "github.com/zouyx/agollo/v4/component/log" "github.com/zouyx/agollo/v4/extension" @@ -232,12 +233,14 @@ func (c *Cache) UpdateApolloConfig(apolloConfig *config.ApolloConfig, appConfigF //get change list changeList := c.UpdateApolloConfigCache(apolloConfig.Configurations, configCacheExpireTime, apolloConfig.NamespaceName) + notify := appConfig.GetNotificationsMap().GetNotify(apolloConfig.NamespaceName) + //push all newest changes - c.pushNewestChanges(apolloConfig.NamespaceName, apolloConfig.Configurations) + c.pushNewestChanges(apolloConfig.NamespaceName, apolloConfig.Configurations, notify) if len(changeList) > 0 { //create config change event base on change list - event := createConfigChangeEvent(changeList, apolloConfig.NamespaceName) + event := createConfigChangeEvent(changeList, apolloConfig.NamespaceName, notify) //push change event to channel c.pushChangeEvent(event) @@ -375,11 +378,12 @@ func (c *Cache) pushChangeEvent(event *ChangeEvent) { }) } -func (c *Cache) pushNewestChanges(namespace string, configuration map[string]interface{}) { +func (c *Cache) pushNewestChanges(namespace string, configuration map[string]interface{}, notificationID int64) { e := &FullChangeEvent{ Changes: configuration, } e.Namespace = namespace + e.NotificationID = notificationID c.pushChange(func(listener ChangeListener) { go listener.OnNewestChange(e) })