This commit is contained in:
Lars Westermann 2019-06-14 19:52:49 +02:00
parent 619b7340bb
commit e63b51e5a7
Signed by: lars.westermann
GPG key ID: 9D417FA5BB9D5E1D

View file

@ -14,6 +14,8 @@ import io.ktor.routing.Route
import io.ktor.routing.get import io.ktor.routing.get
import kotlinx.html.currentTimeMillis import kotlinx.html.currentTimeMillis
import kotlin.concurrent.thread import kotlin.concurrent.thread
import kotlin.math.abs
import kotlin.math.max
object PushService { object PushService {
@ -35,14 +37,20 @@ object PushService {
} }
private fun getIndexOfTimestamp(timestamp: Long): Int { private fun getIndexOfTimestamp(timestamp: Long): Int {
val index = messages.binarySearch(Pair(timestamp, Message.empty), compareBy { it.first }) var index = messages.binarySearch(Pair(timestamp, Message.empty), compareBy { it.first })
val i = if (index < 0) { index = if (index < 0) {
-index - 1 -index - 1
} else { } else {
index index
} }
return if (i < 0) 0 else i var element = messages.getOrNull(index - 1)
while (element != null && element.first >= timestamp) {
index--
element = messages.getOrNull(index - 1)
}
return max(0, index)
} }
/** /**