Skip to content

Commit

Permalink
check inbound region for sameRegion
Browse files Browse the repository at this point in the history
  • Loading branch information
czarcas7ic committed Jun 24, 2024
1 parent 161d5c9 commit d8cd390
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions p2p/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -790,22 +790,38 @@ func (sw *Switch) acceptRoutine() {
// Note if the new peer is in the same region as us
isSameRegion := sw.addrBook.GetAddressRegion(p.SocketAddr()) == sw.MyRegion

if !isSameRegion {
// If this peer is not in our same region and we have no room to dial peers outside of our region, return error
// TODO check this formula
// Calculate the maximum allowed peers for both same region and other regions
maxOutboundPeersInSameRegion := int(sw.config.MaxPercentPeersInSameRegion * float64(sw.config.MaxNumOutboundPeers))
maxInboundPeersInSameRegion := int(sw.config.MaxPercentPeersInSameRegion * float64(sw.config.MaxNumInboundPeers))
maxOutboundPeersInOtherRegion := sw.config.MaxNumOutboundPeers - maxOutboundPeersInSameRegion
maxInboundPeersInOtherRegion := sw.config.MaxNumInboundPeers - maxInboundPeersInSameRegion

if isSameRegion {
out, in, _ := sw.NumPeers()

if p.IsOutbound() {
maxOutboundPeersInOtherRegion := sw.config.MaxNumOutboundPeers - int(sw.config.MaxPercentPeersInSameRegion*float64(sw.config.MaxNumOutboundPeers))
if sw.CurrentNumOutboundPeersInOtherRegion+1 > maxOutboundPeersInOtherRegion {
if (out-sw.CurrentNumOutboundPeersInOtherRegion)+1 > maxOutboundPeersInSameRegion {
sw.Logger.Error("exceeds max percent peers in same region")
continue
}
} else {
maxInboundPeersInOtherRegion := sw.config.MaxNumInboundPeers - int(sw.config.MaxPercentPeersInSameRegion*float64(sw.config.MaxNumInboundPeers))
if sw.CurrentNumInboundPeersInOtherRegion+1 > maxInboundPeersInOtherRegion {
if (in-sw.CurrentNumInboundPeersInOtherRegion)+1 > maxInboundPeersInSameRegion {
sw.Logger.Error("exceeds max percent peers in same region")
continue
}
}
} else {
if p.IsOutbound() {
if sw.CurrentNumOutboundPeersInOtherRegion+1 > maxOutboundPeersInOtherRegion {
sw.Logger.Error("exceeds max percent peers in other regions")
continue
}
} else {
if sw.CurrentNumInboundPeersInOtherRegion+1 > maxInboundPeersInOtherRegion {
sw.Logger.Error("exceeds max percent peers in other regions")
continue
}
}
}
}

Expand Down

0 comments on commit d8cd390

Please sign in to comment.