Skip to content

Commit

Permalink
refactor(clearly-defined): Make functions suspending
Browse files Browse the repository at this point in the history
Make remaining function of `ClearlyDefinedService` suspending to avoid
calling `runBlocking` as it is better design to leave the creation of
the coroutine context to the consumer of the service.

Signed-off-by: Martin Nonnenmacher <martin.nonnenmacher@bosch.com>
  • Loading branch information
mnonnenmacher authored and sschuberth committed Aug 20, 2024
1 parent dbc3fc5 commit ba9f17f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
10 changes: 5 additions & 5 deletions clients/clearly-defined/src/main/kotlin/ClearlyDefinedService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import java.io.IOException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
Expand Down Expand Up @@ -285,12 +285,12 @@ suspend fun <T> ClearlyDefinedService.call(block: suspend ClearlyDefinedService.
throw IOException(errorMessage, e)
}

fun ClearlyDefinedService.getDefinitionsChunked(
suspend fun ClearlyDefinedService.getDefinitionsChunked(
coordinates: Collection<Coordinates>,
chunkSize: Int = ClearlyDefinedService.MAX_REQUEST_CHUNK_SIZE
): Map<Coordinates, ClearlyDefinedService.Defined> =
buildMap {
runBlocking(Dispatchers.IO) {
withContext(Dispatchers.IO) {
coordinates.chunked(chunkSize).map { chunk ->
async { call { getDefinitions(chunk) } }
}.awaitAll()
Expand All @@ -299,12 +299,12 @@ fun ClearlyDefinedService.getDefinitionsChunked(
}
}

fun ClearlyDefinedService.getCurationsChunked(
suspend fun ClearlyDefinedService.getCurationsChunked(
coordinates: Collection<Coordinates>,
chunkSize: Int = ClearlyDefinedService.MAX_REQUEST_CHUNK_SIZE
): Map<Coordinates, Curation> =
buildMap {
runBlocking(Dispatchers.IO) {
withContext(Dispatchers.IO) {
coordinates.chunked(chunkSize).map { chunk ->
async { call { getCurations(chunk).values } }
}.awaitAll()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class UploadCurationsCommand : OrtCommand(
pkg.toClearlyDefinedCoordinates()?.let { curation to it }
}.toMap()

val definitions = service.getDefinitionsChunked(curationsToCoordinates.values)
val definitions = runBlocking { service.getDefinitionsChunked(curationsToCoordinates.values) }

val curationsByHarvestStatus = curations.groupBy { curation ->
definitions[curationsToCoordinates[curation]]?.getHarvestStatus() ?: logger.warn {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import org.ossreviewtoolkit.plugins.packagecurationproviders.api.PackageCuration
import org.ossreviewtoolkit.utils.common.Options
import org.ossreviewtoolkit.utils.common.collectMessages
import org.ossreviewtoolkit.utils.ort.OkHttpClientHelper
import org.ossreviewtoolkit.utils.ort.runBlocking
import org.ossreviewtoolkit.utils.ort.showStackTrace
import org.ossreviewtoolkit.utils.spdx.SpdxExpression.Strictness
import org.ossreviewtoolkit.utils.spdx.toSpdxOrNull
Expand Down Expand Up @@ -108,7 +109,7 @@ class ClearlyDefinedPackageCurationProvider(
}

val curations = runCatching {
service.getCurationsChunked(coordinatesToIds.keys)
runBlocking { service.getCurationsChunked(coordinatesToIds.keys) }
}.onFailure { e ->
when (e) {
is HttpException -> {
Expand All @@ -134,7 +135,7 @@ class ClearlyDefinedPackageCurationProvider(
}

val filteredCurations = if (config.minTotalLicenseScore > 0) {
val definitions = service.getDefinitionsChunked(curations.keys)
val definitions = runBlocking { service.getDefinitionsChunked(curations.keys) }

curations.filterKeys { coordinates ->
val score = definitions[coordinates]?.licensed?.score?.total
Expand Down

0 comments on commit ba9f17f

Please sign in to comment.