From 93a892f4396487375e5db3170dd632278ee134a0 Mon Sep 17 00:00:00 2001 From: nathan Date: Fri, 11 Sep 2020 03:28:26 +0200 Subject: [PATCH] Connections now can only trigger as expired from when they are created, not when they are last valid. Changed from images.isConnected -> images.count --- src/dorkbox/network/Client.kt | 8 +------ src/dorkbox/network/Server.kt | 8 +------ src/dorkbox/network/connection/Connection.kt | 24 +++++++------------- 3 files changed, 10 insertions(+), 30 deletions(-) diff --git a/src/dorkbox/network/Client.kt b/src/dorkbox/network/Client.kt index 72a99a5b..f8135b30 100644 --- a/src/dorkbox/network/Client.kt +++ b/src/dorkbox/network/Client.kt @@ -474,17 +474,11 @@ open class Client(config: Configuration = Configuration // If the connection has either been closed, or has expired, it needs to be cleaned-up/deleted. var shouldCleanupConnection = false - if (newConnection.isExpired()) { - logger.debug {"[${newConnection.id}] connection expired"} - shouldCleanupConnection = true - } - - else if (newConnection.isClosed()) { + if (newConnection.isClosed()) { logger.debug {"[${newConnection.id}] connection closed"} shouldCleanupConnection = true } - if (shouldCleanupConnection) { close() return@launch diff --git a/src/dorkbox/network/Server.kt b/src/dorkbox/network/Server.kt index 990e8874..853902d3 100644 --- a/src/dorkbox/network/Server.kt +++ b/src/dorkbox/network/Server.kt @@ -485,17 +485,11 @@ open class Server(config: ServerConfiguration = ServerC // If the connection has either been closed, or has expired, it needs to be cleaned-up/deleted. var shouldCleanupConnection = false - if (connection.isExpired()) { - logger.trace {"[${connection.id}] connection expired"} - shouldCleanupConnection = true - } - - else if (connection.isClosed()) { + if (connection.isClosed()) { logger.trace {"[${connection.id}] connection closed"} shouldCleanupConnection = true } - if (shouldCleanupConnection) { // remove this connection so there won't be an attempt to poll it again true diff --git a/src/dorkbox/network/connection/Connection.kt b/src/dorkbox/network/connection/Connection.kt index a5968613..fac37fce 100644 --- a/src/dorkbox/network/connection/Connection.kt +++ b/src/dorkbox/network/connection/Connection.kt @@ -98,7 +98,7 @@ open class Connection(connectionParameters: ConnectionParams<*>) { internal var postCloseAction: suspend () -> Unit = {} // only accessed on a single thread! - private var previousConnectionStartTime = Long.MAX_VALUE + private val connectionInitTime = System.nanoTime() private val isClosed = atomic(false) @@ -291,25 +291,17 @@ open class Connection(connectionParameters: ConnectionParams<*>) { return messagesInProgress.value } - /** - * @return `true` if this connection has no subscribers (which means this connection does not have a remote connection) - */ - internal fun isExpired(): Boolean { - return if (subscription.isConnected) { - previousConnectionStartTime = System.nanoTime() - false - } - else { - // images can be in a state of flux. Sometimes they come and go VERY quickly - System.nanoTime() - previousConnectionStartTime >= TimeUnit.SECONDS.toNanos(endPoint.config.connectionCloseTimeoutInSeconds.toLong()) - } - } - /** * @return `true` if this connection has been closed */ fun isClosed(): Boolean { - return isClosed.value + val hasNoImages = subscription.hasNoImages() + if (hasNoImages) { + // 1) connections take a little bit of time from polling -> connecting (because of how we poll connections before 'connecting' them). + return System.nanoTime() - connectionInitTime >= TimeUnit.SECONDS.toNanos(1) + } + + return hasNoImages } /**