Skip to content

Commit

Permalink
[PATCH 2/2] the connection should less than max connet peers
Browse files Browse the repository at this point in the history
If the connection more than max connect peers,
disconnect one node.

Signed-off-by: Jin Qing <[email protected]>
  • Loading branch information
superJinQing committed Sep 19, 2017
1 parent 88c6fd9 commit eb1b9ef
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
10 changes: 10 additions & 0 deletions net/node/infoUpdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"DNA/common/config"
"DNA/common/log"
"DNA/core/ledger"
"DNA/events"
. "DNA/net/message"
. "DNA/net/protocol"
"math/rand"
Expand Down Expand Up @@ -243,6 +244,14 @@ func (node *node) updateNodeInfo() {
//close(quit)
}

func (node *node) CheckConnCnt() {
//compare if connect count is larger than DefaultMaxPeers, disconnect one of the connection
if node.nbrNodes.GetConnectionCnt() > node.GetDefaultMaxPeers() {
disconnNode := node.RandGetANbr()
node.eventQueue.GetEvent("disconnect").Notify(events.EventNodeDisconnect, disconnNode)
}
}

func (node *node) updateConnection() {
t := time.NewTimer(time.Second * CONNMONITOR)
for {
Expand All @@ -251,6 +260,7 @@ func (node *node) updateConnection() {
node.ConnectSeeds()
//node.TryConnect()
node.ConnectNode()
node.CheckConnCnt()
t.Stop()
t.Reset(time.Second * CONNMONITOR)
}
Expand Down
9 changes: 9 additions & 0 deletions net/node/nodeMap.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,12 @@ func (node *node) GetNbrNodeCnt() uint32 {
}
return count
}

func (node *node) RandGetANbr() Noder {
for _, n := range node.nbrNodes.List {
if n.GetState() == ESTABLISH {
return n
}
}
return nil
}

0 comments on commit eb1b9ef

Please sign in to comment.