From 9bd9293879b9fe601a3415a3d82d0a77126a0900 Mon Sep 17 00:00:00 2001 From: Phil Date: Wed, 27 Feb 2019 13:57:54 +0800 Subject: [PATCH] add SubscribeToNamesapce --- agollo.go | 5 +++++ client.go | 5 +++++ poller.go | 15 ++++++++++++--- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/agollo.go b/agollo.go index c291193..c642f10 100644 --- a/agollo.go +++ b/agollo.go @@ -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) diff --git a/client.go b/client.go index a964e30..d15ce18 100644 --- a/client.go +++ b/client.go @@ -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) diff --git a/poller.go b/poller.go index ebda3f9..ab2ffbe 100644 --- a/poller.go +++ b/poller.go @@ -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 @@ -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 } @@ -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())