Class: ChConnect::Connection
- Inherits:
-
Object
- Object
- ChConnect::Connection
- Defined in:
- lib/ch_connect/connection.rb
Overview
A pooled ClickHouse connection built on the clickhouse-c ioless client: the C extension is a pure protocol state machine + block decoder, and all socket I/O, TLS, and timeouts live here in Ruby. Result blocks are parsed in C.
Maintains a pool of connections (config.pool_size); the native protocol handles one query at a time per connection, so concurrent queries each check out their own connection.
Constant Summary collapse
- COMPRESSION_AVAILABLE =
{ nil => true, :lz4 => NativeClient::LZ4_AVAILABLE, :zstd => NativeClient::ZSTD_AVAILABLE }.freeze
- READ_CHUNK =
64 * 1024
- WRITE_CHUNK =
64 * 1024
- PARAM_ESCAPES =
{ "\0" => "\\0", "\a" => "\\a", "\b" => "\\b", "\e" => "\\e", "\f" => "\\f", "\n" => "\\n", "\r" => "\\r", "\t" => "\\t", "\v" => "\\v", "'" => "\\'", "\\" => "\\\\" }.freeze
- PARAM_ESCAPE_PATTERN =
/[\0\a\b\e\f\n\r\t\v'\\]/- RESERVED_SETTINGS =
{ "network_compression_method" => "set config.compression instead", "output_format_native_encode_types_in_binary_format" => "required by the native decoder" }.freeze
Instance Attribute Summary collapse
-
#config ⇒ Config
readonly
The configuration used by this connection.
Instance Method Summary collapse
-
#close ⇒ void
Closes all pooled connections.
-
#initialize(config = ChConnect.config) ⇒ Connection
constructor
Creates a new connection.
-
#query(sql, params: nil, settings: nil, idempotent: false) ⇒ Response
Executes and instruments a SQL query.
Constructor Details
#initialize(config = ChConnect.config) ⇒ Connection
Creates a new connection.
237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/ch_connect/connection.rb', line 237 def initialize(config = ChConnect.config) @config = config.dup.freeze validate_compression! @codec_name = @config.compression&.to_s @pool = ConnectionPool.new( size: @config.pool_size, timeout: @config.pool_timeout, auto_reload_after_fork: true ) do Slot.new(@config) end end |
Instance Attribute Details
#config ⇒ Config (readonly)
Returns the configuration used by this connection.
232 233 234 |
# File 'lib/ch_connect/connection.rb', line 232 def config @config end |
Instance Method Details
#close ⇒ void
This method returns an undefined value.
Closes all pooled connections.
269 270 271 |
# File 'lib/ch_connect/connection.rb', line 269 def close @pool.shutdown(&:close) end |
#query(sql, params: nil, settings: nil, idempotent: false) ⇒ Response
Executes and instruments a SQL query.
260 261 262 263 264 |
# File 'lib/ch_connect/connection.rb', line 260 def query(sql, params: nil, settings: nil, idempotent: false) @config.instrumenter.instrument("query.clickhouse", {sql: sql}) do execute_query(sql, params, settings, idempotent) end end |