Action Cable redis-rb 5+ compatibility

- Pass configuration as symbols
- Stop messing with the raw client
This commit is contained in:
Jean Boussier 2022-08-19 12:48:20 +02:00
parent 71c24aa0bc
commit 7587b7e99a
4 changed files with 42 additions and 33 deletions

@ -1,9 +1,6 @@
# frozen_string_literal: true
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gemspec
if RUBY_VERSION < "3"

@ -1,3 +1,9 @@
* The Redis adapter is now compatible with redis-rb 5.0
Compatibility with redis-rb 3.x was dropped.
*Jean Boussier*
* The Action Cable server is now mounted with `anchor: true`.
This means that routes that also start with `/cable` will no longer clash with Action Cable.

@ -54,7 +54,7 @@ def redis_connection_for_broadcasts
end
def redis_connection
self.class.redis_connector.call(@server.config.cable.merge(id: identifier))
self.class.redis_connector.call(@server.config.cable.symbolize_keys.merge(id: identifier))
end
class Listener < SubscriberMap
@ -161,36 +161,42 @@ def when_connected(&block)
end
end
class SubscribedClient
def initialize(raw_client)
@raw_client = raw_client
end
def subscribe(*channel)
send_command("subscribe", *channel)
end
def unsubscribe(*channel)
send_command("unsubscribe", *channel)
end
private
def send_command(*command)
@raw_client.write(command)
very_raw_connection =
@raw_client.connection.instance_variable_defined?(:@connection) &&
@raw_client.connection.instance_variable_get(:@connection)
if very_raw_connection && very_raw_connection.respond_to?(:flush)
very_raw_connection.flush
end
nil
if ::Redis::VERSION < "5"
class SubscribedClient
def initialize(raw_client)
@raw_client = raw_client
end
end
def extract_subscribed_client(conn)
SubscribedClient.new(conn._client)
def subscribe(*channel)
send_command("subscribe", *channel)
end
def unsubscribe(*channel)
send_command("unsubscribe", *channel)
end
private
def send_command(*command)
@raw_client.write(command)
very_raw_connection =
@raw_client.connection.instance_variable_defined?(:@connection) &&
@raw_client.connection.instance_variable_get(:@connection)
if very_raw_connection && very_raw_connection.respond_to?(:flush)
very_raw_connection.flush
end
nil
end
end
def extract_subscribed_client(conn)
SubscribedClient.new(conn._client)
end
else
def extract_subscribed_client(conn)
conn
end
end
end
end

@ -48,7 +48,7 @@ def expected_connection
end
test "sets connection id for connection" do
assert_called_with ::Redis, :new, [ expected_connection.stringify_keys ] do
assert_called_with ::Redis, :new, [ expected_connection.symbolize_keys ] do
@adapter.send(:redis_connection)
end
end