|
| 1 | +/** |
| 2 | + * ShinyProxy-Operator |
| 3 | + * |
| 4 | + * Copyright (C) 2021-2022 Open Analytics |
| 5 | + * |
| 6 | + * =========================================================================== |
| 7 | + * |
| 8 | + * This program is free software: you can redistribute it and/or modify |
| 9 | + * it under the terms of the Apache License as published by |
| 10 | + * The Apache Software Foundation, either version 2 of the License, or |
| 11 | + * (at your option) any later version. |
| 12 | + * |
| 13 | + * This program is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + * Apache License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the Apache License |
| 19 | + * along with this program. If not, see <http://www.apache.org/licenses/> |
| 20 | + */ |
| 21 | +package eu.openanalytics.shinyproxyoperator.controller |
| 22 | + |
| 23 | +import com.fasterxml.jackson.annotation.JsonProperty |
| 24 | +import com.fasterxml.jackson.databind.ObjectMapper |
| 25 | +import com.fasterxml.jackson.module.kotlin.registerKotlinModule |
| 26 | +import eu.openanalytics.shinyproxyoperator.crd.ShinyProxy |
| 27 | +import eu.openanalytics.shinyproxyoperator.crd.ShinyProxyInstance |
| 28 | +import mu.KotlinLogging |
| 29 | +import okhttp3.OkHttpClient |
| 30 | +import okhttp3.Request |
| 31 | +import java.io.IOException |
| 32 | +import java.util.concurrent.TimeUnit |
| 33 | + |
| 34 | + |
| 35 | +class RecyclableChecker( |
| 36 | + private val podRetriever: PodRetriever, |
| 37 | +) { |
| 38 | + |
| 39 | + private val logger = KotlinLogging.logger {} |
| 40 | + private val client: OkHttpClient = OkHttpClient.Builder() |
| 41 | + .connectTimeout(3, TimeUnit.SECONDS) |
| 42 | + .readTimeout(3, TimeUnit.SECONDS) |
| 43 | + .build() |
| 44 | + |
| 45 | + private val objectMapper = ObjectMapper().registerKotlinModule() |
| 46 | + |
| 47 | + data class Response(@JsonProperty("isRecyclable") val isRecyclable: Boolean, @JsonProperty("activeConnections") val activeConnections: Int) |
| 48 | + |
| 49 | + fun isInstanceRecyclable(shinyProxy: ShinyProxy, shinyProxyInstance: ShinyProxyInstance): Boolean { |
| 50 | + val pods = podRetriever.getShinyProxyPods(shinyProxy, shinyProxyInstance) |
| 51 | + |
| 52 | + for (pod in pods) { |
| 53 | + val url = "http://${pod.status.podIP}:9090/actuator/recyclable" |
| 54 | + val request = Request.Builder() |
| 55 | + .url(url) |
| 56 | + .build() |
| 57 | + |
| 58 | + val body = try { |
| 59 | + client.newCall(request).execute().body?.string() |
| 60 | + } catch (e: IOException) { |
| 61 | + logger.warn { "${shinyProxy.logPrefix(shinyProxyInstance)} unreachable for recyclable check (using ${url})" } |
| 62 | + // server unreachable -> do not delete it yet |
| 63 | + return false |
| 64 | + } |
| 65 | + if (body == null) { |
| 66 | + // server unreachable -> do not delete it yet |
| 67 | + return false |
| 68 | + } |
| 69 | + val resp = objectMapper.readValue(body, Response::class.java) |
| 70 | + if (!resp.isRecyclable) { |
| 71 | + logger.info { "${shinyProxy.logPrefix(shinyProxyInstance)} Replica has ${resp.activeConnections} open websocket connections" } |
| 72 | + return false |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return true |
| 77 | + } |
| 78 | + |
| 79 | +} |
0 commit comments