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

Add channel_prefix support to AC redis/evented_redis adapters #27425

Merged
merged 1 commit into from Jan 18, 2017

Conversation

matrix9180
Copy link
Contributor

@matrix9180 matrix9180 commented Dec 21, 2016

Summary

#27224

In Redis, Pub/Sub has no relation to the key space. It was made to not interfere with it on any level, including database numbers.
Publishing on db 10, will be heard by a subscriber on db 1.
(https://redis.io/topics/pubsub#database-amp-scoping)

To avoid problems when multiple Rails applications use the same Redis instance, the redis/evented_redis subscription adapters now support a channel_prefix option (specified in cable.yml)

Other backends do not appear to have this issue.

The generated cable.yml has also been updated to reflect this change.

@rails-bot
Copy link

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @rafaelfranca (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review.

Please see the contribution instructions for more information.

@matrix9180
Copy link
Contributor Author

cc @matthewd

@matrix9180 matrix9180 force-pushed the action_cable_channel_prefix branch from 12ce7f1 to 633c677 Compare Dec 21, 2016
@matrix9180 matrix9180 changed the title #27224 Add channel_prefix support to AC redis/evented_redis adapters Add channel_prefix support to AC redis/evented_redis adapters Dec 24, 2016
Copy link
Member

@rafaelfranca rafaelfranca left a comment

Choose a reason for hiding this comment

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

Thank you for the pull request. It makes sense to me. I made some minor comments in the files.

module ActionCable
module SubscriptionAdapter
module Naming
extend ActiveSupport::Concern
Copy link
Member

Choose a reason for hiding this comment

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

It don't need to be a concern.

@@ -11,6 +11,7 @@
module ActionCable
module SubscriptionAdapter
class EventedRedis < Base # :nodoc:
include Naming
Copy link
Member

Choose a reason for hiding this comment

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

Put a blank line after this one

@@ -6,6 +6,7 @@
module ActionCable
module SubscriptionAdapter
class Redis < Base # :nodoc:
include Naming
Copy link
Member

Choose a reason for hiding this comment

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

Put a blank line after this one

@rafaelfranca rafaelfranca requested a review from matthewd Dec 29, 2016
@@ -14,3 +14,9 @@ def cable_config
super.merge(driver: "hiredis")
end
end

class RedisAdapterWithChannelPrefixTest < RedisAdapterTest
Copy link
Member

Choose a reason for hiding this comment

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

I'm pretty sure this test would pass if it was added without any of the above implementation.

"It still works" is a good baseline, but we'll also need to prove we're achieving our aim: that two differently-configured adapters don't interfere with each other. (Ideally, we can make that a generic test for all adapters -- some will pass naturally, and others will be able to pull in the namespacing module.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm having trouble writing tests with two differently configured adapters as ActionCable::Server::Base's config is defined as a cattr_accessor, so when I make a second one & give it a new config, it shares config with the first.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I worked around my issue by subclassing ActionCable::Server::Base and making config an instance var. I made the tests an included module in the tests as I wasn't sure how to generalize it as the pg adapter is scoped by database. To generalize it, we could have all adapters support channel prefix, as while it's not needed by all adapters, it shouldn't hurt anything either.

@@ -7,3 +7,7 @@ test:
production:
adapter: redis
url: redis://localhost:6379/1
# To avoid channel collisions when multiple use the same Redis instance,
# we prefix channel names with the app/environment name.
# (https://redis.io/topics/pubsub#database-amp-scoping)
Copy link
Member

Choose a reason for hiding this comment

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

This info probably better lives in the "how to configure the adapter" documentation. Do we have any of that at the moment? 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think docs around ac adapter configuration needs some work. Don't think it even mentions the existence of a Postgres adapter for example. :)

@@ -17,15 +18,15 @@ def initialize(*)
end

def broadcast(channel, payload)
redis_connection_for_broadcasts.publish(channel, payload)
redis_connection_for_broadcasts.publish(channel_name(channel), payload)
Copy link
Member

Choose a reason for hiding this comment

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

Instead of making the adapter call channel_name, if we prepend the module, it could define broadcast/subscribe/unsubscribe methods that mangle the name then super.

@@ -0,0 +1,12 @@
module ActionCable
module SubscriptionAdapter
module Naming
Copy link
Member

Choose a reason for hiding this comment

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

:nodoc:

How about we call this ChannelPrefix or similar? SubscriptionAdapter::Naming feels a bit too vague to me.

@matrix9180
Copy link
Contributor Author

Github didn't send me any notices about the comments here. Will make the requested changes.

@matrix9180 matrix9180 force-pushed the action_cable_channel_prefix branch from ff1a9e1 to 53974b4 Compare Jan 13, 2017
@matrix9180
Copy link
Contributor Author

I believe I addressed everything here except documentation updates, but I'm not sure where those would go as documentation on configuring actioncable is rather sparse at the moment.

@matrix9180
Copy link
Contributor Author

r? @rafaelfranca @matthewd

@matrix9180
Copy link
Contributor Author

Added some documentation on SubscriptionAdapter configuration to the ActionCable Overview guide. Not sure if it's in-scope for this PR or not.

Copy link
Member

@rafaelfranca rafaelfranca left a comment

Choose a reason for hiding this comment

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

Could you squash your first 6 commits in one?

@matrix9180
Copy link
Contributor Author

matrix9180 commented Jan 18, 2017 via email

@matrix9180 matrix9180 force-pushed the action_cable_channel_prefix branch from ccd5020 to a9c4dce Compare Jan 18, 2017
@matrix9180
Copy link
Contributor Author

Rebased & squashed. Shall I open a separate PR for the documentation updates?

@rafaelfranca
Copy link
Member

It was fine in a separated commit here but since you pulled it off a separate PR would be better

@matrix9180
Copy link
Contributor Author

#27719 has the docs updates

@matrix9180
Copy link
Contributor Author

Noticed the build failed on 2.3.3. Transient error? Couldn't reproduce it locally.

@rafaelfranca
Copy link
Member

It should be fine now

@rafaelfranca rafaelfranca merged commit 0196704 into rails:master Jan 18, 2017
@matrix9180 matrix9180 deleted the action_cable_channel_prefix branch Jan 21, 2017
matrix9180 added a commit to matrix9180/rails that referenced this pull request Jan 22, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants