From 09e73149fc9f113b16d3f15e827b18130d10ed90 Mon Sep 17 00:00:00 2001 From: Herman Wika Horn Date: Wed, 12 Jun 2024 15:31:48 +0200 Subject: [PATCH] Re-instantiate GithubApiClient regularly Our application typically crashes after some time (>1 day) when running in production, similarly to what we experienced with the ApolloClient some time ago. We hope that re-instantiating the client will solvee this issue. If not, we will have to do some form of try-catch within the coroutines, for instance resulting in a reboot. --- .../no/digipost/github/monitoring/Main.kt | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/no/digipost/github/monitoring/Main.kt b/src/main/kotlin/no/digipost/github/monitoring/Main.kt index 743a4ef..b079f4c 100644 --- a/src/main/kotlin/no/digipost/github/monitoring/Main.kt +++ b/src/main/kotlin/no/digipost/github/monitoring/Main.kt @@ -72,7 +72,7 @@ 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 { @@ -80,7 +80,7 @@ suspend fun main(): Unit = coroutineScope { 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") } @@ -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 ->