redis 6.0.0 → 6.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +114 -0
- data/README.md +172 -5
- data/lib/redis/client.rb +5 -1
- data/lib/redis/commands/arrays.rb +358 -0
- data/lib/redis/commands/bitmaps.rb +24 -0
- data/lib/redis/commands/bless.rb +115 -0
- data/lib/redis/commands/cluster.rb +9 -2
- data/lib/redis/commands/hashes.rb +250 -7
- data/lib/redis/commands/keys.rb +48 -11
- data/lib/redis/commands/modules/json.rb +27 -2
- data/lib/redis/commands/modules/search/aggregation.rb +2 -1
- data/lib/redis/commands/modules/search/field.rb +11 -2
- data/lib/redis/commands/modules/search/hybrid.rb +3 -0
- data/lib/redis/commands/modules/search/index.rb +2 -1
- data/lib/redis/commands/modules/search/miscellaneous.rb +2 -1
- data/lib/redis/commands/modules/search/query.rb +2 -2
- data/lib/redis/commands/modules/search/schema.rb +4 -0
- data/lib/redis/commands/scripting.rb +109 -0
- data/lib/redis/commands/server.rb +158 -7
- data/lib/redis/commands/sets.rb +270 -0
- data/lib/redis/commands/sorted_sets.rb +131 -6
- data/lib/redis/commands/streams.rb +284 -33
- data/lib/redis/commands/strings.rb +232 -6
- data/lib/redis/commands/transactions.rb +6 -1
- data/lib/redis/commands/vector_sets.rb +407 -0
- data/lib/redis/commands.rb +169 -6
- data/lib/redis/distributed.rb +539 -22
- data/lib/redis/hash_ring.rb +2 -1
- data/lib/redis/keyspace_notifications/channels.rb +121 -0
- data/lib/redis/keyspace_notifications/manager.rb +1121 -0
- data/lib/redis/keyspace_notifications/notification.rb +95 -0
- data/lib/redis/keyspace_notifications/parser.rb +170 -0
- data/lib/redis/keyspace_notifications.rb +59 -0
- data/lib/redis/pipeline.rb +17 -5
- data/lib/redis/ractorize.rb +6 -0
- data/lib/redis/subscribe.rb +80 -2
- data/lib/redis/version.rb +1 -1
- data/lib/redis/xxh3.rb +354 -0
- data/lib/redis.rb +29 -8
- metadata +16 -6
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Redis
|
|
4
|
+
module KeyspaceNotifications
|
|
5
|
+
# A parsed keyspace, keyevent or subkey notification.
|
|
6
|
+
#
|
|
7
|
+
# Immutable value object produced by {Parser.parse}. +key+ and the +subkeys+
|
|
8
|
+
# elements are BINARY (ASCII-8BIT) encoded strings, because Redis keys and
|
|
9
|
+
# subkeys may contain arbitrary bytes; call +force_encoding+ on them if your
|
|
10
|
+
# application guarantees UTF-8 keys.
|
|
11
|
+
class Notification
|
|
12
|
+
FAMILIES = %i[keyspace keyevent subkeyspace subkeyevent subkeyspaceitem subkeyspaceevent].freeze
|
|
13
|
+
|
|
14
|
+
# @return [Symbol] the channel family, one of {FAMILIES}
|
|
15
|
+
# @return [Integer, String] the database the event happened in, or "*" when the
|
|
16
|
+
# channel itself carried a database wildcard
|
|
17
|
+
# @return [String] the event name (e.g. "set", "expired", "hdel"). Opaque: new
|
|
18
|
+
# server events flow through without a library update
|
|
19
|
+
# @return [String] the affected key (BINARY encoded)
|
|
20
|
+
# @return [Array<String>] the affected subkeys, in server order with duplicates
|
|
21
|
+
# preserved; empty for keyspace/keyevent, exactly one for subkeyspaceitem
|
|
22
|
+
# @return [String] the raw pub/sub channel the notification arrived on
|
|
23
|
+
# @return [String] the raw pub/sub message payload
|
|
24
|
+
# @return [String, nil] the psubscribe pattern that matched, when delivered via pmessage
|
|
25
|
+
attr_reader :family, :db, :event, :key, :subkeys, :channel, :payload, :pattern
|
|
26
|
+
|
|
27
|
+
# @param family [Symbol] one of {FAMILIES}
|
|
28
|
+
# @param db [Integer, String] database index or "*"
|
|
29
|
+
# @param event [String] event name
|
|
30
|
+
# @param key [String] affected key
|
|
31
|
+
# @param channel [String] raw channel
|
|
32
|
+
# @param payload [String] raw payload
|
|
33
|
+
# @param subkeys [Array<String>] affected subkeys
|
|
34
|
+
# @param pattern [String, nil] matched psubscribe pattern
|
|
35
|
+
def initialize(family:, db:, event:, key:, channel:, payload:, subkeys: [], pattern: nil)
|
|
36
|
+
@family = family
|
|
37
|
+
@db = frozen_copy(db) # Integers pass through; the "*" String form gets copied
|
|
38
|
+
@event = frozen_copy(event)
|
|
39
|
+
@key = frozen_copy(key)
|
|
40
|
+
@subkeys = subkeys.map { |subkey| frozen_copy(subkey) }.freeze
|
|
41
|
+
@channel = frozen_copy(channel)
|
|
42
|
+
@payload = frozen_copy(payload)
|
|
43
|
+
@pattern = frozen_copy(pattern)
|
|
44
|
+
# Deep-frozen: the value object implements #hash / #eql?, so no field may be
|
|
45
|
+
# mutable after construction (a mutated Hash key becomes unreachable).
|
|
46
|
+
freeze
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# Convenience accessor for single-subkey families (subkeyspaceitem).
|
|
50
|
+
#
|
|
51
|
+
# @return [String, nil] the first subkey, or nil when there are none
|
|
52
|
+
def subkey
|
|
53
|
+
@subkeys.first
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# @return [Boolean] whether this notification belongs to one of the Redis 8.8
|
|
57
|
+
# subkey channel families (and therefore may carry subkeys)
|
|
58
|
+
def subkey_family?
|
|
59
|
+
@family != :keyspace && @family != :keyevent
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# @return [Hash{Symbol => Object}] all fields as a hash
|
|
63
|
+
def to_h
|
|
64
|
+
{
|
|
65
|
+
family: @family,
|
|
66
|
+
db: @db,
|
|
67
|
+
event: @event,
|
|
68
|
+
key: @key,
|
|
69
|
+
subkeys: @subkeys,
|
|
70
|
+
channel: @channel,
|
|
71
|
+
payload: @payload,
|
|
72
|
+
pattern: @pattern
|
|
73
|
+
}
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# @param other [Object] the object to compare against
|
|
77
|
+
# @return [Boolean] whether +other+ is a Notification with equal fields
|
|
78
|
+
def ==(other)
|
|
79
|
+
other.is_a?(Notification) && to_h == other.to_h
|
|
80
|
+
end
|
|
81
|
+
alias eql? ==
|
|
82
|
+
|
|
83
|
+
# @return [Integer] a hash derived from all fields
|
|
84
|
+
def hash
|
|
85
|
+
to_h.hash
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
private
|
|
89
|
+
|
|
90
|
+
def frozen_copy(value)
|
|
91
|
+
value.nil? || value.frozen? ? value : value.dup.freeze
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Redis
|
|
4
|
+
module KeyspaceNotifications
|
|
5
|
+
# Binary-safe parsing of keyspace, keyevent and subkey notification messages.
|
|
6
|
+
#
|
|
7
|
+
# Keys and subkeys may contain arbitrary bytes — including the delimiter bytes
|
|
8
|
+
# used by the wire formats (":", "|", ",", "\n") — so parsing never splits keys
|
|
9
|
+
# or subkeys by delimiter: subkey lists are length-prefixed and only the first
|
|
10
|
+
# occurrence of a family's structural delimiter is significant.
|
|
11
|
+
module Parser
|
|
12
|
+
HEADER = /\A__(keyspace|keyevent|subkeyspace|subkeyevent|subkeyspaceitem|subkeyspaceevent)@(\d+|\*)__:/
|
|
13
|
+
|
|
14
|
+
COMMA = 0x2C # ","
|
|
15
|
+
PIPE = 0x7C # "|"
|
|
16
|
+
|
|
17
|
+
module_function
|
|
18
|
+
|
|
19
|
+
# Parse a pub/sub message received on a notification channel.
|
|
20
|
+
#
|
|
21
|
+
# @param channel [String] the channel the message arrived on
|
|
22
|
+
# @param payload [String] the message payload
|
|
23
|
+
# @param pattern [String, nil] the psubscribe pattern that matched, if any
|
|
24
|
+
# @return [Notification, nil] the parsed notification, or nil when +channel+
|
|
25
|
+
# is not a notification channel
|
|
26
|
+
# @raise [ParseError] when the channel belongs to a notification family but the
|
|
27
|
+
# channel suffix or payload is malformed
|
|
28
|
+
def parse(channel, payload, pattern: nil)
|
|
29
|
+
channel = channel.b
|
|
30
|
+
payload = payload.b
|
|
31
|
+
match = HEADER.match(channel)
|
|
32
|
+
return nil unless match
|
|
33
|
+
|
|
34
|
+
family = match[1].to_sym
|
|
35
|
+
db = match[2] == "*" ? "*" : match[2].to_i
|
|
36
|
+
rest = channel.byteslice(match[0].bytesize, channel.bytesize)
|
|
37
|
+
|
|
38
|
+
event, key, subkeys =
|
|
39
|
+
case family
|
|
40
|
+
when :keyspace then parse_keyspace(rest, payload, channel)
|
|
41
|
+
when :keyevent then parse_keyevent(rest, payload, channel)
|
|
42
|
+
when :subkeyspace then parse_subkeyspace(rest, payload, channel)
|
|
43
|
+
when :subkeyevent then parse_subkeyevent(rest, payload, channel)
|
|
44
|
+
when :subkeyspaceitem then parse_subkeyspaceitem(rest, payload, channel)
|
|
45
|
+
when :subkeyspaceevent then parse_subkeyspaceevent(rest, payload, channel)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Redis never emits an empty event name; one here means garbage was
|
|
49
|
+
# published on a notification channel, which must surface as the
|
|
50
|
+
# documented ParseError, not as a Notification violating the contract.
|
|
51
|
+
# (Empty KEYS are legal — "" is a valid Redis key — as are empty subkeys.)
|
|
52
|
+
raise error("empty event name", channel, payload) if event.empty?
|
|
53
|
+
|
|
54
|
+
Notification.new(
|
|
55
|
+
family: family,
|
|
56
|
+
db: db,
|
|
57
|
+
event: event,
|
|
58
|
+
key: key,
|
|
59
|
+
subkeys: subkeys,
|
|
60
|
+
channel: channel,
|
|
61
|
+
payload: payload,
|
|
62
|
+
pattern: pattern
|
|
63
|
+
)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# @api private
|
|
67
|
+
def parse_keyspace(rest, payload, _channel)
|
|
68
|
+
[payload, rest, []]
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# @api private
|
|
72
|
+
def parse_keyevent(rest, payload, _channel)
|
|
73
|
+
[rest, payload, []]
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# @api private
|
|
77
|
+
# Payload: "<event>|<len>:<subkey>[,<len>:<subkey>...]" — events never contain "|",
|
|
78
|
+
# so the first "|" is the delimiter; everything after is length-prefixed.
|
|
79
|
+
def parse_subkeyspace(rest, payload, channel)
|
|
80
|
+
bar = payload.index("|")
|
|
81
|
+
raise error("missing \"|\" between event and subkeys", channel, payload) if bar.nil?
|
|
82
|
+
|
|
83
|
+
event = payload.byteslice(0, bar)
|
|
84
|
+
subkeys = parse_subkey_list(payload, bar + 1, channel)
|
|
85
|
+
[event, rest, subkeys]
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# @api private
|
|
89
|
+
# Payload: "<key_len>:<key>|<len>:<subkey>[,...]" — the key is read by length,
|
|
90
|
+
# never by delimiter, because keys may contain "|".
|
|
91
|
+
def parse_subkeyevent(rest, payload, channel)
|
|
92
|
+
key, pos = read_length_prefixed(payload, 0, channel)
|
|
93
|
+
raise error("missing \"|\" after key", channel, payload) unless payload.getbyte(pos) == PIPE
|
|
94
|
+
|
|
95
|
+
subkeys = parse_subkey_list(payload, pos + 1, channel)
|
|
96
|
+
[rest, key, subkeys]
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# @api private
|
|
100
|
+
# Channel suffix: "<key>\n<subkey>" — the server only emits this family for keys
|
|
101
|
+
# without "\n", so the first "\n" is the delimiter (the subkey may contain "\n").
|
|
102
|
+
def parse_subkeyspaceitem(rest, payload, channel)
|
|
103
|
+
newline = rest.index("\n")
|
|
104
|
+
raise error("missing \"\\n\" between key and subkey", channel, payload) if newline.nil?
|
|
105
|
+
|
|
106
|
+
key = rest.byteslice(0, newline)
|
|
107
|
+
subkey = rest.byteslice(newline + 1, rest.bytesize)
|
|
108
|
+
[payload, key, [subkey]]
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
# @api private
|
|
112
|
+
# Channel suffix: "<event>|<key>" — events never contain "|", so the first "|"
|
|
113
|
+
# is the delimiter (the key may contain further "|" bytes).
|
|
114
|
+
def parse_subkeyspaceevent(rest, payload, channel)
|
|
115
|
+
bar = rest.index("|")
|
|
116
|
+
raise error("missing \"|\" between event and key", channel, payload) if bar.nil?
|
|
117
|
+
|
|
118
|
+
event = rest.byteslice(0, bar)
|
|
119
|
+
key = rest.byteslice(bar + 1, rest.bytesize)
|
|
120
|
+
subkeys = parse_subkey_list(payload, 0, channel)
|
|
121
|
+
[event, key, subkeys]
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# @api private
|
|
125
|
+
# Reads "<len>:<bytes>" at byte offset +pos+ of +buf+. Returns [value, next_pos].
|
|
126
|
+
# The value is read by length, so its content bytes are inert.
|
|
127
|
+
def read_length_prefixed(buf, pos, channel)
|
|
128
|
+
colon = buf.index(":", pos)
|
|
129
|
+
raise error("missing length prefix at byte #{pos}", channel, buf) if colon.nil? || colon == pos
|
|
130
|
+
|
|
131
|
+
len_str = buf.byteslice(pos, colon - pos)
|
|
132
|
+
raise error("invalid length #{len_str.inspect} at byte #{pos}", channel, buf) unless /\A\d+\z/.match?(len_str)
|
|
133
|
+
|
|
134
|
+
len = len_str.to_i
|
|
135
|
+
# Bounds-check before byteslice: a length exceeding the remaining bytes is
|
|
136
|
+
# malformed input (and an absurdly large one would otherwise surface as
|
|
137
|
+
# RangeError from byteslice instead of the documented ParseError).
|
|
138
|
+
raise error("truncated value at byte #{colon + 1}", channel, buf) if len > buf.bytesize - (colon + 1)
|
|
139
|
+
|
|
140
|
+
[buf.byteslice(colon + 1, len), colon + 1 + len]
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
# @api private
|
|
144
|
+
# Reads "<len>:<subkey>[,<len>:<subkey>...]" from +pos+ to the end of +buf+.
|
|
145
|
+
# Returns an ordered Array preserving duplicates. The grammar requires at
|
|
146
|
+
# least one entry — an empty subkey is "0:", never an absent list — so a
|
|
147
|
+
# payload ending right at +pos+ is malformed, not an empty list.
|
|
148
|
+
def parse_subkey_list(buf, pos, channel)
|
|
149
|
+
subkeys = []
|
|
150
|
+
size = buf.bytesize
|
|
151
|
+
raise error("missing subkey list at byte #{pos}", channel, buf) if pos >= size
|
|
152
|
+
|
|
153
|
+
loop do
|
|
154
|
+
value, pos = read_length_prefixed(buf, pos, channel)
|
|
155
|
+
subkeys << value
|
|
156
|
+
break if pos == size
|
|
157
|
+
raise error("expected \",\" at byte #{pos}", channel, buf) unless buf.getbyte(pos) == COMMA
|
|
158
|
+
|
|
159
|
+
pos += 1
|
|
160
|
+
end
|
|
161
|
+
subkeys
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
# @api private
|
|
165
|
+
def error(message, channel, payload)
|
|
166
|
+
ParseError.new("malformed keyspace notification: #{message}", channel: channel, payload: payload)
|
|
167
|
+
end
|
|
168
|
+
end
|
|
169
|
+
end
|
|
170
|
+
end
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Redis
|
|
4
|
+
# Keyspace, keyevent and (Redis 8.8+) subkey notification support, layered on pub/sub.
|
|
5
|
+
#
|
|
6
|
+
# Layer 1 — {Channels} builders and {Parser} are usable directly inside the existing
|
|
7
|
+
# subscribe/psubscribe DSL. Layer 2 — {Manager} (built via {Redis#keyspace_notifications})
|
|
8
|
+
# owns a dedicated connection and background thread and dispatches parsed {Notification}
|
|
9
|
+
# objects to handlers.
|
|
10
|
+
#
|
|
11
|
+
# Notifications must be enabled on the server via the `notify-keyspace-events`
|
|
12
|
+
# configuration (a server-side, per-node setting — this library deliberately does not
|
|
13
|
+
# set it for you). Notification channels are emitted by the server; do not PUBLISH to
|
|
14
|
+
# them manually. See specs/keyspace-notifications/user-guide.md.
|
|
15
|
+
module KeyspaceNotifications
|
|
16
|
+
# Raised when a message on a notification channel cannot be decoded.
|
|
17
|
+
class ParseError < BaseError
|
|
18
|
+
# @return [String, nil] the raw channel the message arrived on
|
|
19
|
+
# @return [String, nil] the raw message payload
|
|
20
|
+
attr_reader :channel, :payload
|
|
21
|
+
|
|
22
|
+
# @param message [String] the error message
|
|
23
|
+
# @param channel [String, nil] the raw channel
|
|
24
|
+
# @param payload [String, nil] the raw payload
|
|
25
|
+
def initialize(message, channel: nil, payload: nil)
|
|
26
|
+
super(message)
|
|
27
|
+
@channel = channel
|
|
28
|
+
@payload = payload
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# Build a keyspace-notification manager. The manager owns a dedicated connection
|
|
34
|
+
# (duplicated from this client's options) and a background listener thread; the
|
|
35
|
+
# calling client remains fully usable for commands.
|
|
36
|
+
#
|
|
37
|
+
# @param error_handler [#call, nil] receives every background error (parse errors,
|
|
38
|
+
# handler exceptions, connection loss); defaults to warning on $stderr
|
|
39
|
+
# @param reconnect_attempts [Integer, Array<Integer, Float>] number of attempts trying
|
|
40
|
+
# to reconnect and re-subscribe after losing the connection (with no sleep in
|
|
41
|
+
# between), or a list of sleep durations between attempts — the same semantics as
|
|
42
|
+
# the `reconnect_attempts` client option. Defaults to an exponential
|
|
43
|
+
# 0.5s → 30s ladder of 10 attempts; the budget resets after every healthy session
|
|
44
|
+
# @return [KeyspaceNotifications::Manager]
|
|
45
|
+
def keyspace_notifications(error_handler: nil, reconnect_attempts: KeyspaceNotifications::Manager::DEFAULT_RECONNECT_ATTEMPTS)
|
|
46
|
+
# The manager is the sole owner of reconnection timing: transport-level
|
|
47
|
+
# retries are disabled on the owned connection, or the source client's own
|
|
48
|
+
# ladder would run inside every one of the manager's connect attempts.
|
|
49
|
+
KeyspaceNotifications::Manager.new(
|
|
50
|
+
redis: dup(reconnect_attempts: 0),
|
|
51
|
+
error_handler: error_handler, reconnect_attempts: reconnect_attempts
|
|
52
|
+
)
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
require "redis/keyspace_notifications/channels"
|
|
57
|
+
require "redis/keyspace_notifications/notification"
|
|
58
|
+
require "redis/keyspace_notifications/parser"
|
|
59
|
+
require "redis/keyspace_notifications/manager"
|
data/lib/redis/pipeline.rb
CHANGED
|
@@ -6,8 +6,9 @@ class Redis
|
|
|
6
6
|
class PipelinedConnection
|
|
7
7
|
attr_accessor :db
|
|
8
8
|
|
|
9
|
-
def initialize(pipeline, futures = [], exception: true)
|
|
9
|
+
def initialize(pipeline, futures = [], client: pipeline, exception: true)
|
|
10
10
|
@pipeline = pipeline
|
|
11
|
+
@client = client
|
|
11
12
|
@futures = futures
|
|
12
13
|
@exception = exception
|
|
13
14
|
end
|
|
@@ -19,7 +20,7 @@ class Redis
|
|
|
19
20
|
end
|
|
20
21
|
|
|
21
22
|
def multi
|
|
22
|
-
transaction = MultiConnection.new(@pipeline, @futures)
|
|
23
|
+
transaction = MultiConnection.new(@pipeline, @futures, client: @client, exception: @exception)
|
|
23
24
|
send_command([:multi])
|
|
24
25
|
size = @futures.size
|
|
25
26
|
yield transaction
|
|
@@ -75,10 +76,14 @@ class Redis
|
|
|
75
76
|
def initialize
|
|
76
77
|
super("Value will be available once the pipeline executes.")
|
|
77
78
|
end
|
|
79
|
+
|
|
80
|
+
def exception(*)
|
|
81
|
+
frozen? ? dup : super
|
|
82
|
+
end
|
|
78
83
|
end
|
|
79
84
|
|
|
80
85
|
class Future < BasicObject
|
|
81
|
-
FutureNotReady = ::Redis::FutureNotReady.new
|
|
86
|
+
FutureNotReady = ::Redis::FutureNotReady.new.freeze
|
|
82
87
|
|
|
83
88
|
def initialize(command, coerce, exception)
|
|
84
89
|
@command = command
|
|
@@ -92,10 +97,14 @@ class Redis
|
|
|
92
97
|
end
|
|
93
98
|
|
|
94
99
|
def _set(object)
|
|
95
|
-
@object = @coerce ? @coerce.call(object) : object
|
|
100
|
+
@object = @coerce && !object.is_a?(::StandardError) ? @coerce.call(object) : object
|
|
96
101
|
value
|
|
97
102
|
end
|
|
98
103
|
|
|
104
|
+
def _set_unless_error(object)
|
|
105
|
+
_set(object) unless @object.is_a?(::StandardError)
|
|
106
|
+
end
|
|
107
|
+
|
|
99
108
|
def value
|
|
100
109
|
::Kernel.raise(@object) if @exception && @object.is_a?(::StandardError)
|
|
101
110
|
@object
|
|
@@ -118,11 +127,14 @@ class Redis
|
|
|
118
127
|
end
|
|
119
128
|
|
|
120
129
|
def _set(replies)
|
|
121
|
-
@object = if replies
|
|
130
|
+
@object = if replies && !replies.is_a?(::StandardError)
|
|
122
131
|
@futures.map.with_index do |future, index|
|
|
123
132
|
future._set(replies[index])
|
|
124
133
|
future.value
|
|
125
134
|
end
|
|
135
|
+
elsif replies.is_a?(::StandardError)
|
|
136
|
+
@futures.each { |future| future._set_unless_error(replies) }
|
|
137
|
+
replies
|
|
126
138
|
else
|
|
127
139
|
replies
|
|
128
140
|
end
|
data/lib/redis/subscribe.rb
CHANGED
|
@@ -2,15 +2,36 @@
|
|
|
2
2
|
|
|
3
3
|
class Redis
|
|
4
4
|
class SubscribedClient
|
|
5
|
+
# Upper bound on each guarded read (see #next_event) — and therefore on how
|
|
6
|
+
# long a cross-thread write can wait for the monitor.
|
|
7
|
+
READ_SLICE = 0.05
|
|
8
|
+
private_constant :READ_SLICE
|
|
9
|
+
|
|
5
10
|
def initialize(client)
|
|
6
11
|
@client = client
|
|
7
12
|
@write_monitor = Monitor.new
|
|
13
|
+
# Count of writers waiting for (or holding) the monitor; mutations under
|
|
14
|
+
# their own lock so it cannot drift. The read loop deschedules while
|
|
15
|
+
# writers are queued — MRI mutexes barge, and on MRI 3.2 a writer can
|
|
16
|
+
# otherwise starve behind consecutive read slices (Thread.pass is not a
|
|
17
|
+
# handoff there).
|
|
18
|
+
@pending_writes = 0
|
|
19
|
+
@pending_writes_lock = Mutex.new
|
|
20
|
+
@closed = false
|
|
8
21
|
end
|
|
9
22
|
|
|
10
23
|
def call_v(command)
|
|
24
|
+
@pending_writes_lock.synchronize { @pending_writes += 1 }
|
|
11
25
|
@write_monitor.synchronize do
|
|
26
|
+
# A write must never reach a connection whose teardown is freeing it:
|
|
27
|
+
# on the hiredis driver that is a native use-after-free (SIGSEGV), not
|
|
28
|
+
# a rescuable exception.
|
|
29
|
+
raise SubscriptionError, "This client is closed" if @closed
|
|
30
|
+
|
|
12
31
|
@client.call_v(command)
|
|
13
32
|
end
|
|
33
|
+
ensure
|
|
34
|
+
@pending_writes_lock.synchronize { @pending_writes -= 1 }
|
|
14
35
|
end
|
|
15
36
|
|
|
16
37
|
def subscribe(*channels, &block)
|
|
@@ -50,7 +71,15 @@ class Redis
|
|
|
50
71
|
end
|
|
51
72
|
|
|
52
73
|
def close
|
|
53
|
-
|
|
74
|
+
# Serialized with every write and guarded read: freeing the connection
|
|
75
|
+
# under either crashes the process on the hiredis driver.
|
|
76
|
+
@pending_writes_lock.synchronize { @pending_writes += 1 }
|
|
77
|
+
@write_monitor.synchronize do
|
|
78
|
+
@closed = true
|
|
79
|
+
@client.close
|
|
80
|
+
end
|
|
81
|
+
ensure
|
|
82
|
+
@pending_writes_lock.synchronize { @pending_writes -= 1 }
|
|
54
83
|
end
|
|
55
84
|
|
|
56
85
|
protected
|
|
@@ -63,7 +92,7 @@ class Redis
|
|
|
63
92
|
else call_v([start, *channels])
|
|
64
93
|
end
|
|
65
94
|
|
|
66
|
-
while event =
|
|
95
|
+
while event = next_event(timeout)
|
|
67
96
|
if event.is_a?(::RedisClient::CommandError)
|
|
68
97
|
raise Client::ERROR_MAPPING.fetch(event.class), event.message
|
|
69
98
|
end
|
|
@@ -77,6 +106,55 @@ class Redis
|
|
|
77
106
|
# No need to unsubscribe here. The real client closes the connection
|
|
78
107
|
# whenever an exception is raised (see #ensure_connected).
|
|
79
108
|
end
|
|
109
|
+
|
|
110
|
+
# Reads the next subscription event in short monitor-guarded slices rather
|
|
111
|
+
# than one indefinite read: on a connection error the driver's read path
|
|
112
|
+
# disconnects the connection object in place (hiredis nulls its native
|
|
113
|
+
# context), so an unguarded read racing a cross-thread write or #close is a
|
|
114
|
+
# native use-after-free. Slicing under the writers' monitor means reads,
|
|
115
|
+
# writes and close never overlap; a writer waits at most one slice.
|
|
116
|
+
# Blocking semantics are preserved (a slice timeout just reads again); the
|
|
117
|
+
# timed form keeps the caller's overall deadline, nil meaning "no event
|
|
118
|
+
# within the requested timeout".
|
|
119
|
+
def next_event(timeout)
|
|
120
|
+
if timeout > 0
|
|
121
|
+
deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout
|
|
122
|
+
loop do
|
|
123
|
+
writer_priority_wait
|
|
124
|
+
remaining = deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
125
|
+
return nil if remaining <= 0
|
|
126
|
+
|
|
127
|
+
event = guarded_read([remaining, READ_SLICE].min)
|
|
128
|
+
return event if event
|
|
129
|
+
end
|
|
130
|
+
else
|
|
131
|
+
loop do
|
|
132
|
+
writer_priority_wait
|
|
133
|
+
event = guarded_read(READ_SLICE)
|
|
134
|
+
return event if event
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
def guarded_read(slice)
|
|
140
|
+
@write_monitor.synchronize do
|
|
141
|
+
raise SubscriptionError, "This client is closed" if @closed
|
|
142
|
+
|
|
143
|
+
@client.next_event(slice)
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
# Bounded writer priority: a queued writer gets the monitor before the next
|
|
148
|
+
# read slice. sleep deterministically deschedules this thread (Thread.pass
|
|
149
|
+
# is a hint MRI 3.2 ignores in favor of the barging re-acquirer); the bound
|
|
150
|
+
# means a counter defect could only throttle reads, never stop them.
|
|
151
|
+
def writer_priority_wait
|
|
152
|
+
50.times do
|
|
153
|
+
break if @pending_writes.zero?
|
|
154
|
+
|
|
155
|
+
sleep 0.001
|
|
156
|
+
end
|
|
157
|
+
end
|
|
80
158
|
end
|
|
81
159
|
|
|
82
160
|
class Subscription
|
data/lib/redis/version.rb
CHANGED