Skip to content
This repository has been archived by the owner on Apr 13, 2022. It is now read-only.

Commit

Permalink
conectivity, flexible disconnecting time
Browse files Browse the repository at this point in the history
  • Loading branch information
kushti committed Oct 15, 2020
1 parent b9ab7ae commit f920be3
Showing 1 changed file with 28 additions and 12 deletions.
40 changes: 28 additions & 12 deletions src/main/scala/scorex/core/network/NetworkController.scala
Expand Up @@ -15,6 +15,7 @@ import scorex.core.network.message.{Message, MessageSpec}
import scorex.core.network.peer.PeerManager.ReceivableMessages._
import scorex.core.network.peer.{LocalAddressPeerFeature, PeerInfo, PeerManager, PenaltyType}
import scorex.core.settings.NetworkSettings
import scorex.core.utils.TimeProvider.Time
import scorex.core.utils.{NetworkUtils, TimeProvider}
import scorex.util.ScorexLogging

Expand Down Expand Up @@ -61,7 +62,11 @@ class NetworkController(settings: NetworkSettings,
private var connections = Map.empty[InetSocketAddress, ConnectedPeer]
private var unconfirmedConnections = Set.empty[InetSocketAddress]

private var lastIncomingMessage : TimeProvider.Time = 0
/**
* Storing timestamp of a last message got via p2p network.
* Used to check whether connectivity is lost.
*/
private var lastIncomingMessageTime : TimeProvider.Time = 0L

//check own declared address for validity
validateDeclaredAddress()
Expand Down Expand Up @@ -91,6 +96,14 @@ class NetworkController(settings: NetworkSettings,
context stop self
}

def networkTime(): Time = scorexContext.timeProvider.time()

// Checks that connectivity is not lost
private def connectivity: Boolean = {
connections.nonEmpty &&
networkTime() < (lastIncomingMessageTime + settings.syncStatusRefreshStable.toMillis)
}

private def businessLogic: Receive = {
//a message coming in from another peer
case msg@Message(spec, _, Some(remote)) =>
Expand All @@ -103,8 +116,8 @@ class NetworkController(settings: NetworkSettings,
connections.get(remote.connectionId.remoteAddress) match {
case Some(cp) => cp.peerInfo match {
case Some(pi) =>
val now = scorexContext.timeProvider.time()
lastIncomingMessage = now
val now = networkTime()
lastIncomingMessageTime = now
connections += remoteAddress -> cp.copy(peerInfo = Some(pi.copy(lastSeen = now)))
case None => log.warn("Peer info not found for a message got from: " + remoteAddress)
}
Expand Down Expand Up @@ -168,6 +181,7 @@ class NetworkController(settings: NetworkSettings,
}

// If enough live connections, remove not responding peer from database
// In not enough live connections, maybe connectivity lost but the node has not updated its status, no ban then
if(connections.size > settings.maxConnections / 2) {
peerManagerRef ! RemovePeer(c.remoteAddress)
}
Expand All @@ -187,7 +201,7 @@ class NetworkController(settings: NetworkSettings,
//calls from API / application
private def interfaceCalls: Receive = {
case GetPeersStatus =>
sender() ! PeersStatusResponse(lastIncomingMessage, scorexContext.timeProvider.time())
sender() ! PeersStatusResponse(lastIncomingMessageTime, networkTime())

case GetConnectedPeers =>
sender() ! connections.values.flatMap(_.peerInfo).toSeq
Expand Down Expand Up @@ -226,15 +240,14 @@ class NetworkController(settings: NetworkSettings,

private def dropDeadConnections(): Unit = {
context.system.scheduler.schedule(60.seconds, 60.seconds) {
connections.values.filter { cp =>
val now = scorexContext.timeProvider.time()
val lastSeen = cp.peerInfo.map(_.lastSeen).getOrElse(now)
(now - lastSeen) > 1000 * 60 * 2 // 2 minutes
}.foreach { cp =>
val now = scorexContext.timeProvider.time()
// Drop connections with peers if they seem to be inactive
val now = networkTime()
connections.values.foreach { cp =>
val lastSeen = cp.peerInfo.map(_.lastSeen).getOrElse(now)
log.info(s"Dropping connection with ${cp.peerInfo}, last seen ${(now - lastSeen) / 1000} seconds ago")
cp.handlerRef ! CloseConnection
if ((now - lastSeen) > settings.syncStatusRefreshStable.toMillis) {
log.info(s"Dropping connection with ${cp.peerInfo}, last seen ${(now - lastSeen) / 1000} seconds ago")
cp.handlerRef ! CloseConnection
}
}
}
}
Expand Down Expand Up @@ -481,6 +494,9 @@ object NetworkController {

case object GetConnectedPeers

/**
* Get p2p network status
*/
case object GetPeersStatus
}

Expand Down

0 comments on commit f920be3

Please sign in to comment.