How to set ActiveRecord request timeout for mysql?

How to set mysql query timeout in ActiveRecord? I want to set it to something very short, for example, 10-15 ms. This is for a Sinatra ruby ​​web application.

Thanks.

+4
source share
2 answers

Well, it looks like in these lines 29 and 30 in mysql_adapter.rb,

@connection.options(Mysql::OPT_READ_TIMEOUT, @config[:read_timeout]) if @config[:read_timeout] @connection.options(Mysql::OPT_WRITE_TIMEOUT, @config[:write_timeout]) if @config[:write_timeout] 

You just need to add the read_timeout and write_timeout values ​​to the .yaml database configuration file.

In this way,

 development: adapter: mysql encoding: utf8 database: app_development pool: 5 username: root password: write_timeout: 1 read_timeout: 1 

Use the trick to set read and write timeouts for 1 second. Unfortunately, this does not allow you to set intermediate timeouts.

+7
source

You can also set it for each connection as follows:

 ActiveRecord::Base.connection.instance_variable_get('@connection').instance_variable_set('@read_timeout', 0) 

I do this, for example, to read read_timeout by default, but override it for my lengthy nightly scripts.

0
source

Source: https://habr.com/ru/post/1345807/


All Articles