Skip to content
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

Support ActiveRecord 5.0 #210

Merged
merged 5 commits into from
Oct 24, 2016
Merged

Conversation

hyoshida
Copy link
Contributor

I was using em-synchrony in Rails 4.2. It works perfectly.

But em-synchrony couldn't be running after update Rails to 5.0. The error is something like this:

undefined local variable or method `current_connection_id' for #<ActiveRecord::ConnectionAdapters::ConnectionPool:0x000008135f5c38>
/usr/home/hyoshida/src/sample/vendor/bundle/ruby/2.3.0/gems/em-synchrony-1.0.5/lib/em-synchrony/activerecord.rb:12:in `block in connection'
/usr/home/hyoshida/src/sample/vendor/bundle/ruby/2.3.0/gems/em-synchrony-1.0.5/lib/em-synchrony/thread.rb:63:in `synchronize'
/usr/home/hyoshida/src/sample/vendor/bundle/ruby/2.3.0/gems/em-synchrony-1.0.5/lib/em-synchrony/activerecord.rb:11:in `connection'

I think the cause is the following change in ActiveRecord:

# ActiveRecord::ConnectionAdapters::ConnectionPool#connection
AR 4.2: @reserved_connections[current_connection_id] ||= checkout
AR 5.0: @thread_cached_conns[connection_cache_key(Thread.current)] ||= checkout

I tried to fix this error. Hope you like it.

@dgutov
Copy link
Collaborator

dgutov commented Oct 16, 2016

The definition of connection in 5.0 is as simple as you say, but in 4.2 it also has a synchronize call.

Looks like this change will add some overhead under 4.2 which the current code tries to avoid.

@hyoshida
Copy link
Contributor Author

Thank you for the reply, @dgutov.

Indeed, My change will add overhead under Rails 4.2.
I wrote a monkey patch as a workaround.

By the way, I think em-synchrony does not need ActiveRecord's thread-safe implementation, Because ActiveRecord on em-synchrony is always working with single thread. It's for this reason that em-synchrony will remove synchronize for Thread, right?
Please tell me if I'm wrong.

@dgutov
Copy link
Collaborator

dgutov commented Oct 23, 2016

@hyoshida Could we move the version check to above the level of def connection? And define the function one or the other way depending on the result.

By the way, I think em-synchrony does not need ActiveRecord's thread-safe implementation, Because ActiveRecord on em-synchrony is always working with single thread. It's for this reason that em-synchrony will remove synchronize for Thread, right?

I... guess? Are you proposing some further change?

@hyoshida
Copy link
Contributor Author

@hyoshida Could we move the version check to above the level of def connection? And define the function one or the other way depending on the result.

OK, Fixed it.

Are you proposing some further change?

No. I just wanted to confirm.

@dgutov
Copy link
Collaborator

dgutov commented Oct 24, 2016

No. I just wanted to confirm.

OK. If I understand your question right, it's not important that we only use one thread. Because we synchronize on a "fibered mutex" instead, and it's more granular than synchronizing on a thread.

@dgutov dgutov merged commit 8554ced into igrigorik:master Oct 24, 2016
@dgutov
Copy link
Collaborator

dgutov commented Oct 24, 2016

Thanks!

@hyoshida
Copy link
Contributor Author

OK. If I understand your question right, it's not important that we only use one thread. Because we synchronize on a "fibered mutex" instead, and it's more granular than synchronizing on a thread.

I see.

Thank you for the answer and merge :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants