@@ -24,6 +24,8 @@ def deprecate!(message)
24
24
25
25
include Commands
26
26
27
+ SERVER_URL_OPTIONS = %i( url host port path ) . freeze
28
+
27
29
# Create a new client instance
28
30
#
29
31
# @param [Hash] options
@@ -37,13 +39,12 @@ def deprecate!(message)
37
39
# @option options [Float] :connect_timeout (same as timeout) timeout for initial connect in seconds
38
40
# @option options [String] :username Username to authenticate against server
39
41
# @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
41
43
# @option options [Symbol] :driver Driver to use, currently supported: `:ruby`, `:hiredis`
42
44
# @option options [String] :id ID for the client connection, assigns name to current connection by sending
43
45
# `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.
47
48
# @option options [Boolean] :inherit_socket (false) Whether to use socket in forked process or not
48
49
# @option options [Array] :sentinels List of sentinels to contact
49
50
# @option options [Symbol] :role (:master) Role to fetch via Sentinel, either `:master` or `:slave`
@@ -56,23 +57,22 @@ def deprecate!(message)
56
57
# @return [Redis] a new client instance
57
58
def initialize ( options = { } )
58
59
@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 )
59
65
@cluster_mode = options . key? ( :cluster )
60
66
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
62
69
@queue = Hash . new { |h , k | h [ k ] = [ ] }
63
70
@monitor = Monitor . new
64
71
end
65
72
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
-
73
73
# 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 )
76
76
end
77
77
78
78
# Test whether or not the client is connected
@@ -82,7 +82,7 @@ def connected?
82
82
83
83
# Disconnect the client as quickly and silently as possible.
84
84
def close
85
- @original_client . disconnect
85
+ @original_client . close
86
86
end
87
87
alias disconnect! close
88
88
@@ -95,59 +95,15 @@ def _client
95
95
end
96
96
97
97
def pipelined
98
- pipeline = Pipeline . new ( @client )
99
- pipelined_connection = PipelinedConnection . new ( pipeline )
100
- yield pipelined_connection
101
98
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 )
143
101
end
144
- else
145
- send_command ( [ :multi ] )
146
102
end
147
103
end
148
104
149
105
def id
150
- @original_client . id
106
+ @original_client . config . id || @original_client . config . server_url
151
107
end
152
108
153
109
def inspect
@@ -165,8 +121,8 @@ def connection
165
121
host : @original_client . host ,
166
122
port : @original_client . port ,
167
123
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 } "
170
126
}
171
127
end
172
128
@@ -184,15 +140,15 @@ def send_command(command, &block)
184
140
185
141
def send_blocking_command ( command , timeout , &block )
186
142
@monitor . synchronize do
187
- @client . call_with_timeout ( command , timeout , &block )
143
+ @client . blocking_call ( timeout , command , &block )
188
144
end
189
145
end
190
146
191
147
def _subscription ( method , timeout , channels , block )
192
148
return @client . call ( [ method ] + channels ) if subscribed?
193
149
194
150
begin
195
- original , @client = @client , SubscribedClient . new ( @client )
151
+ original , @client = @client , SubscribedClient . new ( @client . pubsub )
196
152
if timeout > 0
197
153
@client . send ( method , timeout , *channels , &block )
198
154
else
@@ -205,7 +161,6 @@ def _subscription(method, timeout, channels, block)
205
161
end
206
162
207
163
require "redis/version"
208
- require "redis/connection"
209
164
require "redis/client"
210
165
require "redis/cluster"
211
166
require "redis/pipeline"
0 commit comments