Skip to content

Allow per-query read_timeout #955

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -26,3 +26,7 @@ Style/TrailingCommaInLiteral:

Style/TrivialAccessors:
AllowPredicates: true

Metrics/BlockLength:
Exclude:
- 'spec/**/*.rb'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

8 changes: 6 additions & 2 deletions ext/mysql2/client.c
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@

VALUE cMysql2Client;
extern VALUE mMysql2, cMysql2Error, cMysql2TimeoutError;
static VALUE sym_id, sym_version, sym_header_version, sym_async, sym_symbolize_keys, sym_as, sym_array, sym_stream;
static VALUE sym_id, sym_version, sym_header_version, sym_async, sym_symbolize_keys, sym_as, sym_array, sym_stream, sym_read_timeout;
static VALUE sym_no_good_index_used, sym_no_index_used, sym_query_was_slow;
static ID intern_brackets, intern_merge, intern_merge_bang, intern_new_with_args;

@@ -639,7 +639,10 @@ static VALUE do_query(void *args) {
int retval;
VALUE read_timeout;

read_timeout = rb_iv_get(async_args->self, "@read_timeout");
read_timeout = rb_hash_aref(rb_iv_get(async_args->self, "@current_query_options"), sym_read_timeout);
if (NIL_P(read_timeout)) {
read_timeout = rb_iv_get(async_args->self, "@read_timeout");
}

tvp = NULL;
if (!NIL_P(read_timeout)) {
@@ -1452,6 +1455,7 @@ void init_mysql2_client() {
sym_as = ID2SYM(rb_intern("as"));
sym_array = ID2SYM(rb_intern("array"));
sym_stream = ID2SYM(rb_intern("stream"));
sym_read_timeout = ID2SYM(rb_intern("read_timeout"));

sym_no_good_index_used = ID2SYM(rb_intern("no_good_index_used"));
sym_no_index_used = ID2SYM(rb_intern("no_index_used"));
14 changes: 14 additions & 0 deletions spec/mysql2/client_spec.rb
Original file line number Diff line number Diff line change
@@ -963,6 +963,20 @@ def run_gc
end.not_to raise_error
end

context 'with query-level timeout' do
it "time outs on long queries" do
expect do
@client.query("select sleep(2)", read_timeout: 1)
end.to raise_error(Mysql2::Error::TimeoutError)
end

it "does not time out on queries under timeout" do
expect do
@client.query("select sleep(1)", read_timeout: 2)
end.not_to raise_error
end
end

context 'write operations api' do
before(:each) do
@client.query "USE test"