ClickHouse / ClickHouse/clickhouse-java

clickhouse-http-client: how do I close the connection in case of an error?

Open
#1,712 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

area:network bug triaged usability
Dominant language
Java
Stars
1.6k
Forks
637
Avg merge
2d 23h
Merged PRs (30d)
29

Description

When the client cancels the request, the connection to the asynchronous request is not closed

I use spring-boot in combination with clickhouse-http-client and Kotlin
Library Version: com.clickhouse:clickhouse-http-client:0.6.1

Steps to reproduce
  1. If the client sends 10 read requests and cancels them, the limit in max_open_connections (ClickHouseHttpOption.MAX_OPEN_CONNECTIONS) will be reached, because, in fact, the connection will not be closed by anyone.
Code example
package com.api.test

import com.clickhouse.client.ClickHouseClient
import com.clickhouse.client.ClickHouseNode
import com.clickhouse.data.ClickHouseFormat
import kotlinx.coroutines.future.await
import org.slf4j.LoggerFactory
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import kotlin.coroutines.cancellation.CancellationException

@RestController
@RequestMapping("/test")
class TestApi(
    private val clickHouseClient: ClickHouseClient,
    private val clickhouseNode: ClickHouseNode,
) {
    private val logger = LoggerFactory.getLogger(TestApi::class.java)

    @GetMapping("/read")
    suspend fun read() {
        try {
            val response = clickHouseClient.read(clickhouseNode)
                .format(ClickHouseFormat.JSON)
                .query("""SELECT sleep(3)""".trimIndent())
                .execute()
                .await()
            response.close()
        } catch (e: CancellationException) {
            logger.error("Error", e)
            // How do I close the connection?
        }
    }
}
Question

ClickHouse has the cancel_http_readonly_queries_on_client_close option. But how can I use the library to initiate connection closure instead of waiting for a response? Or maybe I can cancel a previously sent request?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing ClickHouseClient.read(...).execute() and the coroutine await path used in the example, then inspect how ClickHouseHttpOption.MAX_OPEN_CONNECTIONS and response.close() are handled during cancellation. Reproduce the cancelled requests and determine the expected connection and server-query cleanup behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin, spring-boot
Domain
backend, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.