Parallel fetch

This commit is contained in:
downthecrop 2025-09-18 23:09:54 -07:00
parent 3fae644550
commit 07e5fb08b3
2 changed files with 49 additions and 13 deletions

View file

@ -8,6 +8,7 @@ import java.io.BufferedReader
import java.io.InputStreamReader import java.io.InputStreamReader
import java.net.HttpURLConnection import java.net.HttpURLConnection
import java.net.URL import java.net.URL
import java.util.concurrent.*
import javax.swing.SwingUtilities import javax.swing.SwingUtilities
object GitLabPluginFetcher { object GitLabPluginFetcher {
@ -16,6 +17,9 @@ object GitLabPluginFetcher {
private const val CHROME_USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36" private const val CHROME_USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
private const val DEBUG = true // Set to false to disable debug logging private const val DEBUG = true // Set to false to disable debug logging
// Thread pool for concurrent plugin property fetching
private val executorService = Executors.newFixedThreadPool(5)
// Debug logging function // Debug logging function
private fun debugLog(message: String) { private fun debugLog(message: String) {
if (DEBUG) { if (DEBUG) {
@ -55,25 +59,41 @@ object GitLabPluginFetcher {
debugLog("Parsed ${treeItems.size} items from JSON") debugLog("Parsed ${treeItems.size} items from JSON")
// Filter for directories (trees) with mode "040000" // Filter for directories (trees) with mode "040000"
var directoryCount = 0 val pluginDirectories = mutableListOf<Pair<String, String>>()
for (jsonObject in treeItems) { for (jsonObject in treeItems) {
if (jsonObject["type"].asString == "tree" && jsonObject["mode"].asString == "040000") { if (jsonObject["type"].asString == "tree" && jsonObject["mode"].asString == "040000") {
directoryCount++
val folderId = jsonObject["id"].asString val folderId = jsonObject["id"].asString
val folderPath = jsonObject["path"].asString val folderPath = jsonObject["path"].asString
debugLog("Processing directory: $folderPath (ID: $folderId)") pluginDirectories.add(folderId to folderPath)
debugLog("Found directory: $folderPath (ID: $folderId)")
}
}
debugLog("Found ${pluginDirectories.size} plugin directories")
// Fetch plugin properties in parallel
val pluginFutures = mutableListOf<Future<GitLabPlugin>>()
for ((folderId, folderPath) in pluginDirectories) {
val future = executorService.submit(Callable<GitLabPlugin> {
try { try {
val pluginProperties = fetchPluginProperties(folderPath) val pluginProperties = fetchPluginProperties(folderPath)
plugins.add(GitLabPlugin(folderId, folderPath, pluginProperties, null))
debugLog("Successfully fetched properties for: $folderPath") debugLog("Successfully fetched properties for: $folderPath")
GitLabPlugin(folderId, folderPath, pluginProperties, null)
} catch (e: Exception) { } catch (e: Exception) {
debugLog("Error fetching plugin.properties for $folderPath: ${e.message}") debugLog("Error fetching plugin.properties for $folderPath: ${e.message}")
plugins.add(GitLabPlugin(folderId, folderPath, null, "Error fetching plugin.properties")) GitLabPlugin(folderId, folderPath, null, "Error fetching plugin.properties")
}
})
pluginFutures.add(future)
}
// Collect results
for (future in pluginFutures) {
try {
plugins.add(future.get())
} catch (e: Exception) {
debugLog("Error getting plugin result: ${e.message}")
} }
} }
}
debugLog("Found $directoryCount directories (trees)")
} else { } else {
debugLog("HTTP error: $responseCode") debugLog("HTTP error: $responseCode")
val errorReader = BufferedReader(InputStreamReader(connection.errorStream)) val errorReader = BufferedReader(InputStreamReader(connection.errorStream))

View file

@ -1,5 +1,8 @@
package KondoKit.components.ReflectiveEditorComponents package KondoKit.components.ReflectiveEditorComponents
import java.util.concurrent.ExecutorService
import java.util.concurrent.TimeUnit
/** /**
* Simple test to verify GitLabPluginFetcher debug logging works * Simple test to verify GitLabPluginFetcher debug logging works
*/ */
@ -11,6 +14,19 @@ fun main() {
plugins.forEach { plugin -> plugins.forEach { plugin ->
println("- ${plugin.path}: ${plugin.pluginProperties?.description ?: plugin.pluginError}") println("- ${plugin.path}: ${plugin.pluginProperties?.description ?: plugin.pluginError}")
} }
// Shutdown the executor service
try {
val field = GitLabPluginFetcher::class.java.getDeclaredField("executorService")
field.isAccessible = true
val executorService = field.get(GitLabPluginFetcher) as ExecutorService
executorService.shutdown()
if (!executorService.awaitTermination(5, TimeUnit.SECONDS)) {
executorService.shutdownNow()
}
} catch (e: Exception) {
e.printStackTrace()
}
} }
// Give the async fetch some time to complete // Give the async fetch some time to complete