Skip to content

Commit 08a2100

Browse files
committed
Use redis-client as transport
`main` and `distributed` test suites pass. `cluster` and `sentinel` still need some updating
1 parent d113f69 commit 08a2100

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+278
-1923
lines changed

.rubocop.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ Metrics/BlockNesting:
139139
Style/HashTransformValues:
140140
Enabled: false
141141

142+
Style/TrailingCommaInHashLiteral:
143+
Enabled: false
144+
142145
Style/SymbolProc:
143146
Exclude:
144147
- 'test/**/*'

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
# Unreleased 5.0.0
44

5+
- `select` no longer record the current database. If the client has to reconnect after `select` was used, it will reconnect to the original database.
6+
- Removed `logger` option.
7+
- Removed `reconnect_delay_max` and `reconnect_delay`, you can pass precise sleep durations to `reconnect_attempts` instead.
58
- Require Ruby 2.5+.
69
- Removed the deprecated `queue` and `commit` methods. Use `pipelined` instead.
710
- Removed the deprecated `pipelined` and `multi` signature. Commands now MUST be called on the block argument, not the original redis instance.

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ gem 'rake'
99
gem 'rubocop', '~> 1.25.1'
1010
gem 'mocha'
1111
gem 'hiredis'
12+
gem 'redis-client', github: 'redis-rb/redis-client' # RESP2 supportnot yet released

benchmarking/logging.rb

Lines changed: 0 additions & 74 deletions
This file was deleted.

lib/redis.rb

Lines changed: 22 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ def deprecate!(message)
2424

2525
include Commands
2626

27+
SERVER_URL_OPTIONS = %i(url host port path).freeze
28+
2729
# Create a new client instance
2830
#
2931
# @param [Hash] options
@@ -37,13 +39,12 @@ def deprecate!(message)
3739
# @option options [Float] :connect_timeout (same as timeout) timeout for initial connect in seconds
3840
# @option options [String] :username Username to authenticate against server
3941
# @option options [String] :password Password to authenticate against server
40-
# @option options [Integer] :db (0) Database to select after initial connect
42+
# @option options [Integer] :db (0) Database to select after connect and on reconnects
4143
# @option options [Symbol] :driver Driver to use, currently supported: `:ruby`, `:hiredis`
4244
# @option options [String] :id ID for the client connection, assigns name to current connection by sending
4345
# `CLIENT SETNAME`
44-
# @option options [Hash, Integer] :tcp_keepalive Keepalive values, if Integer `intvl` and `probe` are calculated
45-
# based on the value, if Hash `time`, `intvl` and `probes` can be specified as a Integer
46-
# @option options [Integer] :reconnect_attempts Number of attempts trying to connect
46+
# @option options [Integer, Array<Integer, Float>] :reconnect_attempts Number of attempts trying to connect,
47+
# or a list of sleep duration between attempts.
4748
# @option options [Boolean] :inherit_socket (false) Whether to use socket in forked process or not
4849
# @option options [Array] :sentinels List of sentinels to contact
4950
# @option options [Symbol] :role (:master) Role to fetch via Sentinel, either `:master` or `:slave`
@@ -56,23 +57,22 @@ def deprecate!(message)
5657
# @return [Redis] a new client instance
5758
def initialize(options = {})
5859
@options = options.dup
60+
@options[:reconnect_attempts] = 1 unless @options.key?(:reconnect_attempts)
61+
if ENV["REDIS_URL"] && SERVER_URL_OPTIONS.none? { |o| @options.key?(o) }
62+
@options[:url] = ENV["REDIS_URL"]
63+
end
64+
inherit_socket = @options.delete(:inherit_socket)
5965
@cluster_mode = options.key?(:cluster)
6066
client = @cluster_mode ? Cluster : Client
61-
@original_client = @client = client.new(options)
67+
@original_client = @client = client.new(@options)
68+
@client.inherit_socket! if inherit_socket
6269
@queue = Hash.new { |h, k| h[k] = [] }
6370
@monitor = Monitor.new
6471
end
6572

66-
# Run code with the client reconnecting
67-
def with_reconnect(val = true, &blk)
68-
synchronize do |client|
69-
client.with_reconnect(val, &blk)
70-
end
71-
end
72-
7373
# Run code without the client reconnecting
74-
def without_reconnect(&blk)
75-
with_reconnect(false, &blk)
74+
def without_reconnect(&block)
75+
@original_client.disable_reconnection(&block)
7676
end
7777

7878
# Test whether or not the client is connected
@@ -82,7 +82,7 @@ def connected?
8282

8383
# Disconnect the client as quickly and silently as possible.
8484
def close
85-
@original_client.disconnect
85+
@original_client.close
8686
end
8787
alias disconnect! close
8888

@@ -95,59 +95,15 @@ def _client
9595
end
9696

9797
def pipelined
98-
pipeline = Pipeline.new(@client)
99-
pipelined_connection = PipelinedConnection.new(pipeline)
100-
yield pipelined_connection
10198
synchronize do |client|
102-
client.call_pipeline(pipeline)
103-
end
104-
end
105-
106-
# Mark the start of a transaction block.
107-
#
108-
# Passing a block is optional.
109-
#
110-
# @example With a block
111-
# redis.multi do |multi|
112-
# multi.set("key", "value")
113-
# multi.incr("counter")
114-
# end # => ["OK", 6]
115-
#
116-
# @example Without a block
117-
# redis.multi
118-
# # => "OK"
119-
# redis.set("key", "value")
120-
# # => "QUEUED"
121-
# redis.incr("counter")
122-
# # => "QUEUED"
123-
# redis.exec
124-
# # => ["OK", 6]
125-
#
126-
# @yield [multi] the commands that are called inside this block are cached
127-
# and written to the server upon returning from it
128-
# @yieldparam [Redis] multi `self`
129-
#
130-
# @return [String, Array<...>]
131-
# - when a block is not given, `OK`
132-
# - when a block is given, an array with replies
133-
#
134-
# @see #watch
135-
# @see #unwatch
136-
def multi
137-
if block_given?
138-
pipeline = Pipeline::Multi.new(@client)
139-
pipelined_connection = PipelinedConnection.new(pipeline)
140-
yield pipelined_connection
141-
synchronize do |client|
142-
client.call_pipeline(pipeline)
99+
client.pipelined do |raw_pipeline|
100+
yield PipelinedConnection.new(raw_pipeline)
143101
end
144-
else
145-
send_command([:multi])
146102
end
147103
end
148104

149105
def id
150-
@original_client.id
106+
@original_client.config.id || @original_client.config.server_url
151107
end
152108

153109
def inspect
@@ -165,8 +121,8 @@ def connection
165121
host: @original_client.host,
166122
port: @original_client.port,
167123
db: @original_client.db,
168-
id: @original_client.id,
169-
location: @original_client.location
124+
id: id,
125+
location: "#{@original_client.host}:#{@original_client.port}"
170126
}
171127
end
172128

@@ -184,15 +140,15 @@ def send_command(command, &block)
184140

185141
def send_blocking_command(command, timeout, &block)
186142
@monitor.synchronize do
187-
@client.call_with_timeout(command, timeout, &block)
143+
@client.blocking_call(timeout, command, &block)
188144
end
189145
end
190146

191147
def _subscription(method, timeout, channels, block)
192148
return @client.call([method] + channels) if subscribed?
193149

194150
begin
195-
original, @client = @client, SubscribedClient.new(@client)
151+
original, @client = @client, SubscribedClient.new(@client.pubsub)
196152
if timeout > 0
197153
@client.send(method, timeout, *channels, &block)
198154
else
@@ -205,7 +161,6 @@ def _subscription(method, timeout, channels, block)
205161
end
206162

207163
require "redis/version"
208-
require "redis/connection"
209164
require "redis/client"
210165
require "redis/cluster"
211166
require "redis/pipeline"

0 commit comments

Comments
 (0)