From 679e002785461a261118b92a7381f21d778d495b Mon Sep 17 00:00:00 2001 From: nathan Date: Fri, 4 Sep 2020 13:19:33 +0200 Subject: [PATCH] Connections can linger a bit before becoming expired (underlying aeron streams can quickly come and go) --- src/dorkbox/network/connection/Connection.kt | 25 +++++--------------- 1 file changed, 6 insertions(+), 19 deletions(-) diff --git a/src/dorkbox/network/connection/Connection.kt b/src/dorkbox/network/connection/Connection.kt index 9ec211e3..eeb6e302 100644 --- a/src/dorkbox/network/connection/Connection.kt +++ b/src/dorkbox/network/connection/Connection.kt @@ -94,6 +94,9 @@ open class Connection(connectionParameters: ConnectionParams<*>) { internal var preCloseAction: suspend () -> Unit = {} internal var postCloseAction: suspend () -> Unit = {} + // only accessed on a single thread! + private var previousConnectionStartTime = Long.MAX_VALUE + private val isClosed = atomic(false) /** @@ -281,33 +284,17 @@ open class Connection(connectionParameters: ConnectionParams<*>) { return messagesInProgress.value } - - var previousConnectionExpireTime = Long.MAX_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 - val now = System.nanoTime() - when { - previousConnectionExpireTime == Long.MAX_VALUE -> { - // this means we haven't set an expire time yet. - val timeOut = TimeUnit.SECONDS.toNanos(endPoint.config.connectionCloseTimeoutInSeconds.toLong()) - previousConnectionExpireTime = now + timeOut - false - } - now < previousConnectionExpireTime -> { - false - } - else -> { - true - } - } + System.nanoTime() - previousConnectionStartTime >= TimeUnit.SECONDS.toNanos(endPoint.config.connectionCloseTimeoutInSeconds.toLong()) } } @@ -344,7 +331,7 @@ open class Connection(connectionParameters: ConnectionParams<*>) { } // on close, we want to make sure this file is DELETED! - val logFile = endPoint.mediaDriverContext!!.aeronDirectory().resolve("publications").resolve("${publication.registrationId()}.logbuffer") + val logFile = endPoint.mediaDriverContext.aeronDirectory().resolve("publications").resolve("${publication.registrationId()}.logbuffer") publication.close()