crypto: Send out some of our requests in parallel

This commit is contained in:
Damir Jelić 2021-04-09 19:10:25 +02:00
parent 99477914df
commit e9e3d129ba

View File

@ -28,9 +28,12 @@ import java.util.concurrent.ConcurrentHashMap
import javax.inject.Inject import javax.inject.Inject
import kotlin.jvm.Throws import kotlin.jvm.Throws
import kotlin.math.max import kotlin.math.max
import kotlinx.coroutines.async
import kotlinx.coroutines.CancellationException import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.cancelChildren import kotlinx.coroutines.cancelChildren
import kotlinx.coroutines.joinAll
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.Mutex
@ -671,14 +674,22 @@ internal class DefaultCryptoService @Inject constructor(
val keyShareLock = roomKeyShareLocks.getOrDefault(roomId, Mutex()) val keyShareLock = roomKeyShareLocks.getOrDefault(roomId, Mutex())
keyShareLock.withLock { keyShareLock.withLock {
for (toDeviceRequest in olmMachine!!.shareGroupSession(roomId, roomMembers)) { coroutineScope {
// TODO these requests should be sent out in parallel olmMachine!!.shareGroupSession(roomId, roomMembers).map {
// This request can only be a to-device request. when (it) {
when (toDeviceRequest) { is Request.ToDevice -> {
is Request.ToDevice -> { async {
sendToDevice(toDeviceRequest) sendToDevice(it)
}
}
else -> {
// This request can only be a to-device request but
// we need to handle all our cases and put this
// async block for our joinAll to work.
async {}
}
} }
} }.joinAll()
} }
} }
} }
@ -751,19 +762,30 @@ internal class DefaultCryptoService @Inject constructor(
private suspend fun sendOutgoingRequests() { private suspend fun sendOutgoingRequests() {
outgointRequestsLock.withLock { outgointRequestsLock.withLock {
// TODO these requests should be sent out in parallel coroutineScope {
for (outgoingRequest in olmMachine!!.outgoingRequests()) { olmMachine!!.outgoingRequests().map {
when (outgoingRequest) { when (it) {
is Request.KeysUpload -> { is Request.KeysUpload -> {
uploadKeys(outgoingRequest) async {
uploadKeys(it)
}
}
is Request.KeysQuery -> {
async {
queryKeys(it)
}
}
is Request.ToDevice -> {
// TODO this sends out mostly key requests, it's a
// bit spammy as of now so it's disabled, needs to
// be fixed on the Rust side.
async {}
}
else -> {
async {}
}
} }
is Request.KeysQuery -> { }.joinAll()
queryKeys(outgoingRequest)
}
is Request.ToDevice -> {
// Timber.v("HELLO TO DEVICE REQUEST ${outgoingRequest.body}")
}
}
} }
} }
} }