-
Notifications
You must be signed in to change notification settings - Fork 291
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
Conversation
p2pserver/net/netserver/netserver.go
Outdated
@@ -64,6 +64,7 @@ type NetServer struct { | |||
ConnectingNodes | |||
PeerAddrMap | |||
Np *peer.NbrPeers | |||
sync.RWMutex |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok~
p2pserver/net/netserver/netserver.go
Outdated
|
||
//GetPeerSyncCountWithSingleIp return count of cons with single ip | ||
func (this *NetServer) GetPeerSyncCountWithSingleIp(ip string) uint { | ||
this.PeerAddrMap.Lock() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RLock?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed it, thx
p2pserver/net/netserver/netserver.go
Outdated
this.Lock() | ||
defer this.Unlock() | ||
connCount := this.GetOutConnectingListLen() | ||
if connCount > config.DefConfig.P2PNode.MaxConnOutBound { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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~
17f13f4
to
ac1e329
Compare
Add total connection count limit function. Signed-off-by: XiaoJie Guo <guoxiaojie@onchain.com>
ae1fcdb
to
fdd27d2
Compare
p2pserver/net/netserver/netserver.go
Outdated
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() |
There was a problem hiding this comment.
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().
p2pserver/net/netserver/netserver.go
Outdated
this.PeerAddrMap.Lock() | ||
defer this.PeerAddrMap.Unlock() | ||
this.PeerAddrMap.RLock() | ||
defer this.PeerAddrMap.RUnlock() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use Lock()
p2pserver/net/netserver/netserver.go
Outdated
this.PeerAddrMap.Lock() | ||
defer this.PeerAddrMap.Unlock() | ||
this.PeerAddrMap.RLock() | ||
defer this.PeerAddrMap.RUnlock() | ||
this.PeerSyncAddress[addr] = p |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use Lock()
p2pserver/net/netserver/netserver.go
Outdated
this.ConnectingNodes.Lock() | ||
defer this.ConnectingNodes.Unlock() | ||
this.ConnectingNodes.RLock() | ||
defer this.ConnectingNodes.RUnlock() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use Lock()
p2pserver/net/netserver/netserver.go
Outdated
defer this.ConnectingNodes.Unlock() | ||
func (this *NetServer) AddOutConnectingList(addr string) (added bool) { | ||
this.ConnectingNodes.RLock() | ||
defer this.ConnectingNodes.RUnlock() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use Lock()
p2pserver/net/netserver/netserver.go
Outdated
this.PeerAddrMap.Lock() | ||
defer this.PeerAddrMap.Unlock() | ||
this.PeerAddrMap.RLock() | ||
defer this.PeerAddrMap.RUnlock() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use Lock()
p2pserver/net/netserver/netserver.go
Outdated
|
||
//AddInConnRecord add in connection to inConnRecord | ||
func (this *NetServer) AddInConnRecord(addr string) { | ||
this.inConnRecord.RLock() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use lock()
p2pserver/net/netserver/netserver.go
Outdated
//RemoveInConnRecord remove in connection from inConnRecordList | ||
func (this *NetServer) RemoveFromInConnRecord(addr string) { | ||
this.inConnRecord.RLock() | ||
defer this.inConnRecord.RUnlock() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use Lock()
p2pserver/net/netserver/netserver.go
Outdated
|
||
//AddOutConnRecord add out connection to outConnRecord | ||
func (this *NetServer) AddOutConnRecord(addr string, status int) { | ||
this.outConnRecord.RLock() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use Lock()
p2pserver/net/netserver/netserver.go
Outdated
//RemoveOutConnRecord remove out connection from outConnRecord | ||
func (this *NetServer) RemoveFromOutConnRecord(addr string) { | ||
this.outConnRecord.RLock() | ||
defer this.outConnRecord.RUnlock() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use Lock()
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>
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:]...) |
There was a problem hiding this comment.
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>
p2pserver/net/netserver/netserver.go
Outdated
this.outConnRecord.Lock() | ||
defer this.outConnRecord.Unlock() | ||
if _, ok := this.outConnRecord.OutConnectingAddrs[addr]; !ok { | ||
this.outConnRecord.OutConnectingAddrs[addr] = status |
There was a problem hiding this comment.
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.
p2pserver/net/netserver/netserver.go
Outdated
@@ -269,6 +290,7 @@ func (this *NetServer) Connect(addr string, isConsensus bool) error { | |||
} | |||
this.RemoveFromConnectingList(addr) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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?
p2pserver/net/netserver/netserver.go
Outdated
@@ -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 |
There was a problem hiding this comment.
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.
p2pserver/net/netserver/netserver.go
Outdated
@@ -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 |
There was a problem hiding this comment.
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.
p2pserver/net/netserver/netserver.go
Outdated
@@ -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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need return
p2pserver/net/netserver/netserver.go
Outdated
if colonPos == -1 { | ||
colonPos = len(remoteAddr) | ||
} | ||
remoteIp := remoteAddr[:colonPos] |
There was a problem hiding this comment.
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?
p2pserver/net/netserver/netserver.go
Outdated
if err != nil { | ||
log.Errorf("error parse remote ip:%s", addr) | ||
log.Error("parse ip error ", err.Error()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
close connection
p2pserver/net/netserver/netserver.go
Outdated
continue | ||
} | ||
if !this.IsIPInInConnRecord(remoteIp) { | ||
conn.Close() | ||
return |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need continue
p2pserver/net/netserver/netserver.go
Outdated
@@ -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() |
There was a problem hiding this comment.
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>
1.Add total connection count limit function.
2.Add connection count limit function with single ip.