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

refactor router code

parent 6436e242
No related branches found
No related tags found
No related merge requests found
......@@ -19,6 +19,8 @@ package directory
import (
"sync"
"github.com/apache/dubbo-go/cluster/router/router_chain"
)
import (
"go.uber.org/atomic"
......@@ -34,11 +36,12 @@ import (
var RouterUrlSet = gxset.NewSet()
type BaseDirectory struct {
url *common.URL
destroyed *atomic.Bool
routers []cluster.Router
mutex sync.Mutex
once sync.Once
url *common.URL
destroyed *atomic.Bool
routers []cluster.Router
mutex sync.Mutex
once sync.Once
routerChain *router_chain.RouterChain
}
func NewBaseDirectory(url *common.URL) BaseDirectory {
......
package cluster
package router_chain
import (
"sort"
)
import (
"github.com/apache/dubbo-go/cluster"
"github.com/apache/dubbo-go/common"
"github.com/apache/dubbo-go/common/extension"
"github.com/apache/dubbo-go/protocol"
)
type RouterChain struct {
routers []Router
builtinRouters []Router
routers []cluster.Router
builtinRouters []cluster.Router
Invokers []protocol.Invoker
}
func NewRouterChain(url common.URL) *RouterChain {
//var builtinRouters []Router
//factories := extension.GetRouterFactories()
//for _, factory := range factories {
// router, _ := factory.Router(&url)
// builtinRouters = append(builtinRouters, router)
//}
//var routers []Router
//copy(routers, builtinRouters)
//sort.Slice(routers, func(i, j int) bool {
// return routers[i].Priority() < routers[j].Priority()
//})
//return &RouterChain{
// builtinRouters: routers,
// routers: routers,
//}
return nil
func NewRouterChain(url *common.URL) *RouterChain {
var builtinRouters []cluster.Router
factories := extension.GetRouterFactories()
for _, factory := range factories {
router, _ := factory.Router(url)
builtinRouters = append(builtinRouters, router)
}
var routers []cluster.Router
copy(routers, builtinRouters)
sort.SliceStable(routers, func(i, j int) bool {
return routers[i].Priority() < routers[j].Priority()
})
return &RouterChain{
builtinRouters: builtinRouters,
routers: routers,
}
}
func (r RouterChain) AddRouters(routers []Router) {
func (r RouterChain) AddRouters(routers []cluster.Router) {
r.routers = append(r.routers, routers...)
sort.SliceStable(r.routers, func(i, j int) bool {
return routers[i].Priority() < routers[j].Priority()
......
......@@ -36,7 +36,7 @@ func GetRouterFactory(name string) cluster.RouterFactory {
return routers[name]()
}
func GetRouterFactorys() []cluster.RouterFactory {
func GetRouterFactories() []cluster.RouterFactory {
var factorys []cluster.RouterFactory
for _, value := range routers {
......
......@@ -20,6 +20,9 @@ package protocol
import (
"strings"
"sync"
"github.com/apache/dubbo-go/cluster"
"github.com/apache/dubbo-go/cluster/router/router_chain"
)
import (
......@@ -121,6 +124,10 @@ func (proto *registryProtocol) Refer(url common.URL) protocol.Invoker {
}
go directory.Subscribe(serviceUrl)
chain := router_chain.NewRouterChain(serviceUrl)
if chain != nil {
//directory.SetRouters(chain.)
}
//new cluster invoker
cluster := extension.GetCluster(serviceUrl.GetParam(constant.CLUSTER_KEY, constant.DEFAULT_CLUSTER))
......
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