Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: when the routing policy is not set, the setInvokers() method has a co process leak #2104

Merged
merged 10 commits into from
Nov 26, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions cluster/router/chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,15 @@ type RouterChain struct {
notify chan struct{}
// Address cache
cache atomic.Value

routerStatus atomic.Int32
}

const (
NoRouter = iota
HasRouter
)

func (c *RouterChain) GetNotifyChan() chan struct{} {
return c.notify
}
Expand Down Expand Up @@ -113,6 +120,10 @@ func (c *RouterChain) AddRouters(routers []router.PriorityRouter) {
c.mutex.Lock()
defer c.mutex.Unlock()
c.routers = newRouters
if c.routerStatus.Load() == int32(NoRouter) {
return
}

go func() {
c.notify <- struct{}{}
}()
Expand All @@ -124,6 +135,9 @@ func (c *RouterChain) SetInvokers(invokers []protocol.Invoker) {
c.mutex.Lock()
c.invokers = invokers
c.mutex.Unlock()
if c.routerStatus.Load() == int32(NoRouter) {
return
}

go func() {
c.notify <- struct{}{}
Expand Down Expand Up @@ -296,6 +310,7 @@ func NewRouterChain(url *common.URL) (*RouterChain, error) {
last: time.Now(),
notify: make(chan struct{}),
}
chain.routerStatus.Store(int32(HasRouter))

routers := make([]router.PriorityRouter, 0, len(routerFactories))
for key, routerFactory := range routerFactories {
Expand Down
50 changes: 50 additions & 0 deletions cluster/router/chain/chain_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package chain

import (
caochengxiang marked this conversation as resolved.
Show resolved Hide resolved
"fmt"
_ "github.com/apache/dubbo-go/cluster/router"
_ "github.com/apache/dubbo-go/cluster/router/tag"
"github.com/apache/dubbo-go/common"
"github.com/apache/dubbo-go/common/constant"
"github.com/apache/dubbo-go/protocol"
"github.com/dubbogo/go-zookeeper/zk"
"github.com/stretchr/testify/assert"
"testing"
)

var (
url, _ = common.NewURL(
fmt.Sprintf("dubbo://%s:%d/com.ikurento.user.UserProvider", constant.LOCAL_HOST_VALUE, constant.DEFAULT_PORT))
anyURL, _ = common.NewURL(fmt.Sprintf("condition://%s/com.foo.BarService", constant.ANYHOST_VALUE))
)

const (
test1234IP = "1.2.3.4"
test0000IP = "0.0.0.0"
port20000 = 20000

dubboForamt = "dubbo://%s:%d/com.foo.BarService"
anyUrlFormat = "condition://%s/com.foo.BarService"
applicationKey = "test-condition"
applicationField = "application"
forceField = "force"
forceValue = "true"
)

var zkCluster *zk.TestCluster

func TestNewRouterChain(t *testing.T) {
chain, _ := NewRouterChain(getRouteUrl(applicationKey))
assert.Equal(t, chain.routerStatus.Load(), int32(HasRouter))
var invokers []protocol.Invoker
dubboURL, _ := common.NewURL(fmt.Sprintf(dubboForamt, test1234IP, port20000))
invokers = append(invokers, protocol.NewBaseInvoker(dubboURL))
chain.SetInvokers(invokers)
}

func getRouteUrl(applicationKey string) *common.URL {
url, _ := common.NewURL(fmt.Sprintf(anyUrlFormat, test0000IP))
url.AddParam(applicationField, applicationKey)
url.AddParam(forceField, forceValue)
return url
}