Skip to content

Commit 210e56c

Browse files
authored
fix(server): support caching absence of version artifacts (#2183)
Part of #2160. The goal is to reduce the number of calls to GitHub and hopefully make the server more stable when lots of requests come.
1 parent 0220209 commit 210e56c

File tree

3 files changed

+9
-8
lines changed

3 files changed

+9
-8
lines changed

jit-binding-server/src/main/kotlin/io/github/typesafegithub/workflows/jitbindingserver/ArtifactRoutes.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ import io.micrometer.prometheusmetrics.PrometheusMeterRegistry
2525
import kotlinx.coroutines.CoroutineScope
2626
import kotlinx.coroutines.Dispatchers
2727
import kotlinx.coroutines.launch
28+
import java.util.Optional
29+
import kotlin.jvm.optionals.getOrNull
2830

2931
private val logger = logger { }
3032

31-
typealias CachedVersionArtifact = VersionArtifacts?
33+
typealias CachedVersionArtifact = Optional<VersionArtifacts>
3234

3335
private val prefetchScope = CoroutineScope(Dispatchers.IO)
3436

@@ -120,7 +122,7 @@ private suspend fun ApplicationCall.toBindingArtifacts(
120122
if (refresh) {
121123
bindingsCache.invalidate(actionCoords)
122124
}
123-
return bindingsCache.get(actionCoords)
125+
return bindingsCache.get(actionCoords).getOrNull()
124126
}
125127

126128
private fun PrometheusMeterRegistry.incrementArtifactCounter(

jit-binding-server/src/main/kotlin/io/github/typesafegithub/workflows/jitbindingserver/Main.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import io.micrometer.core.instrument.Tag
2626
import io.micrometer.prometheusmetrics.PrometheusConfig
2727
import io.micrometer.prometheusmetrics.PrometheusMeterRegistry
2828
import java.time.Duration
29+
import java.util.Optional
2930
import kotlin.time.Duration.Companion.hours
3031

3132
private val logger =
@@ -109,7 +110,7 @@ private fun buildBindingsCache(
109110
.newBuilder()
110111
.refreshAfterWrite(1.hours)
111112
.recordStats()
112-
.asLoadingCache<ActionCoords, CachedVersionArtifact> { buildVersionArtifacts(it, httpClient) }
113+
.asLoadingCache { Optional.ofNullable(buildVersionArtifacts(it, httpClient)) }
113114

114115
@Suppress("ktlint:standard:function-signature") // Conflict with detekt.
115116
private fun buildMetadataCache(

jit-binding-server/src/test/kotlin/io/github/typesafegithub/workflows/jitbindingserver/ArtifactRoutesTest.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,9 @@ class ArtifactRoutesTest :
8888
// Then
8989
response2.status shouldBe HttpStatusCode.NotFound
9090

91-
// This test shows the current behavior where requesting a resource
92-
// that doesn't exist twice in a row causes calling the version artifact
93-
// twice.
94-
// Fix in scope of https://github.com/typesafegithub/github-workflows-kt/issues/2160
95-
verify(exactly = 2) { mockBuildVersionArtifacts(any(), any()) }
91+
// The fact that the resource doesn't exist is cached, and the
92+
// resource generation logic isn't called in the second request.
93+
verify(exactly = 1) { mockBuildVersionArtifacts(any(), any()) }
9694
}
9795
}
9896

0 commit comments

Comments
 (0)