Skip to content

Commit

Permalink
Refactor API call handling and error flow control
Browse files Browse the repository at this point in the history
This commit removes the use of runCatching for API calls in PackageSearchApiPackageCache and instead handles potential failures further upstream. Additionally, introduced in the PackageSearchProjectService is a restart mechanism with a retry limit that prevents continuous restarts and avoids potential infinite loops. The PackageSearchGradleModelBuilder class also had an unused import that was removed.
  • Loading branch information
lamba92 committed Dec 8, 2023
1 parent bff0144 commit 1732153
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.gradle.util.GradleVersion;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.gradle.tooling.AbstractModelBuilderService;
import org.jetbrains.plugins.gradle.tooling.ErrorMessageBuilder;
import org.jetbrains.plugins.gradle.tooling.Message;
import org.jetbrains.plugins.gradle.tooling.ModelBuilderContext;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.jetbrains.packagesearch.plugin.core.utils.withInitialValue
import com.jetbrains.packagesearch.plugin.utils.PackageSearchApplicationCachesService
import com.jetbrains.packagesearch.plugin.utils.WindowedModuleBuilderContext
import com.jetbrains.packagesearch.plugin.utils.filterNotNullKeys
import com.jetbrains.packagesearch.plugin.utils.logWarn
import com.jetbrains.packagesearch.plugin.utils.nativeModulesFlow
import com.jetbrains.packagesearch.plugin.utils.startWithNull
import com.jetbrains.packagesearch.plugin.utils.timer
Expand All @@ -33,6 +34,7 @@ import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.consumeAsFlow
import kotlinx.coroutines.flow.debounce
Expand All @@ -45,6 +47,8 @@ import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.shareIn
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import org.jetbrains.packagesearch.api.v3.ApiRepository

@Service(Level.PROJECT)
Expand Down Expand Up @@ -97,7 +101,7 @@ class PackageSearchProjectService(
) { nativeModules, transformerExtensions, context ->
transformerExtensions.flatMap { transformer ->
nativeModules.map { module ->
with (context) {
with(context) {
transformer.provideModule(module).startWithNull()
}
}
Expand All @@ -109,9 +113,25 @@ class PackageSearchProjectService(
private val restartFlow = restartChannel.consumeAsFlow()
.shareIn(coroutineScope, SharingStarted.Eagerly, replay = 1)

private var counter = 0
private val counterMutex = Mutex()

private suspend fun restartWithCounter() = counterMutex.withLock {
if (counter++ < 3) {
restart()
}
}

private suspend fun resetCounter() = counterMutex.withLock { counter = 0 }

val modulesStateFlow = restartFlow
.withInitialValue(Unit)
.flatMapLatest { moduleProvidersList }
.catch {
logWarn("${this::class.simpleName}#modulesStateFlow", throwable = it)
restartWithCounter()
}
.onEach { resetCounter() }
.stateIn(coroutineScope, SharingStarted.Eagerly, emptyList())

val modulesByBuildFile = modulesStateFlow
Expand All @@ -125,8 +145,7 @@ class PackageSearchProjectService(
init {

IntelliJApplication.PackageSearchApplicationCachesService
.apiPackageCache
.isOnlineFlow()
.isOnlineFlow
.filter { it }
.onEach { restart() }
.launchIn(coroutineScope)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ class PackageSearchApiPackageCache(
.associateBy { it.id }
val missingIds = ids - localDatabaseResults.keys
if (missingIds.isNotEmpty()) {
val networkResults = runCatching { apiCall(missingIds) }
.getOrDefault(emptyMap())
val networkResults = apiCall(missingIds)
// TODO cache also miss in network to avoid pointless empty query
if (networkResults.isNotEmpty()) {
val packageEntries = networkResults.values.map { it.asCacheEntry() }
Expand Down

0 comments on commit 1732153

Please sign in to comment.