Class: ClickhouseNative::Pool

Inherits:
Object
  • Object
show all
Defined in:
lib/clickhouse_native/pool.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#databaseObject (readonly)

Returns the value of attribute database.



7
8
9
# File 'lib/clickhouse_native/pool.rb', line 7

def database
  @database
end

#hostObject (readonly)

Returns the value of attribute host.



7
8
9
# File 'lib/clickhouse_native/pool.rb', line 7

def host
  @host
end

#portObject (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



69
70
71
# File 'lib/clickhouse_native/pool.rb', line 69

def describe_table(table, db_name: nil)
  with { |c| c.describe_table(table, db_name:) }
end

#execute(sql) ⇒ Object



41
42
43
# File 'lib/clickhouse_native/pool.rb', line 41

def execute(sql)
  with { |c| c.execute(sql) }
end

#insert(table, rows, **opts) ⇒ Object



57
58
59
# File 'lib/clickhouse_native/pool.rb', line 57

def insert(table, rows, **opts)
  with { |c| c.insert(table, rows, **opts) }
end

#pingObject



61
62
63
# File 'lib/clickhouse_native/pool.rb', line 61

def ping
  with(&:ping)
end

#query(sql) ⇒ Object



45
46
47
# File 'lib/clickhouse_native/pool.rb', line 45

def query(sql)
  with { |c| c.query(sql) }
end

#query_each(sql, &block) ⇒ Object



49
50
51
# File 'lib/clickhouse_native/pool.rb', line 49

def query_each(sql, &block)
  with { |c| c.query_each(sql, &block) }
end

#query_value(sql) ⇒ Object



53
54
55
# File 'lib/clickhouse_native/pool.rb', line 53

def query_value(sql)
  with { |c| c.query_value(sql) }
end

#server_versionObject



65
66
67
# File 'lib/clickhouse_native/pool.rb', line 65

def server_version
  with(&:server_version)
end

#withObject

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.



32
33
34
35
36
37
38
39
# File 'lib/clickhouse_native/pool.rb', line 32

def with
  @pool.with do |client|
    yield client
  rescue
    @pool.discard_current_connection(&:close)
    raise
  end
end