Module: Legion::Data::Connection
- Defined in:
- lib/legion/data/connection.rb
Defined Under Namespace
Classes: QueryFileLogger, SlowQueryLogger
Constant Summary
collapse
- ADAPTERS =
%i[sqlite mysql2 postgres].freeze
- GENERIC_KEYS =
%i[max_connections pool_timeout preconnect single_threaded test name].freeze
- ADAPTER_KEYS =
{
sqlite: %i[timeout readonly disable_dqs],
postgres: %i[connect_timeout sslmode sslrootcert search_path],
mysql2: %i[connect_timeout read_timeout write_timeout encoding sql_mode]
}.freeze
- ADAPTER_DEFAULTS =
{
sqlite: { timeout: 5000, readonly: false, disable_dqs: true },
postgres: { connect_timeout: 20, sslmode: 'disable' },
mysql2: { connect_timeout: 120, encoding: 'utf8mb4' }
}.freeze
- QUERY_LOG_DIR =
File.expand_path('~/.legionio/logs').freeze
Class Attribute Summary collapse
Class Method Summary
collapse
Class Attribute Details
.sequel ⇒ Object
Returns the value of attribute sequel.
99
100
101
|
# File 'lib/legion/data/connection.rb', line 99
def sequel
@sequel
end
|
Class Method Details
.adapter ⇒ Object
101
102
103
|
# File 'lib/legion/data/connection.rb', line 101
def adapter
@adapter ||= Legion::Settings[:data][:adapter]&.to_sym || :sqlite
end
|
.connect_with_replicas ⇒ Object
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
# File 'lib/legion/data/connection.rb', line 186
def connect_with_replicas
return unless adapter == :postgres
replica_url = Legion::Settings[:data][:read_replica_url]
replica_list = Array(Legion::Settings[:data][:replicas]).dup
replica_list.prepend(replica_url) if replica_url && !replica_url.empty?
replica_list.uniq!
replica_list.compact!
return if replica_list.empty?
@sequel.extension(:server_block)
replica_list.each_with_index do |url, idx|
@sequel.add_servers("read_#{idx}": url)
end
@replica_servers = replica_list.each_with_index.map { |_, idx| :"read_#{idx}" }
Legion::Logging.debug "Registered #{@replica_servers.size} read replica(s)" if defined?(Legion::Logging)
end
|
.creds_builder(final_creds = {}) ⇒ Object
244
245
246
247
248
249
250
251
252
|
# File 'lib/legion/data/connection.rb', line 244
def creds_builder(final_creds = {})
final_creds.merge! Legion::Data::Settings.creds(adapter)
final_creds.merge! Legion::Settings[:data][:creds] if Legion::Settings[:data][:creds].is_a? Hash
port = final_creds[:port]
merge_tls_creds(final_creds, adapter: adapter, port: port)
final_creds
end
|
.merge_tls_creds(creds, adapter:, port:) ⇒ Object
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
# File 'lib/legion/data/connection.rb', line 218
def merge_tls_creds(creds, adapter:, port:)
return creds if adapter == :sqlite
return creds unless defined?(Legion::Crypt::TLS)
tls_settings = data_tls_settings
return creds unless tls_settings[:enabled] == true
tls = Legion::Crypt::TLS.resolve(tls_settings, port: port)
return creds unless tls[:enabled]
case adapter
when :postgres
creds[:sslmode] = tls[:verify] == :none ? 'require' : 'verify-full'
creds[:sslrootcert] = tls[:ca] if tls[:ca]
creds[:sslcert] = tls[:cert] if tls[:cert]
creds[:sslkey] = tls[:key] if tls[:key]
when :mysql2
creds[:ssl_mode] = tls[:verify] == :none ? 'required' : 'verify_identity'
creds[:sslca] = tls[:ca] if tls[:ca]
creds[:sslcert] = tls[:cert] if tls[:cert]
creds[:sslkey] = tls[:key] if tls[:key]
end
creds
end
|
.pool_stats ⇒ Object
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
# File 'lib/legion/data/connection.rb', line 146
def pool_stats
return {} unless @sequel
pool = @sequel.pool
stats = {
type: pool.pool_type,
size: pool.size,
max_size: pool.respond_to?(:max_size) ? pool.max_size : nil
}
case pool.pool_type
when :timed_queue, :sharded_timed_queue
queue_size = pool.instance_variable_get(:@queue)&.size || 0
stats[:available] = queue_size
stats[:in_use] = stats[:size] - queue_size
stats[:waiting] = pool.num_waiting
when :threaded, :sharded_threaded
avail = pool.instance_variable_get(:@available_connections)
stats[:available] = avail&.size || 0
stats[:in_use] = stats[:size] - stats[:available]
stats[:waiting] = pool.num_waiting
when :single, :sharded_single
stats[:available] = pool.size
stats[:in_use] = 0
stats[:waiting] = 0
end
stats.compact
rescue StandardError
{}
end
|
.read_server ⇒ Object
208
209
210
211
212
|
# File 'lib/legion/data/connection.rb', line 208
def read_server
return :default if @replica_servers.nil? || @replica_servers.empty?
:read_0
end
|
.replica_servers ⇒ Object
214
215
216
|
# File 'lib/legion/data/connection.rb', line 214
def replica_servers
@replica_servers || []
end
|
.setup ⇒ Object
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
# File 'lib/legion/data/connection.rb', line 105
def setup
opts = sequel_opts
@sequel = if adapter == :sqlite
::Sequel.connect(opts.merge(adapter: :sqlite, database: sqlite_path))
else
begin
::Sequel.connect(opts.merge(adapter: adapter, **creds_builder))
rescue StandardError => e
raise unless dev_fallback?
if defined?(Legion::Logging)
Legion::Logging.warn(
"Shared DB unreachable (#{e.message}), dev_mode fallback to SQLite"
)
end
@adapter = :sqlite
sqlite_opts = sequel_opts
::Sequel.connect(sqlite_opts.merge(adapter: :sqlite, database: sqlite_path))
end
end
Legion::Settings[:data][:connected] = true
log_connection_info if defined?(Legion::Logging)
configure_extensions
connect_with_replicas
end
|
.shutdown ⇒ Object
178
179
180
181
182
183
184
|
# File 'lib/legion/data/connection.rb', line 178
def shutdown
@sequel&.disconnect
@query_file_logger&.close
@query_file_logger = nil
Legion::Settings[:data][:connected] = false
Legion::Logging.info 'Legion::Data connection closed' if defined?(Legion::Logging)
end
|
.stats ⇒ Object
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
# File 'lib/legion/data/connection.rb', line 131
def stats
return { connected: false } unless @sequel
data = Legion::Settings[:data]
{
connected: data[:connected],
adapter: adapter,
pool: pool_stats,
tuning: tuning_stats(data),
database: database_stats
}
rescue StandardError => e
{ connected: (data[:connected] if data.is_a?(Hash)), adapter: adapter, error: e.message }
end
|