[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,21 +9,19 @@ 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 { async { fetchPackageInformationAsync(it) } }
.map { fetched -> .map { fetched ->
val (packageName, value) = fetched.await() val (packageName, value) = fetched.await()
val fetchedPackageInformation = Gson().fromJson(value, FetchedPackageInformation::class.java) val fetchedPackageInformation = Gson().fromJson(value, FetchedPackageInformation::class.java)
@@ -33,18 +31,16 @@ class VersionFetcher(
) )
} }
} }
}
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() {
@@ -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,10 +34,12 @@ fun main() {
val template = velocityEngine.getTemplate("$fileName.vm") val template = velocityEngine.getTemplate("$fileName.vm")
val packages = VersionFetcher().use {
runBlocking { runBlocking {
val packages = VersionFetcher(coroutineContext).use {
it.fetch() it.fetch()
} }
}
findLastVersions(packages) findLastVersions(packages)
.also { .also {
context.put("dependencies", it) context.put("dependencies", it)
@@ -48,7 +49,6 @@ fun main() {
template.merge(context, it) template.merge(context, it)
} }
} }
}
fun findLastVersions(packages: List<PackageInformation>): List<Package> { fun findLastVersions(packages: List<PackageInformation>): List<Package> {
return packages return packages