Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PIA-1187: Make ping result list synchronized #13

Merged
merged 4 commits into from
Jan 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import kotlinx.coroutines.*
import java.io.IOException
import java.net.InetSocketAddress
import java.net.Socket
import java.util.Collections
import kotlin.coroutines.CoroutineContext
import kotlin.system.measureTimeMillis

Expand All @@ -43,10 +44,12 @@ internal actual class PingPerformer : CoroutineScope {
callback: (result: Map<String, List<Pair<String, Long>>>) -> Unit
) {
async {
val result = mutableMapOf<String, List<Pair<String, Long>>>()
val syncResult =
Collections.synchronizedMap(mutableMapOf<String, List<Pair<String, Long>>>())
val requests: MutableList<Job> = mutableListOf()
for ((region, endpointsInRegion) in endpoints) {
val regionEndpointsResults = mutableListOf<Pair<String, Long>>()
val syncRegionEndpointsResults =
Collections.synchronizedList(mutableListOf<Pair<String, Long>>())
endpointsInRegion.forEach {
requests.add(async(Dispatchers.IO) {
var error: Error? = null
Expand All @@ -56,13 +59,13 @@ internal actual class PingPerformer : CoroutineScope {
latency = error?.let {
REGIONS_PING_TIMEOUT.toLong()
} ?: latency
regionEndpointsResults.add(Pair(it, latency))
result[region] = regionEndpointsResults
syncRegionEndpointsResults.add(Pair(it, latency))
syncResult[region] = syncRegionEndpointsResults
})
}
}
requests.joinAll()
launch(Dispatchers.Main) { callback(result) }
launch(Dispatchers.Main) { callback(syncResult) }
}
}

Expand Down