Skip to content

Commit ca491bc

Browse files
committed
Get rid of @original_client
Now that we no longer swap the client during pipelining and such, we can get rid of that mess.
1 parent 08a2100 commit ca491bc

File tree

4 files changed

+34
-34
lines changed

4 files changed

+34
-34
lines changed

lib/redis.rb

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -64,25 +64,27 @@ def initialize(options = {})
6464
inherit_socket = @options.delete(:inherit_socket)
6565
@cluster_mode = options.key?(:cluster)
6666
client = @cluster_mode ? Cluster : Client
67-
@original_client = @client = client.new(@options)
67+
@subscription_client = nil
68+
@client = client.new(@options)
6869
@client.inherit_socket! if inherit_socket
6970
@queue = Hash.new { |h, k| h[k] = [] }
7071
@monitor = Monitor.new
7172
end
7273

7374
# Run code without the client reconnecting
7475
def without_reconnect(&block)
75-
@original_client.disable_reconnection(&block)
76+
@client.disable_reconnection(&block)
7677
end
7778

7879
# Test whether or not the client is connected
7980
def connected?
80-
@original_client.connected?
81+
@client.connected? || @subscription_client&.connected?
8182
end
8283

8384
# Disconnect the client as quickly and silently as possible.
8485
def close
85-
@original_client.close
86+
@client.close
87+
@subscription_client&.close
8688
end
8789
alias disconnect! close
8890

@@ -103,7 +105,7 @@ def pipelined
103105
end
104106

105107
def id
106-
@original_client.config.id || @original_client.config.server_url
108+
@client.config.id || @client.config.server_url
107109
end
108110

109111
def inspect
@@ -115,14 +117,14 @@ def dup
115117
end
116118

117119
def connection
118-
return @original_client.connection_info if @cluster_mode
120+
return @client.connection_info if @cluster_mode
119121

120122
{
121-
host: @original_client.host,
122-
port: @original_client.port,
123-
db: @original_client.db,
123+
host: @client.host,
124+
port: @client.port,
125+
db: @client.db,
124126
id: id,
125-
location: "#{@original_client.host}:#{@original_client.port}"
127+
location: "#{@client.host}:#{@client.port}"
126128
}
127129
end
128130

@@ -145,17 +147,19 @@ def send_blocking_command(command, timeout, &block)
145147
end
146148

147149
def _subscription(method, timeout, channels, block)
148-
return @client.call([method] + channels) if subscribed?
150+
if @subscription_client
151+
return @subscription_client.call([method] + channels)
152+
end
149153

150154
begin
151-
original, @client = @client, SubscribedClient.new(@client.pubsub)
155+
@subscription_client = SubscribedClient.new(@client.pubsub)
152156
if timeout > 0
153-
@client.send(method, timeout, *channels, &block)
157+
@subscription_client.send(method, timeout, *channels, &block)
154158
else
155-
@client.send(method, *channels, &block)
159+
@subscription_client.send(method, *channels, &block)
156160
end
157161
ensure
158-
@client = original
162+
@subscription_client = nil
159163
end
160164
end
161165
end

lib/redis/commands/pubsub.rb

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ def publish(channel, message)
99
end
1010

1111
def subscribed?
12-
synchronize do |client|
13-
client.is_a? SubscribedClient
14-
end
12+
!@subscription_client.nil?
1513
end
1614

1715
# Listen for messages published to the given channels.
@@ -31,10 +29,10 @@ def subscribe_with_timeout(timeout, *channels, &block)
3129

3230
# Stop listening for messages posted to the given channels.
3331
def unsubscribe(*channels)
34-
synchronize do |client|
35-
raise "Can't unsubscribe if not subscribed." unless subscribed?
32+
raise "Can't unsubscribe if not subscribed." unless subscribed?
3633

37-
client.unsubscribe(*channels)
34+
synchronize do |_client|
35+
_subscription(:unsubscribe, 0, channels, nil)
3836
end
3937
end
4038

@@ -55,10 +53,10 @@ def psubscribe_with_timeout(timeout, *channels, &block)
5553

5654
# Stop listening for messages posted to channels matching the given patterns.
5755
def punsubscribe(*channels)
58-
synchronize do |client|
59-
raise "Can't unsubscribe if not subscribed." unless subscribed?
56+
raise "Can't unsubscribe if not subscribed." unless subscribed?
6057

61-
client.punsubscribe(*channels)
58+
synchronize do |_client|
59+
_subscription(:punsubscribe, 0, channels, nil)
6260
end
6361
end
6462

test/distributed/publish_subscribe_test.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,10 @@ def test_subscribe_within_subscribe
7474
end
7575

7676
def test_other_commands_within_a_subscribe
77-
assert_raises Redis::CommandError do
78-
r.subscribe("foo") do |on|
79-
on.subscribe do |_channel, _total|
80-
r.set("bar", "s2")
81-
end
77+
r.subscribe("foo") do |on|
78+
on.subscribe do |_channel, _total|
79+
r.set("bar", "s2")
80+
r.unsubscribe("foo")
8281
end
8382
end
8483
end

test/redis/publish_subscribe_test.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,10 @@ def test_subscribe_within_subscribe
198198
end
199199

200200
def test_other_commands_within_a_subscribe
201-
assert_raises Redis::CommandError do
202-
r.subscribe("foo") do |on|
203-
on.subscribe do |_channel, _total|
204-
r.set("bar", "s2")
205-
end
201+
r.subscribe("foo") do |on|
202+
on.subscribe do |_channel, _total|
203+
r.set("bar", "s2")
204+
r.unsubscribe("foo")
206205
end
207206
end
208207
end

0 commit comments

Comments
 (0)