beiwei30 commented on a change in pull request #708:
URL: https://github.com/apache/dubbo-go/pull/708#discussion_r472222779
##########
File path: cluster/router/chain/chain.go
##########
@@ -79,6 +104,100 @@ func (c *RouterChain) AddRouters(routers
[]router.PriorityRouter) {
c.routers = newRouters
}
+// SetInvokers receives updated invokers from registry center. If the times of
notification exceeds countThreshold and
+// time interval exceeds timeThreshold since last cache update, then notify to
update the cache.
+func (c *RouterChain) SetInvokers(invokers []protocol.Invoker) {
+ c.mutex.Lock()
+ c.invokers = invokers
+ c.mutex.Unlock()
+
+ c.count++
+ now := time.Now()
+ if c.count >= countThreshold && now.Sub(c.last) >= timeThreshold {
+ c.last = now
+ c.count = 0
+ go func() {
+ c.notify <- struct{}{}
+ }()
+ }
+}
+
+// loop listens on events to update the address cache when it's necessary,
either when it receives notification
+// from address update, or when timeInterval exceeds.
+func (c *RouterChain) loop() {
+ for {
+ ticker := time.NewTicker(timeInterval)
+ select {
+ case <-ticker.C:
+ c.buildCache()
+ case <-c.notify:
+ c.buildCache()
+ }
+ }
+}
+
+// copyRouters make a snapshot copy from RouterChain's router list.
+func (c *RouterChain) copyRouters() []router.PriorityRouter {
+ c.mutex.RLock()
+ defer c.mutex.RUnlock()
+ ret := make([]router.PriorityRouter, 0, len(c.routers))
+ ret = append(ret, c.routers...)
+ return ret
+}
+
+// copyInvokers copies a snapshot of the received invokers.
+func (c *RouterChain) copyInvokers() []protocol.Invoker {
+ c.mutex.RLock()
+ defer c.mutex.RUnlock()
+ if c.invokers == nil || len(c.invokers) == 0 {
+ return nil
+ }
+ ret := make([]protocol.Invoker, 0, len(c.invokers))
+ ret = append(ret, c.invokers...)
+ return ret
+}
+
+// loadCache loads cache from sync.Value to guarantee the visibility
+func (c *RouterChain) loadCache() *InvokerCache {
+ v := c.cache.Load()
+ if v == nil {
+ return nil
+ }
+
+ return v.(*InvokerCache)
+}
+
+// buildCache builds address cache with the new invokers for all poolable
routers.
+func (c *RouterChain) buildCache() {
+ invokers := c.copyInvokers()
+ if invokers == nil || len(c.invokers) == 0 {
+ return
+ }
+
+ cache := BuildCache(invokers)
+ origin := c.loadCache()
+
+ var mutex sync.Mutex
+ var wg sync.WaitGroup
Review comment:
done.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]