Skip to content

Commit

Permalink
add SubscribeToNamesapce
Browse files Browse the repository at this point in the history
  • Loading branch information
philchia committed Feb 27, 2019
1 parent f68e2c0 commit 9bd9293
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
5 changes: 5 additions & 0 deletions agollo.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ func WatchUpdate() <-chan *ChangeEvent {
return defaultClient.WatchUpdate()
}

// SubscribeToNamesapce fetch namespace config to local and subscribe to updates
func SubscribeToNamesapce(namespace string) error {
return defaultClient.SubscribeToNamesapce(namespace)
}

// GetStringValueWithNameSpace get value from given namespace
func GetStringValueWithNameSpace(namespace, key, defaultValue string) string {
return defaultClient.GetStringValueWithNameSpace(namespace, key, defaultValue)
Expand Down
5 changes: 5 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ func (c *Client) mustGetCache(namespace string) *cache {
return c.caches.mustGetCache(namespace)
}

// SubscribeToNamesapce fetch namespace config to local and subscribe to updates
func (c *Client) SubscribeToNamesapce(namespace string) error {
return c.longPoller.addNamespaces(namespace)
}

// GetStringValueWithNameSpace get value from given namespace
func (c *Client) GetStringValueWithNameSpace(namespace, key, defaultValue string) string {
cache := c.mustGetCache(namespace)
Expand Down
15 changes: 12 additions & 3 deletions poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ type poller interface {
preload() error
// stop poll updates
stop()
// addNamespaces add new namespace and pump config data
addNamespaces(namespaces ...string) error
}

// notificationHandler handle namespace update notification
Expand Down Expand Up @@ -46,9 +48,8 @@ func newLongPoller(conf *Conf, interval time.Duration, handler notificationHandl
notifications: new(notificatonRepo),
handler: handler,
}
for _, namespace := range conf.NameSpaceNames {
poller.notifications.setNotificationID(namespace, defaultNotificationID)
}

poller.addNamespaces(conf.NameSpaceNames...)

return poller
}
Expand All @@ -61,6 +62,14 @@ func (p *longPoller) preload() error {
return p.pumpUpdates()
}

// addNamespaces subscribe to new namespaces and pull all config data to local
func (p *longPoller) addNamespaces(namespaces ...string) error {
for _, namespace := range namespaces {
p.notifications.setNotificationID(namespace, defaultNotificationID)
}
return p.pumpUpdates()
}

func (p *longPoller) watchUpdates() {

p.ctx, p.cancel = context.WithCancel(context.Background())
Expand Down

0 comments on commit 9bd9293

Please sign in to comment.