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

ONT-780 [NCC-2018-014] Add connections limit function. #316

Merged
merged 9 commits into from
Jun 20, 2018

Conversation

qingche123
Copy link
Member

1.Add total connection count limit function.
2.Add connection count limit function with single ip.

@@ -64,6 +64,7 @@ type NetServer struct {
ConnectingNodes
PeerAddrMap
Np *peer.NbrPeers
sync.RWMutex
Copy link
Contributor

@laizy laizy Jun 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

document which field this mutex protect ? only find it is used in Connect function.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this block:

connCount := this.GetOutConnectingListLen()
if connCount > config.DefConfig.P2PNode.MaxConnOutBound {
	log.Warnf("Connect: out connections(%d) reach the max limit(%d)", connCount,
		config.DefConfig.P2PNode.MaxConnOutBound)
	return errors.New("connect: out connections reach the max limit")
}

in Connect function needs to be protected, and I'v changed sync.RWMutex to sync.Mutex

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If so, I highly suggest you naming it connectLock instead of embedding struct field.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok~


//GetPeerSyncCountWithSingleIp return count of cons with single ip
func (this *NetServer) GetPeerSyncCountWithSingleIp(ip string) uint {
this.PeerAddrMap.Lock()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RLock?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed it, thx

this.Lock()
defer this.Unlock()
connCount := this.GetOutConnectingListLen()
if connCount > config.DefConfig.P2PNode.MaxConnOutBound {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK, GetOutConnectingListLen returns the number of nodes in handshaking state, not the total number of connected nodes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

total number of connected nodes, here I mean number of handshaking nodes + number of handshaking completed nodes.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've fixed it, thx~

@qingche123 qingche123 force-pushed the master branch 7 times, most recently from 17f13f4 to ac1e329 Compare June 18, 2018 12:53
Add total connection count limit function.

Signed-off-by: XiaoJie Guo <guoxiaojie@onchain.com>
@qingche123 qingche123 force-pushed the master branch 3 times, most recently from ae1fcdb to fdd27d2 Compare June 19, 2018 02:44
this.PeerConsAddress[addr] = p
}

//RemovePeerSyncAddress remove sync addr from peer-addr map
func (this *NetServer) RemovePeerSyncAddress(addr string) {
this.PeerAddrMap.Lock()
defer this.PeerAddrMap.Unlock()
this.PeerAddrMap.RLock()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try to change the content, it should be Lock(), not RLock().

this.PeerAddrMap.Lock()
defer this.PeerAddrMap.Unlock()
this.PeerAddrMap.RLock()
defer this.PeerAddrMap.RUnlock()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use Lock()

this.PeerAddrMap.Lock()
defer this.PeerAddrMap.Unlock()
this.PeerAddrMap.RLock()
defer this.PeerAddrMap.RUnlock()
this.PeerSyncAddress[addr] = p
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use Lock()

this.ConnectingNodes.Lock()
defer this.ConnectingNodes.Unlock()
this.ConnectingNodes.RLock()
defer this.ConnectingNodes.RUnlock()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use Lock()

defer this.ConnectingNodes.Unlock()
func (this *NetServer) AddOutConnectingList(addr string) (added bool) {
this.ConnectingNodes.RLock()
defer this.ConnectingNodes.RUnlock()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Lock()

this.PeerAddrMap.Lock()
defer this.PeerAddrMap.Unlock()
this.PeerAddrMap.RLock()
defer this.PeerAddrMap.RUnlock()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Lock()


//AddInConnRecord add in connection to inConnRecord
func (this *NetServer) AddInConnRecord(addr string) {
this.inConnRecord.RLock()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use lock()

//RemoveInConnRecord remove in connection from inConnRecordList
func (this *NetServer) RemoveFromInConnRecord(addr string) {
this.inConnRecord.RLock()
defer this.inConnRecord.RUnlock()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Lock()


//AddOutConnRecord add out connection to outConnRecord
func (this *NetServer) AddOutConnRecord(addr string, status int) {
this.outConnRecord.RLock()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Lock()

//RemoveOutConnRecord remove out connection from outConnRecord
func (this *NetServer) RemoveFromOutConnRecord(addr string) {
this.outConnRecord.RLock()
defer this.outConnRecord.RUnlock()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Lock()

XiaoJie Guo added 4 commits June 19, 2018 12:44
Add connection count limit function for single ip.

Signed-off-by: XiaoJie Guo <guoxiaojie@onchain.com>
Remove AddrReq sender from the AddrResp.

Signed-off-by: XiaoJie Guo <guoxiaojie@onchain.com>
rebrand the connection limit function.

Signed-off-by: XiaoJie Guo <guoxiaojie@onchain.com>
rebrand single ip connection limit function

Signed-off-by: XiaoJie Guo <guoxiaojie@onchain.com>
XiaoJie Guo added 2 commits June 19, 2018 12:44
Add connection limit function with consensus channel.

Signed-off-by: XiaoJie Guo <guoxiaojie@onchain.com>
Fix RemoveFromInConnRecord bug

Signed-off-by: XiaoJie Guo <guoxiaojie@onchain.com>
@@ -49,6 +49,12 @@ func AddrReqHandle(data *msgTypes.MsgPayload, p2p p2p.P2P, pid *evtActor.PID, ar

var addrStr []msgCommon.PeerAddr
addrStr = p2p.GetNeighborAddrs()
for i, peer := range addrStr {
if remotePeer.GetID() == peer.ID {
addrStr = append(addrStr[:i], addrStr[i+1:]...)
Copy link
Contributor

@laizy laizy Jun 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

modify array in iteration will cause bug.

Fix AddrReqHandle bug.

Signed-off-by: XiaoJie Guo <guoxiaojie@onchain.com>
this.outConnRecord.Lock()
defer this.outConnRecord.Unlock()
if _, ok := this.outConnRecord.OutConnectingAddrs[addr]; !ok {
this.outConnRecord.OutConnectingAddrs[addr] = status
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the status stored in OutConnectingAddrs is never be updated properly after set to HAND and used after Connect function. so need updated to track the peer's status or discard to store this like InConnRecord.

@@ -269,6 +290,7 @@ func (this *NetServer) Connect(addr string, isConsensus bool) error {
}
this.RemoveFromConnectingList(addr)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since added == false, this line seems redundant?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if ConnectingList include this addr(AddOutConnectingList return false), but the peer is not exist, then need to do RemoveFromConnectingList

@@ -306,6 +330,7 @@ func (this *NetServer) Connect(addr string, isConsensus bool) error {
version := msgpack.NewVersion(this, false, ledger.DefLedger.GetCurrentBlockHeight())
err := remotePeer.SyncLink.Tx(version)
if err != nil {
this.RemoveFromOutConnRecord(addr)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missed this line in line 348?

@@ -413,7 +437,7 @@ func (this *NetServer) startSyncAccept(listener net.Listener) {
conn, err := listener.Accept()
if err != nil {
log.Error("error accepting ", err.Error())
return
continue
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when Accept returns error, it will return error directly at later call. so need return here to avoid busy loop.

@@ -422,13 +446,38 @@ func (this *NetServer) startSyncAccept(listener net.Listener) {
log.Info("remote sync node connect with ",
conn.RemoteAddr(), conn.LocalAddr())

if this.IsAddrInInConnRecord(conn.RemoteAddr().String()) {
return
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need close connection and continue here.

@@ -442,7 +491,7 @@ func (this *NetServer) startConsAccept(listener net.Listener) {
conn, err := listener.Accept()
if err != nil {
log.Error("error accepting ", err.Error())
return
continue
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need return

if colonPos == -1 {
colonPos = len(remoteAddr)
}
remoteIp := remoteAddr[:colonPos]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not use common.ParseIPAddr like in line 503?

if err != nil {
log.Errorf("error parse remote ip:%s", addr)
log.Error("parse ip error ", err.Error())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

close connection

continue
}
if !this.IsIPInInConnRecord(remoteIp) {
conn.Close()
return
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need continue

@@ -511,8 +574,8 @@ func (this *NetServer) GetPeerFromAddr(addr string) *peer.Peer {
//IsNbrPeerAddr return result whether the address is under connecting
func (this *NetServer) IsNbrPeerAddr(addr string, isConsensus bool) bool {
var addrNew string
this.Np.RLock()
defer this.Np.RUnlock()
this.Np.Lock()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RLock should be ok here.

Fix some issue in netserver.

Signed-off-by: XiaoJie Guo <guoxiaojie@onchain.com>
@laizy laizy merged commit fcee257 into ontio:master Jun 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants