beiwei30 commented on a change in pull request #708:
URL: https://github.com/apache/dubbo-go/pull/708#discussion_r471946477



##########
File path: cluster/router/chain/chain.go
##########
@@ -79,6 +105,104 @@ 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 {
+               select {
+               case <-time.Tick(timeInterval):
+                       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 := copySlice(c.routers)
+       return ret.([]router.PriorityRouter)
+}
+
+// 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 := copySlice(c.invokers)
+       return ret.([]protocol.Invoker)
+}
+
+func copySlice(s interface{}) interface{} {

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]

Reply via email to