Skip to content

Commit

Permalink
fix: refresh peer info
Browse files Browse the repository at this point in the history
  • Loading branch information
yxq authored and 33cn committed Aug 3, 2021
1 parent aca60b8 commit 7e95977
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 16 deletions.
5 changes: 2 additions & 3 deletions system/p2p/dht/protocol/peer/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ func init() {
// Protocol ...
type Protocol struct {
*protocol.P2PEnv
refreshing int32

// "/ip4/{ip}/tcp/{port}"
externalAddr string
Expand Down Expand Up @@ -101,11 +100,11 @@ func InitProtocol(env *protocol.P2PEnv) {
case <-ticker.C:
p.refreshSelf()
case <-ticker2.C:
peers := p.RoutingTable.ListPeers()
peers := p.RoutingTable.NearestPeers(kbt.ConvertPeerID(p.Host.ID()), p.RoutingTable.Size())
if len(peers) <= maxPeers {
break
}
p.refreshPeerInfo(peers[:len(peers)-maxPeers])
p.refreshPeerInfo(peers[maxPeers:])

case <-ticker3.C:
p.checkOutBound(p.PeerInfoManager.Fetch(p.Host.ID()).GetHeader().GetHeight())
Expand Down
13 changes: 0 additions & 13 deletions system/p2p/dht/protocol/peer/peerinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"strconv"
"strings"
"sync"
"sync/atomic"
"time"

"github.com/33cn/chain33/common/version"
Expand Down Expand Up @@ -65,10 +64,6 @@ func (p *Protocol) refreshSelf() {
}

func (p *Protocol) refreshPeerInfo(peers []peer.ID) {
if !atomic.CompareAndSwapInt32(&p.refreshing, 0, 1) {
return
}
defer atomic.StoreInt32(&p.refreshing, 0)
var wg sync.WaitGroup
// 限制最大并发数量为20
ch := make(chan struct{}, 20)
Expand Down Expand Up @@ -169,8 +164,6 @@ func (p *Protocol) detectNodeAddr() {
func (p *Protocol) queryPeerInfoOld(pid peer.ID) (*types.Peer, error) {
ctx, cancel := context.WithTimeout(p.Ctx, time.Second*5)
defer cancel()
p.Host.ConnManager().Protect(pid, peerInfoOld)
defer p.Host.ConnManager().Unprotect(pid, peerInfoOld)
stream, err := p.Host.NewStream(ctx, pid, peerInfoOld)
if err != nil {
log.Error("refreshPeerInfo", "new stream error", err, "peer id", pid)
Expand Down Expand Up @@ -206,8 +199,6 @@ func (p *Protocol) queryPeerInfoOld(pid peer.ID) (*types.Peer, error) {
func (p *Protocol) queryPeerInfo(pid peer.ID) (*types.Peer, error) {
ctx, cancel := context.WithTimeout(p.Ctx, time.Second*5)
defer cancel()
p.Host.ConnManager().Protect(pid, peerInfo)
defer p.Host.ConnManager().Unprotect(pid, peerInfo)
stream, err := p.Host.NewStream(ctx, pid, peerInfo)
if err != nil {
log.Error("refreshPeerInfo", "new stream error", err, "peer id", pid)
Expand All @@ -226,8 +217,6 @@ func (p *Protocol) queryPeerInfo(pid peer.ID) (*types.Peer, error) {
func (p *Protocol) queryVersionOld(pid peer.ID) error {
ctx, cancel := context.WithTimeout(p.Ctx, time.Second*5)
defer cancel()
p.Host.ConnManager().Protect(pid, peerVersionOld)
defer p.Host.ConnManager().Unprotect(pid, peerVersionOld)
stream, err := p.Host.NewStream(ctx, pid, peerVersionOld)
if err != nil {
log.Error("NewStream", "err", err, "remoteID", pid)
Expand Down Expand Up @@ -274,8 +263,6 @@ func (p *Protocol) queryVersionOld(pid peer.ID) error {
func (p *Protocol) queryVersion(pid peer.ID) error {
ctx, cancel := context.WithTimeout(p.Ctx, time.Second*5)
defer cancel()
p.Host.ConnManager().Protect(pid, peerVersion)
defer p.Host.ConnManager().Unprotect(pid, peerVersion)
stream, err := p.Host.NewStream(ctx, pid, peerVersion)
if err != nil {
log.Error("NewStream", "err", err, "remoteID", pid)
Expand Down

0 comments on commit 7e95977

Please sign in to comment.