[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.*
import io.ktor.client.request.* import io.ktor.client.request.*
import kotlinx.coroutines.async import kotlinx.coroutines.async
import kotlinx.coroutines.withContext import kotlinx.coroutines.coroutineScope
import java.io.UnsupportedEncodingException import java.io.UnsupportedEncodingException
import java.net.URLEncoder import java.net.URLEncoder
import java.nio.charset.StandardCharsets import java.nio.charset.StandardCharsets
import kotlin.coroutines.CoroutineContext
class VersionFetcher( class VersionFetcher : AutoCloseable {
private val coroutineContext: CoroutineContext
) : AutoCloseable {
private val client = HttpClient() private val client = HttpClient()
suspend fun fetch(): List<PackageInformation> { suspend fun fetch(): List<PackageInformation> {
return npmPackages return coroutineScope {
.map { fetchPackageInformationAsync(it) } npmPackages
.map { fetched -> .map { async { fetchPackageInformationAsync(it) } }
val (packageName, value) = fetched.await() .map { fetched ->
val fetchedPackageInformation = Gson().fromJson(value, FetchedPackageInformation::class.java) val (packageName, value) = fetched.await()
PackageInformation( val fetchedPackageInformation = Gson().fromJson(value, FetchedPackageInformation::class.java)
packageName, PackageInformation(
fetchedPackageInformation.versions.keys packageName,
) fetchedPackageInformation.versions.keys
} )
}
}
} }
private suspend fun fetchPackageInformationAsync(packageName: String) = private suspend fun fetchPackageInformationAsync(packageName: String): Pair<String, String> {
withContext(coroutineContext) { val packagePath =
val packagePath = if (packageName.startsWith("@"))
if (packageName.startsWith("@")) "@" + encodeURIComponent(packageName)
"@" + encodeURIComponent(packageName) else
else encodeURIComponent(packageName)
encodeURIComponent(packageName)
async { return (packageName to client.get<String>("http://registry.npmjs.org/$packagePath"))
packageName to client.get<String>("http://registry.npmjs.org/$packagePath") }
}
}
override fun close() { override fun close() {
client.close() client.close()
@@ -7,7 +7,6 @@ package org.jetbrains.kotlin.generators.gradle.targets.js
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import org.apache.velocity.VelocityContext import org.apache.velocity.VelocityContext
import org.apache.velocity.app.Velocity
import org.apache.velocity.app.VelocityEngine import org.apache.velocity.app.VelocityEngine
import org.apache.velocity.runtime.RuntimeConstants.RESOURCE_LOADER import org.apache.velocity.runtime.RuntimeConstants.RESOURCE_LOADER
import java.io.File import java.io.File
@@ -35,18 +34,19 @@ fun main() {
val template = velocityEngine.getTemplate("$fileName.vm") val template = velocityEngine.getTemplate("$fileName.vm")
runBlocking { val packages = VersionFetcher().use {
val packages = VersionFetcher(coroutineContext).use { runBlocking {
it.fetch() it.fetch()
} }
findLastVersions(packages) }
.also {
context.put("dependencies", it)
}
targetFile.writer().use { findLastVersions(packages)
template.merge(context, it) .also {
context.put("dependencies", it)
} }
targetFile.writer().use {
template.merge(context, it)
} }
} }