1
1
package io.github.typesafegithub.workflows.jitbindingserver
2
2
3
3
import com.github.benmanes.caffeine.cache.Caffeine
4
+ import com.sksamuel.aedile.core.LoadingCache
4
5
import com.sksamuel.aedile.core.asLoadingCache
5
6
import com.sksamuel.aedile.core.refreshAfterWrite
6
7
import io.github.oshai.kotlinlogging.KotlinLogging.logger
@@ -35,41 +36,46 @@ typealias ArtifactResult = Result<Map<String, Artifact>>
35
36
36
37
private val prefetchScope = CoroutineScope (Dispatchers .IO )
37
38
38
- private val bindingsCache =
39
+ internal fun buildBindingsCache (): LoadingCache < ActionCoords , ArtifactResult > =
39
40
Caffeine
40
41
.newBuilder()
41
42
.refreshAfterWrite(1 .hours)
42
43
.recordStats()
43
44
.asLoadingCache<ActionCoords , ArtifactResult > { runCatching { it.buildVersionArtifacts()!! } }
44
45
45
- fun Routing.artifactRoutes (prometheusRegistry : PrometheusMeterRegistry ? = null) {
46
+ fun Routing.artifactRoutes (
47
+ bindingsCache : LoadingCache <ActionCoords , ArtifactResult >,
48
+ prometheusRegistry : PrometheusMeterRegistry ? = null,
49
+ ) {
46
50
prometheusRegistry?.let {
47
51
CaffeineCacheMetrics .monitor(it, bindingsCache.underlying(), " bindings_cache" )
48
52
}
49
53
50
54
route(" {owner}/{name}/{version}/{file}" ) {
51
- artifact(prometheusRegistry, refresh = false )
55
+ artifact(prometheusRegistry, bindingsCache, refresh = false )
52
56
}
53
57
54
58
route(" /refresh/{owner}/{name}/{version}/{file}" ) {
55
- artifact(prometheusRegistry, refresh = true )
59
+ artifact(prometheusRegistry, bindingsCache, refresh = true )
56
60
}
57
61
}
58
62
59
63
private fun Route.artifact (
60
64
prometheusRegistry : PrometheusMeterRegistry ? ,
65
+ bindingsCache : LoadingCache <ActionCoords , ArtifactResult >,
61
66
refresh : Boolean = false,
62
67
) {
63
- headArtifact(prometheusRegistry, refresh)
64
- getArtifact(prometheusRegistry, refresh)
68
+ headArtifact(bindingsCache, prometheusRegistry, refresh)
69
+ getArtifact(bindingsCache, prometheusRegistry, refresh)
65
70
}
66
71
67
72
private fun Route.headArtifact (
73
+ bindingsCache : LoadingCache <ActionCoords , ArtifactResult >,
68
74
prometheusRegistry : PrometheusMeterRegistry ? ,
69
75
refresh : Boolean ,
70
76
) {
71
77
head {
72
- val bindingArtifacts = call.toBindingArtifacts(refresh) ? : return @head call.respondNotFound()
78
+ val bindingArtifacts = call.toBindingArtifacts(refresh, bindingsCache ) ? : return @head call.respondNotFound()
73
79
74
80
val file = call.parameters[" file" ] ? : return @head call.respondNotFound()
75
81
@@ -84,11 +90,12 @@ private fun Route.headArtifact(
84
90
}
85
91
86
92
private fun Route.getArtifact (
93
+ bindingsCache : LoadingCache <ActionCoords , ArtifactResult >,
87
94
prometheusRegistry : PrometheusMeterRegistry ? ,
88
95
refresh : Boolean ,
89
96
) {
90
97
get {
91
- val bindingArtifacts = call.toBindingArtifacts(refresh) ? : return @get call.respondNotFound()
98
+ val bindingArtifacts = call.toBindingArtifacts(refresh, bindingsCache ) ? : return @get call.respondNotFound()
92
99
93
100
if (refresh && ! deliverOnRefreshRoute) return @get call.respondText(text = " OK" )
94
101
@@ -105,13 +112,19 @@ private fun Route.getArtifact(
105
112
}
106
113
}
107
114
108
- internal fun prefetchBindingArtifacts (coords : Collection <ActionCoords >) {
115
+ internal fun prefetchBindingArtifacts (
116
+ coords : Collection <ActionCoords >,
117
+ bindingsCache : LoadingCache <ActionCoords , ArtifactResult >,
118
+ ) {
109
119
prefetchScope.launch {
110
120
bindingsCache.getAll(coords)
111
121
}
112
122
}
113
123
114
- private suspend fun ApplicationCall.toBindingArtifacts (refresh : Boolean ): Map <String , Artifact >? {
124
+ private suspend fun ApplicationCall.toBindingArtifacts (
125
+ refresh : Boolean ,
126
+ bindingsCache : LoadingCache <ActionCoords , ArtifactResult >,
127
+ ): Map <String , Artifact >? {
115
128
val actionCoords = parameters.extractActionCoords(extractVersion = true )
116
129
117
130
logger.info { " ➡️ Requesting ${actionCoords.prettyPrint} " }
0 commit comments