Commit f3eee223 authored by suyanlong's avatar suyanlong

Fixed route firstRoute function

parent 6cc0606d
Pipeline #8366 failed with stages
...@@ -2,12 +2,12 @@ package peermgr ...@@ -2,12 +2,12 @@ package peermgr
import ( import (
"errors" "errors"
"gitlab.33.cn/link33/sidecar/internal/loggers"
"github.com/libp2p/go-libp2p-core/peer" "github.com/libp2p/go-libp2p-core/peer"
"github.com/meshplus/bitxhub-kit/crypto" "github.com/meshplus/bitxhub-kit/crypto"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"gitlab.33.cn/link33/sidecar/internal/loggers"
"gitlab.33.cn/link33/sidecar/internal/port" "gitlab.33.cn/link33/sidecar/internal/port"
"gitlab.33.cn/link33/sidecar/model/pb" "gitlab.33.cn/link33/sidecar/model/pb"
) )
......
...@@ -132,17 +132,16 @@ func (p *PortMap) AllRouterPort() []Port { ...@@ -132,17 +132,16 @@ func (p *PortMap) AllRouterPort() []Port {
func (p *PortMap) RandRouterPort() Port { func (p *PortMap) RandRouterPort() Port {
p.rw.RLock() p.rw.RLock()
defer p.rw.RUnlock() defer p.rw.RUnlock()
var randPort Port
l := len(p.peerPort) l := len(p.peerPort)
i := rand.Intn(l + 1) i := rand.Intn(l)
j := 0 j := 0
for _, pt := range p.peerPort { for _, pt := range p.peerPort {
j++
if i == j { if i == j {
randPort = pt return pt
} }
j++
} }
return randPort return nil
} }
func (p *PortMap) Remove(pt Port) { func (p *PortMap) Remove(pt Port) {
......
...@@ -231,8 +231,20 @@ func (r *router) isRouter(ibtpx *pb.IBTPX) bool { ...@@ -231,8 +231,20 @@ func (r *router) isRouter(ibtpx *pb.IBTPX) bool {
// TODO // TODO
func (r *router) firstRoute(msg *pb.Message) { func (r *router) firstRoute(msg *pb.Message) {
p := r.portMap.RandRouterPort() p := r.portMap.RandRouterPort()
_ = p.AsyncSend(msg) if p == nil {
return r.logger.WithFields(logrus.Fields{
"error": "port == nil",
"func": "firstRoute",
}).Error("port == nil")
} else {
err := p.AsyncSend(msg)
if err != nil {
r.logger.WithFields(logrus.Fields{
"error": "p.AsyncSend return error",
"func": "firstRoute",
}).Error(err.Error())
}
}
} }
func (r *router) getHub() (port.Port, bool) { func (r *router) getHub() (port.Port, bool) {
......
Markdown is supported
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