Skip to content

Commit

Permalink
Merge pull request #21 from digipost/solve-client-crashing
Browse files Browse the repository at this point in the history
Re-instantiate GithubApiClient regularly
  • Loading branch information
hermanwh authored Jun 13, 2024
2 parents 7a6f228 + 09e7314 commit 13615f6
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/main/kotlin/no/digipost/github/monitoring/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,15 @@ suspend fun main(): Unit = coroutineScope {
.register(prometheusMeterRegistry)

val apolloClientFactory = cachedApolloClientFactory(githubToken)
val githubApiClient = GithubApiClient(githubToken)
val githubApiClientFactory = cachedGithubApiClientFactory(githubToken)
val slackClient = slackWebhookUrl?.let{ SlackClient(it) }

launch {
while (isActive) {
try {
withTimeout(TIMOUT_PUBLISH_VULNS) {
val timeMillis = measureTimeMillis {
publish(apolloClientFactory.invoke(), githubApiClient, slackClient, severityLimitForNotifications, multiGaugeRepoVulnCount, multiGaugeContainerScan, multiGaugeInfoScore)
publish(apolloClientFactory.invoke(), githubApiClientFactory.invoke(), slackClient, severityLimitForNotifications, multiGaugeRepoVulnCount, multiGaugeContainerScan, multiGaugeInfoScore)
}
logger.info("Henting av repos med sårbarheter tok ${timeMillis}ms")
}
Expand All @@ -96,6 +96,28 @@ suspend fun main(): Unit = coroutineScope {
.startMetricsServer(prometheusMeterRegistry, 9610)
}

fun cachedGithubApiClientFactory(token: String): () -> GithubApiClient {

val fakt: (String) -> GithubApiClient = { t: String ->
GithubApiClient(t)
}

val age = AtomicLong(System.currentTimeMillis());
var client = fakt(token);

return {
if (System.currentTimeMillis() - age.get() < 1000 * 60 * 60 * 10) {
println("Cachet GithubApiClient")
client
} else {
println("Lager ny GithubApiClient")
client = fakt(token)
age.set(System.currentTimeMillis())
client
}
}
}

fun cachedApolloClientFactory(token: String): () -> ApolloClient {

val fakt: (String) -> ApolloClient = { t: String ->
Expand Down

0 comments on commit 13615f6

Please sign in to comment.