From db46b80e3f89efb9e4a7f8966d3a5f6223ec41a3 Mon Sep 17 00:00:00 2001 From: Kleissner Date: Sun, 9 May 2021 15:37:32 +0200 Subject: [PATCH] Move ping code to separate file (no changes) --- Commands.go | 58 ---------------------------------------------- Ping.go | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 58 deletions(-) create mode 100644 Ping.go diff --git a/Commands.go b/Commands.go index a8cae8a..509461a 100644 --- a/Commands.go +++ b/Commands.go @@ -10,7 +10,6 @@ import ( "bytes" "fmt" "log" - "time" "github.com/PeernetOfficial/core/dht" ) @@ -201,63 +200,6 @@ func (peer *PeerInfo) cmdLocalDiscovery(msg *MessageAnnouncement) { peer.sendAnnouncement(true, ShouldSendFindSelf(), nil, nil, nil, &bootstrapFindSelf{}) } -// pingTime is the time in seconds to send out ping messages -const pingTime = 10 - -// connectionInvalidate is the threshold in seconds to invalidate formerly active connections that no longer receive incoming packets. -const connectionInvalidate = 22 - -// connectionRemove is the threshold in seconds to remove inactive connections in case there is at least one active connection known. -const connectionRemove = 2 * 60 - -// autoPingAll sends out regular ping messages to all connections of all peers. This allows to detect invalid connections and eventually drop them. -func autoPingAll() { - for { - time.Sleep(time.Second) - thresholdInvalidate1 := time.Now().Add(-connectionInvalidate * time.Second) - thresholdInvalidate2 := time.Now().Add(-connectionInvalidate * time.Second * 4) - thresholdPingOut1 := time.Now().Add(-pingTime * time.Second) - thresholdPingOut2 := time.Now().Add(-pingTime * time.Second * 4) - - for _, peer := range PeerlistGet() { - // first handle active connections - for _, connection := range peer.GetConnections(true) { - thresholdPing := thresholdPingOut1 - thresholdInv := thresholdInvalidate1 - - if connection.Status == ConnectionRedundant { - thresholdPing = thresholdPingOut2 - thresholdInv = thresholdInvalidate2 - } - - if connection.LastPacketIn.Before(thresholdInv) { - peer.invalidateActiveConnection(connection) - continue - } - - if connection.LastPacketIn.Before(thresholdPing) && connection.LastPingOut.Before(thresholdPing) { - peer.pingConnection(connection) - continue - } - } - - // handle inactive connections - for _, connection := range peer.GetConnections(false) { - // If the inactive connection is expired, remove it; although only if there is at least one active connection, or two other inactive ones. - if (len(peer.connectionActive) >= 1 || len(peer.connectionInactive) > 2) && connection.Expires.Before(time.Now()) { - peer.removeInactiveConnection(connection) - continue - } - - // if no ping was sent recently, send one now - if connection.LastPingOut.Before(thresholdPingOut1) { - peer.pingConnection(connection) - } - } - } - } -} - // SendChatAll sends a text message to all peers func SendChatAll(text string) { for _, peer := range PeerlistGet() { diff --git a/Ping.go b/Ping.go new file mode 100644 index 0000000..9484f28 --- /dev/null +++ b/Ping.go @@ -0,0 +1,66 @@ +/* +File Name: Ping.go +Copyright: 2021 Peernet s.r.o. +Author: Peter Kleissner +*/ + +package core + +import "time" + +// pingTime is the time in seconds to send out ping messages +const pingTime = 10 + +// connectionInvalidate is the threshold in seconds to invalidate formerly active connections that no longer receive incoming packets. +const connectionInvalidate = 22 + +// connectionRemove is the threshold in seconds to remove inactive connections in case there is at least one active connection known. +const connectionRemove = 2 * 60 + +// autoPingAll sends out regular ping messages to all connections of all peers. This allows to detect invalid connections and eventually drop them. +func autoPingAll() { + for { + time.Sleep(time.Second) + thresholdInvalidate1 := time.Now().Add(-connectionInvalidate * time.Second) + thresholdInvalidate2 := time.Now().Add(-connectionInvalidate * time.Second * 4) + thresholdPingOut1 := time.Now().Add(-pingTime * time.Second) + thresholdPingOut2 := time.Now().Add(-pingTime * time.Second * 4) + + for _, peer := range PeerlistGet() { + // first handle active connections + for _, connection := range peer.GetConnections(true) { + thresholdPing := thresholdPingOut1 + thresholdInv := thresholdInvalidate1 + + if connection.Status == ConnectionRedundant { + thresholdPing = thresholdPingOut2 + thresholdInv = thresholdInvalidate2 + } + + if connection.LastPacketIn.Before(thresholdInv) { + peer.invalidateActiveConnection(connection) + continue + } + + if connection.LastPacketIn.Before(thresholdPing) && connection.LastPingOut.Before(thresholdPing) { + peer.pingConnection(connection) + continue + } + } + + // handle inactive connections + for _, connection := range peer.GetConnections(false) { + // If the inactive connection is expired, remove it; although only if there is at least one active connection, or two other inactive ones. + if (len(peer.connectionActive) >= 1 || len(peer.connectionInactive) > 2) && connection.Expires.Before(time.Now()) { + peer.removeInactiveConnection(connection) + continue + } + + // if no ping was sent recently, send one now + if connection.LastPingOut.Before(thresholdPingOut1) { + peer.pingConnection(connection) + } + } + } + } +}