Skip to content
Snippets Groups Projects
Commit 43917bc1 authored by 邹毅贤's avatar 邹毅贤
Browse files

fix review problems

parent 80b498c5
No related branches found
No related tags found
No related merge requests found
......@@ -41,7 +41,6 @@ type BaseDirectory struct {
url *common.URL
destroyed *atomic.Bool
mutex sync.Mutex
once sync.Once
routerChain router.Chain
}
......@@ -87,8 +86,6 @@ func (dir *BaseDirectory) SetRouters(routers []router.Router) {
}
}
dir.mutex.Lock()
defer dir.mutex.Unlock()
dir.routerChain.AddRouters(routers)
}
......
......@@ -54,7 +54,9 @@ func (dir *staticDirectory) IsAvailable() bool {
}
func (dir *staticDirectory) List(invocation protocol.Invocation) []protocol.Invoker {
invokers := dir.invokers
l := len(dir.invokers)
invokers := make([]protocol.Invoker, l, l)
copy(invokers, dir.invokers)
routerChain := dir.RouterChain()
if routerChain == nil {
......
......@@ -19,6 +19,7 @@ package chain
import (
"sort"
"sync"
)
import (
......@@ -42,11 +43,19 @@ type RouterChain struct {
// Fixed router instances: ConfigConditionRouter, TagRouter, e.g., the rule for each instance may change but the
// instance will never delete or recreate.
builtinRouters []router.Router
mutex sync.RWMutex
}
func (c RouterChain) Route(invoker []protocol.Invoker, url *common.URL, invocation protocol.Invocation) []protocol.Invoker {
finalInvokers := invoker
for _, r := range c.routers {
l := len(c.routers)
rs := make([]router.Router, l, l)
c.mutex.RLock()
copy(rs, c.routers)
c.mutex.RUnlock()
for _, r := range rs {
finalInvokers = r.Route(invoker, url, invocation)
}
return finalInvokers
......@@ -56,6 +65,8 @@ func (c RouterChain) AddRouters(routers []router.Router) {
newRouters = append(newRouters, c.builtinRouters...)
newRouters = append(newRouters, routers...)
sortRouter(newRouters)
c.mutex.Lock()
defer c.mutex.Unlock()
c.routers = newRouters
}
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment