Add channel_prefix support to AC redis/evented_redis adapters #27425

Merged
merged 1 commit into from Jan 18, 2017

Projects

None yet

5 participants

@matrix9180
Contributor
matrix9180 commented Dec 21, 2016 edited

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

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
Contributor
@matrix9180 matrix9180 changed the title from #27224 Add channel_prefix support to AC redis/evented_redis adapters to Add channel_prefix support to AC redis/evented_redis adapters Dec 24, 2016
@rafaelfranca

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
@rafaelfranca
rafaelfranca Dec 29, 2016 Member

It don't need to be a concern.

@@ -11,6 +11,7 @@
module ActionCable
module SubscriptionAdapter
class EventedRedis < Base # :nodoc:
+ include Naming
@rafaelfranca
rafaelfranca Dec 29, 2016 Member

Put a blank line after this one

@@ -6,6 +6,7 @@
module ActionCable
module SubscriptionAdapter
class Redis < Base # :nodoc:
+ include Naming
@rafaelfranca
rafaelfranca Dec 29, 2016 Member

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
@matthewd
matthewd Dec 29, 2016 Member

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.)

@matrix9180
matrix9180 Jan 12, 2017 Contributor

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.

@matrix9180
matrix9180 Jan 13, 2017 Contributor

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)
@matthewd
matthewd Dec 29, 2016 Member

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

@matrix9180
matrix9180 Jan 12, 2017 Contributor

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)
@matthewd
matthewd Dec 29, 2016 Member

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
@matthewd
matthewd Dec 29, 2016 Member

:nodoc:

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

@matrix9180
Contributor

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

@matrix9180
Contributor

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
Contributor
@matrix9180
Contributor

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

@rafaelfranca

Could you squash your first 6 commits in one?

@matrix9180
Contributor
@matrix9180 matrix9180 Add channel_prefix support to ActionCable redis/evented_redis adapters.
a9c4dce
@matrix9180
Contributor

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

@rafaelfranca
Member

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

@matrix9180
Contributor

#27719 has the docs updates

@matrix9180
Contributor

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

@rafaelfranca
Member

It should be fine now

@rafaelfranca rafaelfranca merged commit 0196704 into rails:master Jan 18, 2017

2 checks passed

codeclimate no new or fixed issues
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@matrix9180 matrix9180 deleted the matrix9180:action_cable_channel_prefix branch Jan 21, 2017
@matrix9180 matrix9180 added a commit to matrix9180/rails that referenced this pull request Jan 22, 2017
@matrix9180 matrix9180 Add changelog entry for #27425 30def67
@matrix9180 matrix9180 added a commit to matrix9180/rails that referenced this pull request Jan 22, 2017
@matrix9180 matrix9180 Add changelog entry for #27425 [ci skip] 36a80b4
@travisofthenorth travisofthenorth added a commit to travisofthenorth/rails that referenced this pull request Jan 22, 2017
@matrix9180 @travisofthenorth matrix9180 + travisofthenorth Add changelog entry for #27425 [ci skip] f2f6ad1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment