Skip to content

Commit ee4a510

Browse files
authored
feat(server): cache the generation of the metadata files (#1911)
Fixes #1903
1 parent 30aaba9 commit ee4a510

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ fun main() {
4545
internalRoutes(prometheusRegistry)
4646

4747
artifactRoutes(prometheusRegistry)
48-
metadataRoutes()
48+
metadataRoutes(prometheusRegistry)
4949
}
5050
}.start(wait = true)
5151
}
Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,40 @@
11
package io.github.typesafegithub.workflows.jitbindingserver
22

3+
import com.github.benmanes.caffeine.cache.Caffeine
4+
import com.sksamuel.aedile.core.asLoadingCache
5+
import com.sksamuel.aedile.core.refreshAfterWrite
36
import io.github.oshai.kotlinlogging.KotlinLogging.logger
7+
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.ActionCoords
48
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.prettyPrintWithoutVersion
59
import io.github.typesafegithub.workflows.mavenbinding.buildPackageArtifacts
610
import io.github.typesafegithub.workflows.shared.internal.getGithubAuthToken
7-
import io.ktor.http.HttpStatusCode
811
import io.ktor.server.response.respondText
912
import io.ktor.server.routing.Route
1013
import io.ktor.server.routing.Routing
1114
import io.ktor.server.routing.get
1215
import io.ktor.server.routing.route
16+
import io.micrometer.core.instrument.binder.cache.CaffeineCacheMetrics
17+
import io.micrometer.prometheusmetrics.PrometheusMeterRegistry
18+
import kotlin.time.Duration.Companion.hours
1319

1420
private val logger = logger { }
1521

16-
fun Routing.metadataRoutes() {
22+
typealias MetadataResult = Result<Map<String, String>>
23+
24+
private val metadataCache =
25+
Caffeine
26+
.newBuilder()
27+
.refreshAfterWrite(1.hours)
28+
.recordStats()
29+
.asLoadingCache<ActionCoords, MetadataResult> {
30+
runCatching {
31+
it.buildPackageArtifacts(githubAuthToken = getGithubAuthToken())
32+
}
33+
}
34+
35+
fun Routing.metadataRoutes(prometheusRegistry: PrometheusMeterRegistry) {
36+
CaffeineCacheMetrics.monitor(prometheusRegistry, metadataCache.underlying(), "metadata_cache")
37+
1738
route("{owner}/{name}/{file}") {
1839
metadata()
1940
}
@@ -25,21 +46,26 @@ fun Routing.metadataRoutes() {
2546

2647
private fun Route.metadata(refresh: Boolean = false) {
2748
get {
28-
if (refresh && !deliverOnRefreshRoute) return@get call.respondNotFound()
29-
30-
val file = call.parameters["file"] ?: return@get call.respondNotFound()
3149
val actionCoords = call.parameters.extractActionCoords(extractVersion = false)
3250

3351
logger.info { "➡️ Requesting metadata for ${actionCoords.prettyPrintWithoutVersion}" }
3452

35-
val bindingArtifacts = actionCoords.buildPackageArtifacts(githubAuthToken = getGithubAuthToken())
36-
if (file in bindingArtifacts) {
37-
when (val artifact = bindingArtifacts[file]) {
53+
if (refresh) {
54+
metadataCache.invalidate(actionCoords)
55+
}
56+
val metadataArtifacts = metadataCache.get(actionCoords).getOrThrow()
57+
58+
if (refresh && !deliverOnRefreshRoute) return@get call.respondText(text = "OK")
59+
60+
val file = call.parameters["file"] ?: return@get call.respondNotFound()
61+
62+
if (file in metadataArtifacts) {
63+
when (val artifact = metadataArtifacts[file]) {
3864
is String -> call.respondText(text = artifact)
39-
else -> call.respondText(text = "Not found", status = HttpStatusCode.NotFound)
65+
else -> call.respondNotFound()
4066
}
4167
} else {
42-
call.respondText(text = "Not found", status = HttpStatusCode.NotFound)
68+
call.respondNotFound()
4369
}
4470
}
4571
}

0 commit comments

Comments
 (0)