Class: ClickhouseNative::Pool
- Inherits:
-
Object
- Object
- ClickhouseNative::Pool
- Defined in:
- lib/clickhouse_native/pool.rb
Instance Attribute Summary collapse
-
#database ⇒ Object
readonly
Returns the value of attribute database.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Instance Method Summary collapse
- #describe_table(table, db_name: nil) ⇒ Object
- #execute(sql) ⇒ Object
-
#initialize(host:, port:, database: "default", user: "default", password: "", compression: :none, logger: nil, settings: {}, pool_size: 5, pool_timeout: 5) ⇒ Pool
constructor
A new instance of Pool.
- #insert(table, rows, **opts) ⇒ Object
- #ping ⇒ Object
- #query(sql) ⇒ Object
- #query_each(sql, &block) ⇒ Object
- #query_value(sql) ⇒ Object
- #server_version ⇒ Object
-
#with ⇒ Object
On exception, discard the client rather than reuse it: an error path leaves the socket in an unknown state.
Constructor Details
#initialize(host:, port:, database: "default", user: "default", password: "", compression: :none, logger: nil, settings: {}, pool_size: 5, pool_timeout: 5) ⇒ Pool
Returns a new instance of Pool.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/clickhouse_native/pool.rb', line 9 def initialize(host:, port:, database: "default", user: "default", password: "", compression: :none, logger: nil, settings: {}, pool_size: 5, pool_timeout: 5) @host = host @port = port @database = database client_kwargs = { host:, port:, database:, user:, password:, compression:, logger: } @set_sql = settings_sql(settings) @pool = ConnectionPool.new(size: pool_size, timeout: pool_timeout) do client = Client.new(**client_kwargs) client.execute(@set_sql) if @set_sql client end end |
Instance Attribute Details
#database ⇒ Object (readonly)
Returns the value of attribute database.
7 8 9 |
# File 'lib/clickhouse_native/pool.rb', line 7 def database @database end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
7 8 9 |
# File 'lib/clickhouse_native/pool.rb', line 7 def host @host end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
7 8 9 |
# File 'lib/clickhouse_native/pool.rb', line 7 def port @port end |
Instance Method Details
#describe_table(table, db_name: nil) ⇒ Object
84 85 86 |
# File 'lib/clickhouse_native/pool.rb', line 84 def describe_table(table, db_name: nil) with { |c| c.describe_table(table, db_name:) } end |
#execute(sql) ⇒ Object
56 57 58 |
# File 'lib/clickhouse_native/pool.rb', line 56 def execute(sql) with { |c| c.execute(sql) } end |
#insert(table, rows, **opts) ⇒ Object
72 73 74 |
# File 'lib/clickhouse_native/pool.rb', line 72 def insert(table, rows, **opts) with { |c| c.insert(table, rows, **opts) } end |
#ping ⇒ Object
76 77 78 |
# File 'lib/clickhouse_native/pool.rb', line 76 def ping with(&:ping) end |
#query(sql) ⇒ Object
60 61 62 |
# File 'lib/clickhouse_native/pool.rb', line 60 def query(sql) with { |c| c.query(sql) } end |
#query_each(sql, &block) ⇒ Object
64 65 66 |
# File 'lib/clickhouse_native/pool.rb', line 64 def query_each(sql, &block) with { |c| c.query_each(sql, &block) } end |
#query_value(sql) ⇒ Object
68 69 70 |
# File 'lib/clickhouse_native/pool.rb', line 68 def query_value(sql) with { |c| c.query_value(sql) } end |
#server_version ⇒ Object
80 81 82 |
# File 'lib/clickhouse_native/pool.rb', line 80 def server_version with(&:server_version) end |
#with ⇒ Object
On exception, discard the client rather than reuse it: an error path leaves the socket in an unknown state. The C++ binding issues ResetConnection, but a subsequent send can still surface buffered protocol errors from the prior aborted operation — those get attributed to whatever SQL we tried next (e.g. the SET reapplying session settings), producing misleading log lines and re-raises in unrelated code. A fresh socket + handshake is cheap relative to debugging that.
ConnectionError gets one automatic retry: pooled connections that have been idle long enough for the server / an LB to FIN them surface as “closed” on the very next recv (errno 0, message “closed: Success”). Discarding and re-checking out lands a fresh socket and the operation succeeds. The retry only triggers when the dead-connection error fired before any data was sent, so write operations don’t risk double-execution from this path.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/clickhouse_native/pool.rb', line 40 def with attempts = 0 begin @pool.with do |client| yield client rescue @pool.discard_current_connection(&:close) raise end rescue ConnectionError attempts += 1 retry if attempts == 1 raise end end |