11package 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
36import io.github.oshai.kotlinlogging.KotlinLogging.logger
7+ import io.github.typesafegithub.workflows.actionbindinggenerator.domain.ActionCoords
48import io.github.typesafegithub.workflows.actionbindinggenerator.domain.prettyPrintWithoutVersion
59import io.github.typesafegithub.workflows.mavenbinding.buildPackageArtifacts
610import io.github.typesafegithub.workflows.shared.internal.getGithubAuthToken
7- import io.ktor.http.HttpStatusCode
811import io.ktor.server.response.respondText
912import io.ktor.server.routing.Route
1013import io.ktor.server.routing.Routing
1114import io.ktor.server.routing.get
1215import 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
1420private 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
2647private 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