diff --git a/jit-binding-server/src/main/kotlin/io/github/typesafegithub/workflows/jitbindingserver/ArtifactRoutes.kt b/jit-binding-server/src/main/kotlin/io/github/typesafegithub/workflows/jitbindingserver/ArtifactRoutes.kt index 553e969ee..25de75eff 100644 --- a/jit-binding-server/src/main/kotlin/io/github/typesafegithub/workflows/jitbindingserver/ArtifactRoutes.kt +++ b/jit-binding-server/src/main/kotlin/io/github/typesafegithub/workflows/jitbindingserver/ArtifactRoutes.kt @@ -25,10 +25,12 @@ import io.micrometer.prometheusmetrics.PrometheusMeterRegistry import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch +import java.util.Optional +import kotlin.jvm.optionals.getOrNull private val logger = logger { } -typealias CachedVersionArtifact = VersionArtifacts? +typealias CachedVersionArtifact = Optional private val prefetchScope = CoroutineScope(Dispatchers.IO) @@ -120,7 +122,7 @@ private suspend fun ApplicationCall.toBindingArtifacts( if (refresh) { bindingsCache.invalidate(actionCoords) } - return bindingsCache.get(actionCoords) + return bindingsCache.get(actionCoords).getOrNull() } private fun PrometheusMeterRegistry.incrementArtifactCounter( diff --git a/jit-binding-server/src/main/kotlin/io/github/typesafegithub/workflows/jitbindingserver/Main.kt b/jit-binding-server/src/main/kotlin/io/github/typesafegithub/workflows/jitbindingserver/Main.kt index bb51985e6..05b9e9fda 100644 --- a/jit-binding-server/src/main/kotlin/io/github/typesafegithub/workflows/jitbindingserver/Main.kt +++ b/jit-binding-server/src/main/kotlin/io/github/typesafegithub/workflows/jitbindingserver/Main.kt @@ -26,6 +26,7 @@ import io.micrometer.core.instrument.Tag import io.micrometer.prometheusmetrics.PrometheusConfig import io.micrometer.prometheusmetrics.PrometheusMeterRegistry import java.time.Duration +import java.util.Optional import kotlin.time.Duration.Companion.hours private val logger = @@ -109,7 +110,7 @@ private fun buildBindingsCache( .newBuilder() .refreshAfterWrite(1.hours) .recordStats() - .asLoadingCache { buildVersionArtifacts(it, httpClient) } + .asLoadingCache { Optional.ofNullable(buildVersionArtifacts(it, httpClient)) } @Suppress("ktlint:standard:function-signature") // Conflict with detekt. private fun buildMetadataCache( diff --git a/jit-binding-server/src/test/kotlin/io/github/typesafegithub/workflows/jitbindingserver/ArtifactRoutesTest.kt b/jit-binding-server/src/test/kotlin/io/github/typesafegithub/workflows/jitbindingserver/ArtifactRoutesTest.kt index 1820b004c..0e4b16009 100644 --- a/jit-binding-server/src/test/kotlin/io/github/typesafegithub/workflows/jitbindingserver/ArtifactRoutesTest.kt +++ b/jit-binding-server/src/test/kotlin/io/github/typesafegithub/workflows/jitbindingserver/ArtifactRoutesTest.kt @@ -88,11 +88,9 @@ class ArtifactRoutesTest : // Then response2.status shouldBe HttpStatusCode.NotFound - // This test shows the current behavior where requesting a resource - // that doesn't exist twice in a row causes calling the version artifact - // twice. - // Fix in scope of https://github.com/typesafegithub/github-workflows-kt/issues/2160 - verify(exactly = 2) { mockBuildVersionArtifacts(any(), any()) } + // The fact that the resource doesn't exist is cached, and the + // resource generation logic isn't called in the second request. + verify(exactly = 1) { mockBuildVersionArtifacts(any(), any()) } } }