Improve performance

This commit is contained in:
nielsandriesse 2021-05-19 10:07:09 +10:00
parent fda66d3f5d
commit 4d44bed67d
2 changed files with 6 additions and 8 deletions

View File

@ -26,7 +26,7 @@ class ClosedGroupPoller {
// region Settings // region Settings
companion object { companion object {
private val pollInterval: Long = 2 * 1000 private val pollInterval: Long = 6 * 1000
} }
// endregion // endregion
@ -57,7 +57,8 @@ class ClosedGroupPoller {
// region Private API // region Private API
private fun poll(): List<Promise<Unit, Exception>> { private fun poll(): List<Promise<Unit, Exception>> {
if (!isPolling) { return listOf() } if (!isPolling) { return listOf() }
val publicKeys = MessagingModuleConfiguration.shared.storage.getAllActiveClosedGroupPublicKeys() val storage = MessagingModuleConfiguration.shared.storage
val publicKeys = storage.getAllActiveClosedGroupPublicKeys()
return publicKeys.map { publicKey -> return publicKeys.map { publicKey ->
val promise = SnodeAPI.getSwarm(publicKey).bind { swarm -> val promise = SnodeAPI.getSwarm(publicKey).bind { swarm ->
val snode = swarm.getRandomElementOrNull() ?: throw InsufficientSnodesException() // Should be cryptographically secure val snode = swarm.getRandomElementOrNull() ?: throw InsufficientSnodesException() // Should be cryptographically secure
@ -65,10 +66,7 @@ class ClosedGroupPoller {
SnodeAPI.getRawMessages(snode, publicKey).map {SnodeAPI.parseRawMessagesResponse(it, snode, publicKey) } SnodeAPI.getRawMessages(snode, publicKey).map {SnodeAPI.parseRawMessagesResponse(it, snode, publicKey) }
} }
promise.successBackground { messages -> promise.successBackground { messages ->
if (!MessagingModuleConfiguration.shared.storage.isGroupActive(publicKey)) { if (!storage.isGroupActive(publicKey)) { return@successBackground }
// ignore inactive group's messages
return@successBackground
}
messages.forEach { envelope -> messages.forEach { envelope ->
val job = MessageReceiveJob(envelope.toByteArray()) val job = MessageReceiveJob(envelope.toByteArray())
JobQueue.shared.add(job) JobQueue.shared.add(job)
@ -77,7 +75,7 @@ class ClosedGroupPoller {
promise.fail { promise.fail {
Log.d("Loki", "Polling failed for closed group with public key: $publicKey due to error: $it.") Log.d("Loki", "Polling failed for closed group with public key: $publicKey due to error: $it.")
} }
promise.map { Unit } promise.map { }
} }
} }
// endregion // endregion

View File

@ -18,7 +18,7 @@ class Poller {
var userPublicKey = MessagingModuleConfiguration.shared.storage.getUserPublicKey() ?: "" var userPublicKey = MessagingModuleConfiguration.shared.storage.getUserPublicKey() ?: ""
private var hasStarted: Boolean = false private var hasStarted: Boolean = false
private val usedSnodes: MutableSet<Snode> = mutableSetOf() private val usedSnodes: MutableSet<Snode> = mutableSetOf()
public var isCaughtUp = false var isCaughtUp = false
// region Settings // region Settings
companion object { companion object {