dalli 5.1.8 → 5.2.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 +35 -110
- data/README.md +20 -21
- data/lib/dalli/cache_result.rb +65 -0
- data/lib/dalli/client.rb +87 -58
- data/lib/dalli/compressor.rb +5 -53
- data/lib/dalli/key_manager.rb +2 -23
- data/lib/dalli/options.rb +1 -1
- data/lib/dalli/pipelined_getter.rb +23 -36
- data/lib/dalli/pipelined_setter.rb +8 -17
- data/lib/dalli/protocol/base.rb +25 -134
- data/lib/dalli/protocol/connection_manager.rb +12 -110
- data/lib/dalli/protocol/meta.rb +62 -115
- data/lib/dalli/protocol/request_formatter.rb +30 -34
- data/lib/dalli/protocol/response_buffer.rb +1 -4
- data/lib/dalli/protocol/response_processor.rb +102 -60
- data/lib/dalli/protocol/value_compressor.rb +7 -36
- data/lib/dalli/protocol/value_serializer.rb +1 -1
- data/lib/dalli/protocol.rb +9 -7
- data/lib/dalli/ring.rb +44 -17
- data/lib/dalli/socket.rb +27 -31
- data/lib/dalli/version.rb +1 -1
- data/lib/dalli.rb +1 -5
- metadata +3 -3
- data/lib/dalli/json_serializer.rb +0 -28
|
@@ -63,6 +63,19 @@ module Dalli
|
|
|
63
63
|
skip_flags ? "mg #{encoded_key(key)} v#{TERMINATOR}" : "mg #{encoded_key(key)} v f#{TERMINATOR}"
|
|
64
64
|
end
|
|
65
65
|
|
|
66
|
+
# Fast path for a plain set: mode :set, not quiet, no CAS and no routing
|
|
67
|
+
# tokens. Produces the same bytes as meta_set(key:, value:, bitflags:, ttl:)
|
|
68
|
+
# without the keyword-argument handling and incremental string building.
|
|
69
|
+
def plain_meta_set(key, value_bytesize, bitflags, ttl)
|
|
70
|
+
flags = bitflags ? " F#{bitflags}" : ''
|
|
71
|
+
flags += " T#{integer_flag(:ttl, ttl)}" if ttl
|
|
72
|
+
if KeyRegularizer.required?(key)
|
|
73
|
+
"ms #{KeyRegularizer.encode(key)} #{value_bytesize} c b#{flags} MS#{TERMINATOR}"
|
|
74
|
+
else
|
|
75
|
+
"ms #{key} #{value_bytesize} c#{flags} MS#{TERMINATOR}"
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
66
79
|
# Pass terminate: false to leave off the trailing noop, for callers
|
|
67
80
|
# (the pipelined get) that send it separately.
|
|
68
81
|
def multi_meta_get(keys, skip_flags: false, return_cas: false, terminate: true, p_token: nil, l_token: nil)
|
|
@@ -107,9 +120,8 @@ module Dalli
|
|
|
107
120
|
|
|
108
121
|
def multi_meta_set(entries, ttl: nil, p_token: nil, l_token: nil)
|
|
109
122
|
# Routing tokens apply to every entry in the batch, so the suffix is
|
|
110
|
-
# built once rather than per entry.
|
|
111
|
-
|
|
112
|
-
token_suffix = routing_tokens(p_token: p_token, l_token: l_token).b
|
|
123
|
+
# built once rather than per entry.
|
|
124
|
+
token_suffix = routing_tokens(p_token: p_token, l_token: l_token)
|
|
113
125
|
ttl = integer_flag(:ttl, ttl) if ttl
|
|
114
126
|
|
|
115
127
|
buffer = ''.b
|
|
@@ -124,29 +136,11 @@ module Dalli
|
|
|
124
136
|
buffer << ' b' if base64
|
|
125
137
|
buffer << " F#{bitflags}" if bitflags
|
|
126
138
|
buffer << " T#{ttl}" if ttl
|
|
127
|
-
buffer << ' MS q' << token_suffix << TERMINATOR
|
|
128
|
-
append_bytes(buffer, value)
|
|
129
|
-
buffer << TERMINATOR
|
|
139
|
+
buffer << ' MS q' << token_suffix << TERMINATOR << value << TERMINATOR
|
|
130
140
|
end
|
|
131
141
|
buffer << META_NOOP
|
|
132
142
|
end
|
|
133
143
|
|
|
134
|
-
# Values are appended as bytes. One batch can hold UTF-8 and binary
|
|
135
|
-
# values, which String#<< refuses to combine, and a raw value can be in
|
|
136
|
-
# an encoding that isn't ASCII-compatible, such as UTF-16.
|
|
137
|
-
# Don't chain on the result: TruffleRuby's append_as_bytes returns its
|
|
138
|
-
# arguments rather than the buffer.
|
|
139
|
-
if String.method_defined?(:append_as_bytes)
|
|
140
|
-
def append_bytes(buffer, value)
|
|
141
|
-
buffer.append_as_bytes(value)
|
|
142
|
-
end
|
|
143
|
-
else
|
|
144
|
-
def append_bytes(buffer, value)
|
|
145
|
-
buffer << (value.encoding == Encoding::BINARY ? value : value.b)
|
|
146
|
-
end
|
|
147
|
-
end
|
|
148
|
-
private :append_bytes
|
|
149
|
-
|
|
150
144
|
# Thundering herd protection flag:
|
|
151
145
|
# - stale (I flag): Instead of deleting the item, mark it as stale. Other clients
|
|
152
146
|
# using N/R flags will see the X flag and know the item is being regenerated.
|
|
@@ -177,6 +171,11 @@ module Dalli
|
|
|
177
171
|
cmd << TERMINATOR
|
|
178
172
|
end
|
|
179
173
|
|
|
174
|
+
# Same bytes as meta_delete(key: key), without the keyword-argument handling
|
|
175
|
+
def plain_meta_delete(key)
|
|
176
|
+
"md #{encoded_key(key)}#{TERMINATOR}"
|
|
177
|
+
end
|
|
178
|
+
|
|
180
179
|
# Tombstone and routing-token flags apply to every key in the batch;
|
|
181
180
|
# see meta_delete.
|
|
182
181
|
def multi_meta_delete(keys, stale: false, ttl: nil, drop_value: false, p_token: nil, l_token: nil)
|
|
@@ -219,11 +218,11 @@ module Dalli
|
|
|
219
218
|
# specifications to a proxy or router inbetween a client and a
|
|
220
219
|
# memcached daemon."
|
|
221
220
|
#
|
|
222
|
-
# Empty / nil tokens are treated as no-ops.
|
|
223
|
-
#
|
|
224
|
-
#
|
|
225
|
-
#
|
|
226
|
-
#
|
|
221
|
+
# Empty / nil tokens are treated as no-ops. CRLF and null bytes are
|
|
222
|
+
# rejected with ArgumentError to prevent the token from being used as a
|
|
223
|
+
# wire-protocol injection vector (e.g. "foo\r\nflush_all\r\n" would
|
|
224
|
+
# otherwise be parsed as a second command by memcached or any
|
|
225
|
+
# intermediate proxy/LB).
|
|
227
226
|
def routing_tokens(p_token: nil, l_token: nil)
|
|
228
227
|
# Only an empty *String* is a no-op. Checking respond_to?(:empty?)
|
|
229
228
|
# instead would also swallow p_token: [] / {} before the type check
|
|
@@ -280,18 +279,15 @@ module Dalli
|
|
|
280
279
|
|
|
281
280
|
private
|
|
282
281
|
|
|
283
|
-
# Disallowed bytes:
|
|
284
|
-
# would let
|
|
285
|
-
|
|
286
|
-
ROUTING_TOKEN_FORBIDDEN = /[\x00-\x20\x7F]/
|
|
282
|
+
# Disallowed bytes: CR, LF, NUL. Any of these embedded in a routing
|
|
283
|
+
# token would let the caller inject a second wire-protocol command.
|
|
284
|
+
ROUTING_TOKEN_FORBIDDEN = /[\r\n\0]/
|
|
287
285
|
private_constant :ROUTING_TOKEN_FORBIDDEN
|
|
288
286
|
|
|
289
287
|
def validate_routing_token!(name, value)
|
|
290
288
|
return if value.nil?
|
|
291
289
|
raise ArgumentError, "#{name} must be a String, got #{value.class}" unless value.is_a?(String)
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
raise ArgumentError, "#{name} must not contain whitespace or control characters"
|
|
290
|
+
raise ArgumentError, "#{name} must not contain CRLF or null bytes" if value.match?(ROUTING_TOKEN_FORBIDDEN)
|
|
295
291
|
end
|
|
296
292
|
|
|
297
293
|
# Numeric flag values are written straight into the command line, so a
|
|
@@ -15,9 +15,6 @@ module Dalli
|
|
|
15
15
|
# when advancing through parsed responses.
|
|
16
16
|
##
|
|
17
17
|
class ResponseBuffer
|
|
18
|
-
# Whether the current pipeline was requested with raw: true
|
|
19
|
-
attr_writer :raw
|
|
20
|
-
|
|
21
18
|
def initialize(io_source, response_processor)
|
|
22
19
|
@io_source = io_source
|
|
23
20
|
@response_processor = response_processor
|
|
@@ -42,7 +39,7 @@ module Dalli
|
|
|
42
39
|
# Attempts to process a single response from the buffer,
|
|
43
40
|
# advancing the offset past the consumed bytes.
|
|
44
41
|
def process_single_getk_response
|
|
45
|
-
response = @response_processor.getk_response_from_buffer(@buffer, @offset
|
|
42
|
+
response = @response_processor.getk_response_from_buffer(@buffer, @offset)
|
|
46
43
|
@offset += response.pop
|
|
47
44
|
response
|
|
48
45
|
end
|
|
@@ -24,9 +24,10 @@ module Dalli
|
|
|
24
24
|
SERVER_ERROR = 'SERVER_ERROR'
|
|
25
25
|
|
|
26
26
|
VA_PREFIX = 'VA '
|
|
27
|
-
|
|
28
|
-
MAX_VALUE_BYTES = 1024 * 1024 * 1024
|
|
27
|
+
HD_PREFIX = 'HD '
|
|
29
28
|
FLAGS_TOKEN_PREFIX = ' f'
|
|
29
|
+
CAS_TOKEN_PREFIX = ' c'
|
|
30
|
+
KEY_TOKEN_PREFIX = ' k'
|
|
30
31
|
BYTE_B = 'b'.ord
|
|
31
32
|
BYTE_C = 'c'.ord
|
|
32
33
|
BYTE_F = 'f'.ord
|
|
@@ -48,15 +49,12 @@ module Dalli
|
|
|
48
49
|
@value_marshaller = value_marshaller
|
|
49
50
|
end
|
|
50
51
|
|
|
51
|
-
|
|
52
|
-
# ignoring any flags in the reply. The request didn't ask for flags, so
|
|
53
|
-
# a reply carrying them anyway must not get the value deserialized.
|
|
54
|
-
def meta_get_with_value(cache_nils: false, raw: false)
|
|
52
|
+
def meta_get_with_value(cache_nils: false)
|
|
55
53
|
line = read_line
|
|
56
54
|
# A hit ("VA <size> f<flags>") is parsed in place rather than split
|
|
57
55
|
# into tokens, which saves several allocations on the hottest path.
|
|
58
56
|
if line&.start_with?(VA_PREFIX)
|
|
59
|
-
return retrieve(read_data(size_from_va_line(line)),
|
|
57
|
+
return @value_marshaller.retrieve(read_data(size_from_va_line(line)), bitflags_from_va_line(line))
|
|
60
58
|
end
|
|
61
59
|
|
|
62
60
|
tokens = line&.split || []
|
|
@@ -64,7 +62,7 @@ module Dalli
|
|
|
64
62
|
when EN
|
|
65
63
|
cache_nils ? ::Dalli::NOT_FOUND : nil
|
|
66
64
|
when VA # only reached for an unusually formatted hit line
|
|
67
|
-
retrieve(read_data(tokens[1].to_i),
|
|
65
|
+
@value_marshaller.retrieve(read_data(tokens[1].to_i), bitflags_from_tokens(tokens))
|
|
68
66
|
when HD
|
|
69
67
|
true
|
|
70
68
|
else
|
|
@@ -72,14 +70,14 @@ module Dalli
|
|
|
72
70
|
end
|
|
73
71
|
end
|
|
74
72
|
|
|
75
|
-
def meta_get_with_value_and_cas
|
|
73
|
+
def meta_get_with_value_and_cas
|
|
76
74
|
tokens = error_on_unexpected!(T_VA_EN_HD)
|
|
77
75
|
return [nil, 0] if tokens.first == EN
|
|
78
76
|
|
|
79
77
|
cas = cas_from_tokens(tokens)
|
|
80
78
|
return [nil, cas] unless tokens.first == VA
|
|
81
79
|
|
|
82
|
-
[retrieve(read_data(tokens[1].to_i),
|
|
80
|
+
[@value_marshaller.retrieve(read_data(tokens[1].to_i), bitflags_from_tokens(tokens)), cas]
|
|
83
81
|
end
|
|
84
82
|
|
|
85
83
|
def meta_get_without_value
|
|
@@ -99,13 +97,13 @@ module Dalli
|
|
|
99
97
|
# Used by meta_get for comprehensive metadata retrieval.
|
|
100
98
|
# Supports thundering herd protection (N/R flags) and metadata flags (h/l/u).
|
|
101
99
|
def meta_get_with_metadata(cache_nils: false, return_hit_status: false, return_last_access: false,
|
|
102
|
-
return_ttl_remaining: false
|
|
100
|
+
return_ttl_remaining: false)
|
|
103
101
|
tokens = error_on_unexpected!(T_VA_EN_HD)
|
|
104
102
|
result = build_metadata_result(tokens)
|
|
105
103
|
result[:hit_before] = hit_status_from_tokens(tokens) if return_hit_status
|
|
106
104
|
result[:last_access] = last_access_from_tokens(tokens) if return_last_access
|
|
107
105
|
result[:ttl_remaining] = ttl_remaining_from_tokens(tokens) if return_ttl_remaining
|
|
108
|
-
result[:value] = parse_value_from_tokens(tokens, cache_nils
|
|
106
|
+
result[:value] = parse_value_from_tokens(tokens, cache_nils)
|
|
109
107
|
result
|
|
110
108
|
end
|
|
111
109
|
|
|
@@ -122,15 +120,21 @@ module Dalli
|
|
|
122
120
|
}
|
|
123
121
|
end
|
|
124
122
|
|
|
125
|
-
def parse_value_from_tokens(tokens, cache_nils
|
|
123
|
+
def parse_value_from_tokens(tokens, cache_nils)
|
|
126
124
|
return cache_nils ? ::Dalli::NOT_FOUND : nil if tokens.first == EN
|
|
127
125
|
return unless tokens.first == VA
|
|
128
126
|
|
|
129
|
-
retrieve(read_data(tokens[1].to_i),
|
|
127
|
+
@value_marshaller.retrieve(read_data(tokens[1].to_i), bitflags_from_tokens(tokens))
|
|
130
128
|
end
|
|
131
129
|
|
|
132
130
|
def meta_set_with_cas
|
|
133
|
-
|
|
131
|
+
line = read_line
|
|
132
|
+
# A stored item ("HD c<cas>") is parsed in place rather than split into tokens
|
|
133
|
+
# Search from just after "HD": the " c" token starts at the space that follows it
|
|
134
|
+
return flag_from_line(line, CAS_TOKEN_PREFIX, HD.bytesize) if line&.start_with?(HD_PREFIX)
|
|
135
|
+
|
|
136
|
+
tokens = line&.split || []
|
|
137
|
+
raise_unexpected!(tokens) unless T_HD_NS_NF_EX.include?(tokens.first)
|
|
134
138
|
return false unless tokens.first == HD
|
|
135
139
|
|
|
136
140
|
cas_from_tokens(tokens)
|
|
@@ -144,7 +148,13 @@ module Dalli
|
|
|
144
148
|
end
|
|
145
149
|
|
|
146
150
|
def meta_delete
|
|
147
|
-
|
|
151
|
+
line = read_line
|
|
152
|
+
# The two common replies, compared as whole lines instead of split into tokens
|
|
153
|
+
return true if line == HD
|
|
154
|
+
return false if line == NF
|
|
155
|
+
|
|
156
|
+
tokens = line&.split || []
|
|
157
|
+
raise_unexpected!(tokens) unless T_HD_NF_EX.include?(tokens.first)
|
|
148
158
|
tokens.first == HD
|
|
149
159
|
end
|
|
150
160
|
|
|
@@ -217,13 +227,16 @@ module Dalli
|
|
|
217
227
|
# The remaining three values in the array are the ResponseHeader,
|
|
218
228
|
# key, and value.
|
|
219
229
|
##
|
|
220
|
-
|
|
221
|
-
# see meta_get_with_value.
|
|
222
|
-
def getk_response_from_buffer(buf, offset = 0, raw: false)
|
|
230
|
+
def getk_response_from_buffer(buf, offset = 0)
|
|
223
231
|
# Find the header terminator starting from offset
|
|
224
232
|
term_idx = buf.byteindex(TERMINATOR, offset)
|
|
225
233
|
return [0] unless term_idx
|
|
226
234
|
|
|
235
|
+
if buf.byteslice(offset, VA_PREFIX.bytesize) == VA_PREFIX
|
|
236
|
+
response = va_response_from_buffer(buf, offset, term_idx)
|
|
237
|
+
return response if response
|
|
238
|
+
end
|
|
239
|
+
|
|
227
240
|
header = buf.byteslice(offset, term_idx - offset)
|
|
228
241
|
tokens = header.split
|
|
229
242
|
header_len = header.bytesize + TERMINATOR.length
|
|
@@ -241,20 +254,16 @@ module Dalli
|
|
|
241
254
|
when BYTE_B then base64 ||= token == 'b'
|
|
242
255
|
end
|
|
243
256
|
end
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
#
|
|
249
|
-
#
|
|
250
|
-
#
|
|
251
|
-
#
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
# still has a body -- just its terminator -- so it's parsed below.
|
|
255
|
-
return [tokens.first == MN, header_len] if body_len.zero? && tokens.first != VA
|
|
256
|
-
|
|
257
|
-
check_value_size!(body_len)
|
|
257
|
+
body_len = flag_int(size, 's')
|
|
258
|
+
|
|
259
|
+
# We have a complete response that has no body.
|
|
260
|
+
# This is either the response to the terminating
|
|
261
|
+
# noop or, if the status is not MN, an intermediate
|
|
262
|
+
# error response that needs to be discarded. A hit
|
|
263
|
+
# on an empty value (VA 0) still has a body -- just
|
|
264
|
+
# its terminator -- so it's parsed below as a value.
|
|
265
|
+
return [true, header_len] if body_len.zero? && tokens.first != VA
|
|
266
|
+
|
|
258
267
|
resp_size = header_len + body_len + TERMINATOR.length
|
|
259
268
|
# The header is in the buffer, but the body is not. As we don't have
|
|
260
269
|
# a complete response, don't advance the buffer
|
|
@@ -263,19 +272,47 @@ module Dalli
|
|
|
263
272
|
# The full response is in our buffer, so parse it and return
|
|
264
273
|
# the values
|
|
265
274
|
body = buf.byteslice(offset + header_len, body_len)
|
|
266
|
-
value = retrieve(body,
|
|
267
|
-
|
|
268
|
-
# status) rather than yield it, or take it for the end of the pipeline
|
|
269
|
-
return [false, resp_size] unless key
|
|
270
|
-
|
|
271
|
-
key = key.delete_prefix!('k')
|
|
275
|
+
value = @value_marshaller.retrieve(body, flag_int(bitflags, 'f'))
|
|
276
|
+
key = key ? key.delete_prefix!('k') : 0
|
|
272
277
|
key = KeyRegularizer.decode(key) if base64
|
|
273
278
|
[tokens.first == VA, flag_int(cas, 'c'), key, value, resp_size]
|
|
274
279
|
end
|
|
275
280
|
|
|
276
|
-
#
|
|
277
|
-
|
|
278
|
-
|
|
281
|
+
# getk_response_from_buffer for a "VA ..." header, read in place
|
|
282
|
+
# without allocating the header, its tokens or the flag strings.
|
|
283
|
+
# Gives the same result as the token path. Returns nil to fall back to
|
|
284
|
+
# it when the header has no s flag or a zero size.
|
|
285
|
+
def va_response_from_buffer(buf, offset, term_idx)
|
|
286
|
+
size = bitflags = cas = key = nil
|
|
287
|
+
base64 = false
|
|
288
|
+
pos = buf.byteindex(' ', offset + VA_PREFIX.bytesize)
|
|
289
|
+
while pos && pos < term_idx
|
|
290
|
+
start = pos + 1
|
|
291
|
+
pos = buf.byteindex(' ', start)
|
|
292
|
+
stop = pos && pos < term_idx ? pos : term_idx
|
|
293
|
+
case buf.getbyte(start)
|
|
294
|
+
when BYTE_S then size ||= flag_int_at(buf, start, stop)
|
|
295
|
+
when BYTE_F then bitflags ||= flag_int_at(buf, start, stop)
|
|
296
|
+
when BYTE_C then cas ||= flag_int_at(buf, start, stop)
|
|
297
|
+
when BYTE_K then key ||= buf.byteslice(start + 1, stop - start - 1)
|
|
298
|
+
when BYTE_B then base64 ||= stop == start + 1
|
|
299
|
+
end
|
|
300
|
+
end
|
|
301
|
+
return nil if size.nil? || size.zero?
|
|
302
|
+
|
|
303
|
+
header_len = term_idx - offset + TERMINATOR.length
|
|
304
|
+
resp_size = header_len + size + TERMINATOR.length
|
|
305
|
+
return [0] unless buf.bytesize >= offset + resp_size
|
|
306
|
+
|
|
307
|
+
value = @value_marshaller.retrieve(buf.byteslice(offset + header_len, size), bitflags || 0)
|
|
308
|
+
key = KeyRegularizer.decode(key) if base64 && key
|
|
309
|
+
[true, cas || 0, key || 0, value, resp_size]
|
|
310
|
+
end
|
|
311
|
+
|
|
312
|
+
# Integer value of a flag's token in buf, after its one-byte prefix,
|
|
313
|
+
# as flag_int would read it
|
|
314
|
+
def flag_int_at(buf, start, stop)
|
|
315
|
+
buf.byteslice(start + 1, stop - start - 1).to_i
|
|
279
316
|
end
|
|
280
317
|
|
|
281
318
|
# Integer value of a flag token such as "f123", or 0 when absent.
|
|
@@ -309,8 +346,29 @@ module Dalli
|
|
|
309
346
|
end
|
|
310
347
|
|
|
311
348
|
def bitflags_from_va_line(line)
|
|
312
|
-
|
|
313
|
-
|
|
349
|
+
flag_from_line(line, FLAGS_TOKEN_PREFIX, VA_PREFIX.bytesize)
|
|
350
|
+
end
|
|
351
|
+
|
|
352
|
+
# The key from a VA line's k flag, decoded when the line has the b flag,
|
|
353
|
+
# or nil when there is no k flag
|
|
354
|
+
def key_from_va_line(line)
|
|
355
|
+
idx = line.index(KEY_TOKEN_PREFIX, VA_PREFIX.bytesize)
|
|
356
|
+
return unless idx
|
|
357
|
+
|
|
358
|
+
start = idx + KEY_TOKEN_PREFIX.bytesize
|
|
359
|
+
stop = line.index(' ', start) || line.index(TERMINATOR, start) || line.bytesize
|
|
360
|
+
key = line.byteslice(start, stop - start)
|
|
361
|
+
base64_flag_in_line?(line) ? KeyRegularizer.decode(key) : key
|
|
362
|
+
end
|
|
363
|
+
|
|
364
|
+
def base64_flag_in_line?(line)
|
|
365
|
+
line.include?(' b ') || line.end_with?(" b#{TERMINATOR}", ' b')
|
|
366
|
+
end
|
|
367
|
+
|
|
368
|
+
# Integer value of the first " <flag><digits>" token at or after start, or 0
|
|
369
|
+
def flag_from_line(line, token_prefix, start)
|
|
370
|
+
idx = line.index(token_prefix, start)
|
|
371
|
+
idx ? line.byteslice(idx + token_prefix.bytesize, line.bytesize).to_i : 0
|
|
314
372
|
end
|
|
315
373
|
|
|
316
374
|
def cas_from_tokens(tokens)
|
|
@@ -366,19 +424,6 @@ module Dalli
|
|
|
366
424
|
end
|
|
367
425
|
end
|
|
368
426
|
|
|
369
|
-
# A pipelined reply claiming an impossible size would otherwise have the
|
|
370
|
-
# buffer wait for (and accumulate) that many bytes
|
|
371
|
-
# The size given right after "VA", or 0 when it isn't a number
|
|
372
|
-
def positional_size(tokens)
|
|
373
|
-
tokens.first == VA && tokens[1]&.match?(/\A\d+\z/) ? tokens[1].to_i : 0
|
|
374
|
-
end
|
|
375
|
-
|
|
376
|
-
def check_value_size!(size)
|
|
377
|
-
return if size.between?(0, MAX_VALUE_BYTES)
|
|
378
|
-
|
|
379
|
-
raise Dalli::DalliError, "Reply value size #{size} is out of range"
|
|
380
|
-
end
|
|
381
|
-
|
|
382
427
|
def read_line
|
|
383
428
|
@io_source.read_line&.chomp!(TERMINATOR)
|
|
384
429
|
end
|
|
@@ -389,9 +434,6 @@ module Dalli
|
|
|
389
434
|
end
|
|
390
435
|
|
|
391
436
|
def read_data(data_size)
|
|
392
|
-
# Checked before the terminator is added: -1 or -2 would otherwise
|
|
393
|
-
# read 1 or 0 bytes and leave the connection out of step
|
|
394
|
-
check_value_size!(data_size)
|
|
395
437
|
@io_source.read(data_size + TERMINATOR.bytesize)&.chomp!(TERMINATOR)
|
|
396
438
|
end
|
|
397
439
|
end
|
|
@@ -15,17 +15,11 @@ module Dalli
|
|
|
15
15
|
compress: true,
|
|
16
16
|
compressor: ::Dalli::Compressor,
|
|
17
17
|
# min byte size to attempt compression
|
|
18
|
-
compression_min_size: 4 * 1024
|
|
19
|
-
# max size a stored value may decompress to (nil: no limit). Guards
|
|
20
|
-
# against a small compressed value expanding into gigabytes on read.
|
|
21
|
-
decompressed_max_bytes: 128 * 1024 * 1024 # 128 MiB
|
|
18
|
+
compression_min_size: 4 * 1024 # 4K
|
|
22
19
|
}.freeze
|
|
23
20
|
|
|
24
21
|
OPTIONS = DEFAULTS.keys.freeze
|
|
25
22
|
|
|
26
|
-
KEYWORD_PARAMETER_TYPES = %i[key keyreq].freeze
|
|
27
|
-
private_constant :KEYWORD_PARAMETER_TYPES
|
|
28
|
-
|
|
29
23
|
def initialize(client_options)
|
|
30
24
|
@compression_options =
|
|
31
25
|
DEFAULTS.merge(client_options.slice(*OPTIONS))
|
|
@@ -40,16 +34,8 @@ module Dalli
|
|
|
40
34
|
end
|
|
41
35
|
|
|
42
36
|
def retrieve(value, bitflags)
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
max_bytes = @compression_options[:decompressed_max_bytes]
|
|
46
|
-
# Custom compressors that only define decompress(data) keep working,
|
|
47
|
-
# without the limit
|
|
48
|
-
if max_bytes && compressor_accepts_max_bytes?
|
|
49
|
-
compressor.decompress(value, max_bytes: max_bytes)
|
|
50
|
-
else
|
|
51
|
-
compressor.decompress(value)
|
|
52
|
-
end
|
|
37
|
+
compressed = bitflags.anybits?(Flags::COMPRESSED)
|
|
38
|
+
compressed ? compressor.decompress(value) : value
|
|
53
39
|
|
|
54
40
|
# TODO: We likely want to move this rescue into the Dalli::Compressor / Dalli::GzipCompressor
|
|
55
41
|
# itself, since not all compressors necessarily use Zlib. For now keep it here, so the behavior
|
|
@@ -58,19 +44,6 @@ module Dalli
|
|
|
58
44
|
raise UnmarshalError, "Unable to uncompress value: #{$ERROR_INFO.message}"
|
|
59
45
|
end
|
|
60
46
|
|
|
61
|
-
def compressor_accepts_max_bytes?
|
|
62
|
-
return @compressor_accepts_max_bytes if defined?(@compressor_accepts_max_bytes)
|
|
63
|
-
|
|
64
|
-
@compressor_accepts_max_bytes =
|
|
65
|
-
begin
|
|
66
|
-
compressor.method(:decompress).parameters.any? do |type, name|
|
|
67
|
-
name == :max_bytes && KEYWORD_PARAMETER_TYPES.include?(type)
|
|
68
|
-
end
|
|
69
|
-
rescue NameError # an object without a reflectable decompress method
|
|
70
|
-
false
|
|
71
|
-
end
|
|
72
|
-
end
|
|
73
|
-
|
|
74
47
|
def compress_by_default?
|
|
75
48
|
@compression_options[:compress]
|
|
76
49
|
end
|
|
@@ -84,13 +57,11 @@ module Dalli
|
|
|
84
57
|
end
|
|
85
58
|
|
|
86
59
|
# Checks whether we should apply compression when serializing a value
|
|
87
|
-
# based on the specified options. Returns false
|
|
88
|
-
#
|
|
89
|
-
#
|
|
90
|
-
#
|
|
91
|
-
# falling back to the server default.
|
|
60
|
+
# based on the specified options. Returns false unless the value
|
|
61
|
+
# is greater than the minimum compression size. Otherwise returns
|
|
62
|
+
# based on a method-level option if specified, falling back to the
|
|
63
|
+
# server default.
|
|
92
64
|
def compress_value?(value, req_options)
|
|
93
|
-
return false if req_options&.dig(:raw)
|
|
94
65
|
return false unless value.bytesize >= compression_min_size
|
|
95
66
|
return compress_by_default? unless req_options && !req_options[:compress].nil?
|
|
96
67
|
|
|
@@ -100,7 +100,7 @@ module Dalli
|
|
|
100
100
|
Dalli.logger.warn 'SECURITY WARNING: Dalli is using Marshal for serialization. ' \
|
|
101
101
|
'Marshal can execute arbitrary code during deserialization. ' \
|
|
102
102
|
'If your memcached server could be compromised, consider using ' \
|
|
103
|
-
'a safer serializer: Dalli::Client.new(servers, serializer:
|
|
103
|
+
'a safer serializer like JSON: Dalli::Client.new(servers, serializer: JSON)'
|
|
104
104
|
@@marshal_warning_logged = true # rubocop:disable Style/ClassVars
|
|
105
105
|
end
|
|
106
106
|
end
|
data/lib/dalli/protocol.rb
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require 'openssl'
|
|
4
3
|
require 'timeout'
|
|
5
4
|
|
|
6
5
|
module Dalli
|
|
@@ -17,11 +16,14 @@ module Dalli
|
|
|
17
16
|
[Timeout::Error]
|
|
18
17
|
end
|
|
19
18
|
|
|
20
|
-
# SSL errors
|
|
21
|
-
#
|
|
22
|
-
#
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
19
|
+
# SSL errors that occur during read/write operations (not during initial
|
|
20
|
+
# handshake) should trigger reconnection. These indicate transient network
|
|
21
|
+
# issues, not configuration problems.
|
|
22
|
+
SSL_ERRORS =
|
|
23
|
+
if defined?(OpenSSL::SSL::SSLError)
|
|
24
|
+
[OpenSSL::SSL::SSLError]
|
|
25
|
+
else
|
|
26
|
+
[]
|
|
27
|
+
end
|
|
26
28
|
end
|
|
27
29
|
end
|
data/lib/dalli/ring.rb
CHANGED
|
@@ -36,9 +36,10 @@ module Dalli
|
|
|
36
36
|
|
|
37
37
|
def continuum=(entries)
|
|
38
38
|
@continuum = entries
|
|
39
|
-
# Plain integers, so the
|
|
40
|
-
#
|
|
39
|
+
# Plain integers, so the lookup in server_for_hash_key needn't call
|
|
40
|
+
# Entry#value at each step
|
|
41
41
|
@continuum_values = entries&.map(&:value)
|
|
42
|
+
build_buckets
|
|
42
43
|
end
|
|
43
44
|
|
|
44
45
|
# alive_cache (optional) remembers each server's alive? result, so a
|
|
@@ -55,15 +56,17 @@ module Dalli
|
|
|
55
56
|
raise Dalli::RingError, 'No server available'
|
|
56
57
|
end
|
|
57
58
|
|
|
59
|
+
# Tries the key's own server first, then (with failover) up to 19
|
|
60
|
+
# rehashes of "<try><key>". The first try is outside the loop because it
|
|
61
|
+
# is almost always the one that's used.
|
|
58
62
|
def server_from_continuum(key, alive_cache = nil)
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
63
|
+
server = server_for_hash_key(hash_for(key))
|
|
64
|
+
return server if server_alive?(server, alive_cache)
|
|
65
|
+
return nil unless @failover
|
|
62
66
|
|
|
67
|
+
19.times do |try|
|
|
68
|
+
server = server_for_hash_key(hash_for("#{try}#{key}"))
|
|
63
69
|
return server if server_alive?(server, alive_cache)
|
|
64
|
-
break unless @failover
|
|
65
|
-
|
|
66
|
-
hkey = hash_for("#{try}#{key}")
|
|
67
70
|
end
|
|
68
71
|
nil
|
|
69
72
|
end
|
|
@@ -87,8 +90,13 @@ module Dalli
|
|
|
87
90
|
end
|
|
88
91
|
end
|
|
89
92
|
|
|
93
|
+
# Drains the replies left by quiet requests. Only servers that were sent a
|
|
94
|
+
# quiet request need it, so the others (and servers never connected) are
|
|
95
|
+
# skipped rather than each costing a noop round trip.
|
|
90
96
|
def pipeline_consume_and_ignore_responses
|
|
91
97
|
@servers.each do |s|
|
|
98
|
+
next unless s.quiet_responses_pending?
|
|
99
|
+
|
|
92
100
|
s.request(:noop)
|
|
93
101
|
rescue Dalli::NetworkError
|
|
94
102
|
# Ignore this error, as it indicates the socket is unavailable
|
|
@@ -121,22 +129,41 @@ module Dalli
|
|
|
121
129
|
def server_alive?(server, alive_cache)
|
|
122
130
|
return server.alive? unless alive_cache
|
|
123
131
|
|
|
124
|
-
|
|
132
|
+
alive = alive_cache[server]
|
|
133
|
+
alive.nil? ? (alive_cache[server] = server.alive?) : alive
|
|
125
134
|
end
|
|
126
135
|
|
|
127
136
|
def entry_count_for(server, total_servers, total_weight)
|
|
128
137
|
((total_servers * POINTS_PER_SERVER * server.weight) / Float(total_weight)).floor
|
|
129
138
|
end
|
|
130
139
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
140
|
+
# Hash values are 32 bits. Splitting that range into about one bucket per
|
|
141
|
+
# continuum entry, and noting the first entry at or after each bucket's
|
|
142
|
+
# start, lets server_for_hash_key skip the binary search: it starts at the
|
|
143
|
+
# key's bucket and steps over the few entries before the key's hash.
|
|
144
|
+
def build_buckets
|
|
145
|
+
return unless @continuum_values
|
|
146
|
+
|
|
147
|
+
bits = [@continuum_values.size.bit_length, 1].max
|
|
148
|
+
@bucket_shift = 32 - bits
|
|
149
|
+
@bucket_first = Array.new(1 << bits)
|
|
150
|
+
idx = 0
|
|
151
|
+
size = @continuum_values.size
|
|
152
|
+
@bucket_first.each_index do |bucket|
|
|
153
|
+
floor = bucket << @bucket_shift
|
|
154
|
+
idx += 1 while idx < size && @continuum_values[idx] < floor
|
|
155
|
+
@bucket_first[bucket] = idx
|
|
138
156
|
end
|
|
139
|
-
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def server_for_hash_key(hash_key)
|
|
160
|
+
# Find the last entry with value <= hash_key, wrapping around to the
|
|
161
|
+
# last entry when every value is greater (idx - 1 is then -1)
|
|
162
|
+
idx = @bucket_first[hash_key >> @bucket_shift]
|
|
163
|
+
values = @continuum_values
|
|
164
|
+
size = values.size
|
|
165
|
+
idx += 1 while idx < size && values[idx] <= hash_key
|
|
166
|
+
@continuum[idx - 1].server
|
|
140
167
|
end
|
|
141
168
|
|
|
142
169
|
def build_continuum(servers)
|