[Gradle, JS] Parallelize requests

This commit is contained in:
Ilya Goncharov
2020-09-11 22:46:39 +03:00
parent a86dd8b5ba
commit 9f9cb4f57e
2 changed files with 31 additions and 35 deletions
@@ -9,43 +9,39 @@ import com.google.gson.Gson
import io.ktor.client.*
import io.ktor.client.request.*
import kotlinx.coroutines.async
import kotlinx.coroutines.withContext
import kotlinx.coroutines.coroutineScope
import java.io.UnsupportedEncodingException
import java.net.URLEncoder
import java.nio.charset.StandardCharsets
import kotlin.coroutines.CoroutineContext
class VersionFetcher(
private val coroutineContext: CoroutineContext
) : AutoCloseable {
class VersionFetcher : AutoCloseable {
private val client = HttpClient()
suspend fun fetch(): List<PackageInformation> {
return npmPackages
.map { fetchPackageInformationAsync(it) }
.map { fetched ->
val (packageName, value) = fetched.await()
val fetchedPackageInformation = Gson().fromJson(value, FetchedPackageInformation::class.java)
PackageInformation(
packageName,
fetchedPackageInformation.versions.keys
)
}
return coroutineScope {
npmPackages
.map { async { fetchPackageInformationAsync(it) } }
.map { fetched ->
val (packageName, value) = fetched.await()
val fetchedPackageInformation = Gson().fromJson(value, FetchedPackageInformation::class.java)
PackageInformation(
packageName,
fetchedPackageInformation.versions.keys
)
}
}
}
private suspend fun fetchPackageInformationAsync(packageName: String) =
withContext(coroutineContext) {
val packagePath =
if (packageName.startsWith("@"))
"@" + encodeURIComponent(packageName)
else
encodeURIComponent(packageName)
private suspend fun fetchPackageInformationAsync(packageName: String): Pair<String, String> {
val packagePath =
if (packageName.startsWith("@"))
"@" + encodeURIComponent(packageName)
else
encodeURIComponent(packageName)
async {
packageName to client.get<String>("http://registry.npmjs.org/$packagePath")
}
}
return (packageName to client.get<String>("http://registry.npmjs.org/$packagePath"))
}
override fun close() {
client.close()
@@ -7,7 +7,6 @@ package org.jetbrains.kotlin.generators.gradle.targets.js
import kotlinx.coroutines.runBlocking
import org.apache.velocity.VelocityContext
import org.apache.velocity.app.Velocity
import org.apache.velocity.app.VelocityEngine
import org.apache.velocity.runtime.RuntimeConstants.RESOURCE_LOADER
import java.io.File
@@ -35,18 +34,19 @@ fun main() {
val template = velocityEngine.getTemplate("$fileName.vm")
runBlocking {
val packages = VersionFetcher(coroutineContext).use {
val packages = VersionFetcher().use {
runBlocking {
it.fetch()
}
findLastVersions(packages)
.also {
context.put("dependencies", it)
}
}
targetFile.writer().use {
template.merge(context, it)
findLastVersions(packages)
.also {
context.put("dependencies", it)
}
targetFile.writer().use {
template.merge(context, it)
}
}