From adc463bd5ad617489244cf5aa37b348ff5bdb877 Mon Sep 17 00:00:00 2001 From: Mostafa Dahab Date: Tue, 13 Feb 2024 02:30:02 +0300 Subject: [PATCH 1/3] Add read timeout Signed-off-by: Mostafa Dahab --- README.md | 2 ++ lib/click_house/config.rb | 2 ++ lib/click_house/connection.rb | 1 + 3 files changed, 5 insertions(+) diff --git a/README.md b/README.md index 227f04b..ae8f5cc 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,7 @@ ClickHouse.config do |config| config.url = 'http://localhost:8123' config.timeout = 60 config.open_timeout = 3 + config.read_timeout = 50 config.ssl_verify = false # set to true to symbolize keys for SELECT and INSERT statements (type casting) config.symbolize_keys = false @@ -414,6 +415,7 @@ default: &default url: http://localhost:8123 timeout: 60 open_timeout: 3 + read_timeout: 50 development: database: ecliptic_development diff --git a/lib/click_house/config.rb b/lib/click_house/config.rb index 3405069..61edbc9 100644 --- a/lib/click_house/config.rb +++ b/lib/click_house/config.rb @@ -14,6 +14,7 @@ class Config password: nil, timeout: nil, open_timeout: nil, + read_timeout: nil, ssl_verify: false, headers: {}, global_params: {}, @@ -46,6 +47,7 @@ class Config attr_accessor :password attr_accessor :timeout attr_accessor :open_timeout + attr_accessor :read_timeout attr_accessor :ssl_verify attr_accessor :headers attr_accessor :global_params diff --git a/lib/click_house/connection.rb b/lib/click_house/connection.rb index a832713..2bee9ed 100644 --- a/lib/click_house/connection.rb +++ b/lib/click_house/connection.rb @@ -52,6 +52,7 @@ def transport @transport ||= Faraday.new(config.url!) do |conn| conn.options.timeout = config.timeout conn.options.open_timeout = config.open_timeout + conn.options.read_timeout = config.read_timeout conn.headers = config.headers conn.ssl.verify = config.ssl_verify From 1163686cd7c6c9ebf05078afcc0539f566ce557f Mon Sep 17 00:00:00 2001 From: Mostafa Dahab Date: Tue, 13 Feb 2024 02:30:22 +0300 Subject: [PATCH 2/3] Add adaptor config options Signed-off-by: Mostafa Dahab --- lib/click_house/config.rb | 2 ++ lib/click_house/connection.rb | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/click_house/config.rb b/lib/click_house/config.rb index 61edbc9..fc959a1 100644 --- a/lib/click_house/config.rb +++ b/lib/click_house/config.rb @@ -4,6 +4,7 @@ module ClickHouse class Config DEFAULTS = { adapter: Faraday.default_adapter, + adapter_options: [], url: nil, scheme: 'http', host: 'localhost', @@ -37,6 +38,7 @@ class Config }.freeze attr_accessor :adapter + attr_accessor :adapter_options attr_accessor :logger attr_accessor :scheme attr_accessor :host diff --git a/lib/click_house/connection.rb b/lib/click_house/connection.rb index 2bee9ed..eb4e4f5 100644 --- a/lib/click_house/connection.rb +++ b/lib/click_house/connection.rb @@ -69,7 +69,7 @@ def transport conn.response Middleware::SummaryMiddleware, options: { config: config } # should be after logger conn.response config.json_parser, content_type: %r{application/json}, options: { config: config } conn.response Middleware::ParseCsv, content_type: %r{text/csv}, options: { config: config } - conn.adapter config.adapter + conn.adapter config.adapter, *config.adapter_options end end # rubocop:enable Metrics/AbcSize From 2c0136519b421780d36599c62898537b9747ec43 Mon Sep 17 00:00:00 2001 From: Omar Bahareth Date: Mon, 25 Mar 2024 01:19:53 +0300 Subject: [PATCH 3/3] Handle Case Where Clickhouse Returns Incorrect Content Type Clickhouse sometimes returns a Content type starting with `text/plain` when the body is actually JSON. As a temporary fix, we'll handle this case by attempting to parse the string into JSON, and if that works, we'll continue with `body` being a `Hash`. If it doesn't work, we'll proceed as before. --- lib/click_house/response/factory.rb | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/click_house/response/factory.rb b/lib/click_house/response/factory.rb index c8b8240..df2e21a 100644 --- a/lib/click_house/response/factory.rb +++ b/lib/click_house/response/factory.rb @@ -3,8 +3,8 @@ module ClickHouse module Response class Factory - KEY_META = 'meta' - KEY_DATA = 'data' + KEY_META = "meta" + KEY_DATA = "data" # @return [ResultSet] # @params faraday [Faraday::Response] @@ -12,6 +12,16 @@ class Factory def self.response(faraday, config) body = faraday.body + # Clickhouse can return strings containing JSON sometimes, where the + # content-type is text/plain but the body is actually JSON + if faraday.headers["content-type"].start_with?("text/plain") && body.is_a?(String) + begin + parsed_body = JSON.parse(body) + body = parsed_body if parsed_body.is_a?(Hash) + rescue JSON::ParserError + end + end + # wrap to be able to use connection#select_one, connection#select_value # with other formats like binary return raw(faraday, config) unless body.is_a?(Hash)