Added connection timeout to check when a connection is actually expired

This commit is contained in:
nathan 2020-09-03 22:41:39 +02:00
parent d3c0b4265d
commit 52cecf4f26

View File

@ -281,12 +281,34 @@ open class Connection(connectionParameters: ConnectionParams<*>) {
return messagesInProgress.value 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) * @return `true` if this connection has no subscribers (which means this connection does not have a remote connection)
*/ */
internal fun isExpired(): Boolean { internal fun isExpired(): Boolean {
// cannot use subscription.isConnected !!! images can be in a state of flux. We only care if there are NO images. return if (subscription.isConnected) {
return subscription.hasNoImages() 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
}
}
}
} }
/** /**