Skip to content

Commit

Permalink
If address list less than 1000 send getaddr req
Browse files Browse the repository at this point in the history
If address list less than 1000 send getaddr request

Signed-off-by: Jin Qing <[email protected]>
  • Loading branch information
superJinQing committed Sep 19, 2017
1 parent 7b51744 commit e0516bd
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
4 changes: 3 additions & 1 deletion net/message/verack.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ func (msg verACK) Handle(node Noder) error {
// Fixme, there is a race condition here,
// but it doesn't matter to access the invalid
// node which will trigger a warning
node.ReqNeighborList()
if node.LocalNode().NeedMoreAddresses() {
node.ReqNeighborList()
}
addr := node.GetAddr()
port := node.GetPort()
nodeAddr := addr + ":" + strconv.Itoa(int(port))
Expand Down
4 changes: 3 additions & 1 deletion net/node/infoUpdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ func (node *node) ConnectSeeds() {
node.nbrNodes.Unlock()
if found {
if n.GetState() == ESTABLISH {
n.ReqNeighborList()
if node.LocalNode().NeedMoreAddresses() {
n.ReqNeighborList()
}
}
} else { //not found
go node.Connect(nodeAddr)
Expand Down
13 changes: 13 additions & 0 deletions net/node/knowaddrlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import (
"sync"
)

const (
// needAddressThreshold is the number of addresses under which the
// address manager will claim to need more addresses.
needAddressThreshold = 1000
)

type KnownAddress struct {
srcAddr NodeAddr
}
Expand All @@ -33,6 +39,13 @@ func (ka *KnownAddress) GetID() uint64 {
return ka.srcAddr.ID
}

func (al *KnownAddressList) NeedMoreAddresses() bool {
al.Lock()
defer al.Unlock()

return al.addrCount < needAddressThreshold
}

func (al *KnownAddressList) AddressExisted(uid uint64) bool {
_, ok := al.List[uid]
return ok
Expand Down
1 change: 1 addition & 0 deletions net/protocol/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ type Noder interface {
GetDefaultMaxPeers() uint
GetMaxOutboundCnt() uint
GetGetAddrMax() uint
NeedMoreAddresses() bool
}

func (msg *NodeAddr) Deserialization(p []byte) error {
Expand Down

0 comments on commit e0516bd

Please sign in to comment.