do not respond to pings without UUID. Do not solicit responses unless there's need for more connections

pull/62/head
Zlatin Balevsky 2021-06-18 06:58:00 +01:00
parent 8c0cecbfdd
commit 57e60c631b
No known key found for this signature in database
GPG Key ID: A72832072D525E41
2 changed files with 12 additions and 10 deletions

View File

@ -51,7 +51,7 @@ abstract class Connection implements Closeable {
protected final String name protected final String name
long lastPingSentTime, lastPongReceivedTime long lastPingSentTime
private volatile UUID lastPingUUID private volatile UUID lastPingUUID
@ -133,12 +133,14 @@ abstract class Connection implements Closeable {
protected abstract void write(def message); protected abstract void write(def message);
void sendPing() { void sendPing(boolean expectResponse) {
def ping = [:] def ping = [:]
ping.type = "Ping" ping.type = "Ping"
ping.version = 2 ping.version = 2
if (expectResponse) {
lastPingUUID = UUID.randomUUID() lastPingUUID = UUID.randomUUID()
ping.uuid = lastPingUUID.toString() ping.uuid = lastPingUUID.toString()
}
messages.put(ping) messages.put(ping)
lastPingSentTime = System.currentTimeMillis() lastPingSentTime = System.currentTimeMillis()
} }
@ -170,6 +172,10 @@ abstract class Connection implements Closeable {
protected void handlePing(def ping) { protected void handlePing(def ping) {
log.fine("$name received ping version ${ping.version}") log.fine("$name received ping version ${ping.version}")
if (ping.uuid == null) {
log.fine("Not responding as there was no UUID")
return
}
if (ping.version < 2) if (ping.version < 2)
handlePingV1(ping) handlePingV1(ping)
else else
@ -180,15 +186,12 @@ abstract class Connection implements Closeable {
def pong = [:] def pong = [:]
pong.type = "Pong" pong.type = "Pong"
pong.version = 1 pong.version = 1
if (ping.uuid != null)
pong.uuid = ping.uuid pong.uuid = ping.uuid
pong.pongs = hostCache.getGoodHosts(MAX_PONGS_V1).collect { d -> d.toBase64() } pong.pongs = hostCache.getGoodHosts(MAX_PONGS_V1).collect { d -> d.toBase64() }
messages.put(pong) messages.put(pong)
} }
private void handlePingV2(def ping) { private void handlePingV2(def ping) {
if (ping.uuid == null)
throw new Exception("Ping V2 without an UUID")
UUID uuid = UUID.fromString(ping.uuid) UUID uuid = UUID.fromString(ping.uuid)
byte [] pongPayload = MessageUtil.createPongV2(uuid, hostCache.getGoodHosts(MAX_PONGS_V2)) byte [] pongPayload = MessageUtil.createPongV2(uuid, hostCache.getGoodHosts(MAX_PONGS_V2))
messages.put(pongPayload) messages.put(pongPayload)
@ -196,7 +199,6 @@ abstract class Connection implements Closeable {
protected void handlePong(def pong) { protected void handlePong(def pong) {
log.fine("$name received pong version ${pong.version}") log.fine("$name received pong version ${pong.version}")
lastPongReceivedTime = System.currentTimeMillis()
if (pong.pongs == null) if (pong.pongs == null)
throw new Exception("Pong doesn't have pongs") throw new Exception("Pong doesn't have pongs")

View File

@ -65,7 +65,7 @@ abstract class ConnectionManager {
final long now = System.currentTimeMillis() final long now = System.currentTimeMillis()
getConnections().each { getConnections().each {
if (now - it.lastPingSentTime > PING_TIME) if (now - it.lastPingSentTime > PING_TIME)
it.sendPing() it.sendPing(needsConnections())
} }
} }
} }