4

Hi i recently deploy ktor server project on server as rest api backend for my app using. I'm using netty and running it on server with system service. Ktor server is running on port 7171 and whenever i check connections with port 7171 it keep increasing. I'm checking with this command

ss -ant | grep :7171 | wc -l

After one day connection numbers 20k+ and server crash nothing work.

I think some connections keep open. IN logs i don't get any error except few errors like connection reset by peer.

I'm also using HttpClient with Apache and for caching list of data I'm storing data in companion object so not fetching it every time from database.

I reviewed code and have only doubt about above two things.

These are my gradle dependencies

implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version")
implementation("io.ktor:ktor-server-netty:$ktor_version")
implementation("io.ktor:ktor-client-apache:$ktor_version")
implementation("io.ktor:ktor-client-logging-native:$ktor_version")
implementation("io.ktor:ktor-gson:$ktor_version")
implementation("ch.qos.logback:logback-classic:$logback_version")
implementation("io.ktor:ktor-metrics:$ktor_version")
implementation("io.ktor:ktor-server-core:$ktor_version")
implementation("io.ktor:ktor-server-sessions:$ktor_version")
implementation("io.ktor:ktor-auth-jwt:$ktor_version")
implementation("org.jooq:jooq")
jooqGeneratorRuntime("mysql:mysql-connector-java:8.0.19")
implementation("mysql:mysql-connector-java:8.0.19")
implementation(group = "com.zaxxer", name = "HikariCP", version = "3.4.2")
implementation("io.sentry:sentry:1.7.30")
implementation("software.amazon.awssdk:s3:2.8.7")

Currently i have about 7k users and max concurrent users are 450. Please guide me how i can check issue and figure out problem.

Here is HttClient code:

suspend fun post(
        url: String,
        params: Map<String, String> = emptyMap(),
        headersMap: Map<String, String> = emptyMap()
    ): Result<String> {
        val httpClient = getHttpClient()
        return kotlin.runCatching {
            httpClient.post<String>(url) {
                body = MultiPartFormDataContent(
                    formData {
                        params.forEach {
                            append(it.key, it.value)
                        }
                    }
                )
                if (headersMap.isNotEmpty()) {
                    headersMap.forEach { (key, value) ->
                        header(key, value)
                    }
                }
            }.also {
                httpClient.close()
            }
        }.onFailure { httpClient.close() }
    }

    private fun getHttpClient(): HttpClient {
        return HttpClient(Apache) {
            install(HttpTimeout) {
                requestTimeoutMillis = 60000
            }
            engine {
                customizeClient {
                    sslContext = SSLContextBuilder.create().loadTrustMaterial(object : TrustStrategy {
                        override fun isTrusted(chain: Array<out X509Certificate>?, authType: String?): Boolean {
                            return true
                        }

                    }).build()
                    setSSLHostnameVerifier(NoopHostnameVerifier())
                }
            }
        }
    }

Also please check my api response header i think keepalive should have some expiry? enter image description here

William
  • 225
  • 4
  • 21

0 Answers0