Skip to content

Commit 6807a3a

Browse files
committed
Hiredis: don't use redisReconnect if the config changed
1 parent 954eab2 commit 6807a3a

File tree

4 files changed

+17
-12
lines changed

4 files changed

+17
-12
lines changed

hiredis-client/lib/redis_client/hiredis_connection.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,8 @@ def initialize(ca_file: nil, ca_path: nil, cert: nil, key: nil, hostname: nil)
3939
end
4040
end
4141

42-
attr_reader :config
43-
4442
def initialize(config, connect_timeout:, read_timeout:, write_timeout:)
45-
super()
46-
@config = config
43+
super(config)
4744
self.connect_timeout = connect_timeout
4845
self.read_timeout = read_timeout
4946
self.write_timeout = write_timeout
@@ -136,7 +133,7 @@ def write_multi(commands)
136133
def connect
137134
_connect(@config.path, @config.host, @config.port, @config.ssl_context)
138135
rescue SystemCallError => error
139-
host = @config.path || "#{@config.host}:#{@config.port}"
136+
host = @path || "#{@host}:#{@port}"
140137
error_code = error.class.name.split("::").last
141138
raise CannotConnectError, "Failed to connect to #{host} (#{error_code})"
142139
end

lib/redis_client.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,7 @@ def raw_connection
786786
def connect
787787
@pid = PIDCache.pid
788788

789-
if @raw_connection
789+
if @raw_connection && @raw_connection.revalidate
790790
@middlewares.connect(config) do
791791
@raw_connection.reconnect
792792
end

lib/redis_client/connection_mixin.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@
33
class RedisClient
44
module ConnectionMixin
55
attr_accessor :retry_attempt
6+
attr_reader :config
67

7-
def initialize
8+
def initialize(config)
89
@pending_reads = 0
910
@retry_attempt = nil
11+
@config = config
12+
@path = @config.path
13+
@host = @config.host
14+
@port = @config.port
1015
end
1116

1217
def reconnect
@@ -20,7 +25,7 @@ def close
2025
end
2126

2227
def revalidate
23-
if @pending_reads > 0
28+
if @pending_reads > 0 && !config_changed?
2429
close
2530
false
2631
else
@@ -97,5 +102,11 @@ def connection_error(message)
97102
error._set_retry_attempt(@retry_attempt)
98103
error
99104
end
105+
106+
private
107+
108+
def config_changed?
109+
!(@path == @config.path && @host == @config.host && @port == @config.port)
110+
end
100111
end
101112
end

lib/redis_client/ruby_connection.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,8 @@ def ssl_context(ssl_params)
4040

4141
SUPPORTS_RESOLV_TIMEOUT = Socket.method(:tcp).parameters.any? { |p| p.last == :resolv_timeout }
4242

43-
attr_reader :config
44-
4543
def initialize(config, connect_timeout:, read_timeout:, write_timeout:)
46-
super()
47-
@config = config
44+
super(config)
4845
@connect_timeout = connect_timeout
4946
@read_timeout = read_timeout
5047
@write_timeout = write_timeout

0 commit comments

Comments
 (0)