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 { ...@@ -41,7 +41,6 @@ type BaseDirectory struct {
url *common.URL url *common.URL
destroyed *atomic.Bool destroyed *atomic.Bool
mutex sync.Mutex mutex sync.Mutex
once sync.Once
routerChain router.Chain routerChain router.Chain
} }
...@@ -87,8 +86,6 @@ func (dir *BaseDirectory) SetRouters(routers []router.Router) { ...@@ -87,8 +86,6 @@ func (dir *BaseDirectory) SetRouters(routers []router.Router) {
} }
} }
dir.mutex.Lock()
defer dir.mutex.Unlock()
dir.routerChain.AddRouters(routers) dir.routerChain.AddRouters(routers)
} }
......
...@@ -54,7 +54,9 @@ func (dir *staticDirectory) IsAvailable() bool { ...@@ -54,7 +54,9 @@ func (dir *staticDirectory) IsAvailable() bool {
} }
func (dir *staticDirectory) List(invocation protocol.Invocation) []protocol.Invoker { 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() routerChain := dir.RouterChain()
if routerChain == nil { if routerChain == nil {
......
...@@ -19,6 +19,7 @@ package chain ...@@ -19,6 +19,7 @@ package chain
import ( import (
"sort" "sort"
"sync"
) )
import ( import (
...@@ -42,11 +43,19 @@ type RouterChain struct { ...@@ -42,11 +43,19 @@ type RouterChain struct {
// Fixed router instances: ConfigConditionRouter, TagRouter, e.g., the rule for each instance may change but the // Fixed router instances: ConfigConditionRouter, TagRouter, e.g., the rule for each instance may change but the
// instance will never delete or recreate. // instance will never delete or recreate.
builtinRouters []router.Router builtinRouters []router.Router
mutex sync.RWMutex
} }
func (c RouterChain) Route(invoker []protocol.Invoker, url *common.URL, invocation protocol.Invocation) []protocol.Invoker { func (c RouterChain) Route(invoker []protocol.Invoker, url *common.URL, invocation protocol.Invocation) []protocol.Invoker {
finalInvokers := 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) finalInvokers = r.Route(invoker, url, invocation)
} }
return finalInvokers return finalInvokers
...@@ -56,6 +65,8 @@ func (c RouterChain) AddRouters(routers []router.Router) { ...@@ -56,6 +65,8 @@ func (c RouterChain) AddRouters(routers []router.Router) {
newRouters = append(newRouters, c.builtinRouters...) newRouters = append(newRouters, c.builtinRouters...)
newRouters = append(newRouters, routers...) newRouters = append(newRouters, routers...)
sortRouter(newRouters) sortRouter(newRouters)
c.mutex.Lock()
defer c.mutex.Unlock()
c.routers = newRouters 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