dalli 5.1.10 → 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 +36 -122
- data/README.md +20 -25
- data/lib/dalli/cache_result.rb +65 -0
- data/lib/dalli/client.rb +87 -61
- 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 +21 -44
- data/lib/dalli/pipelined_setter.rb +8 -17
- data/lib/dalli/protocol/base.rb +26 -145
- data/lib/dalli/protocol/connection_manager.rb +13 -121
- data/lib/dalli/protocol/meta.rb +64 -127
- 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 +106 -98
- data/lib/dalli/protocol/string_marshaller.rb +1 -3
- data/lib/dalli/protocol/value_compressor.rb +7 -36
- data/lib/dalli/protocol/value_marshaller.rb +0 -5
- 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 +9 -64
- data/lib/dalli/version.rb +1 -1
- data/lib/dalli.rb +1 -5
- metadata +4 -7
- 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,11 +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
|
|
29
|
-
MAX_STATS_ENTRIES = 10_000
|
|
30
|
-
MAX_QUIET_RESPONSES = 100_000
|
|
27
|
+
HD_PREFIX = 'HD '
|
|
31
28
|
FLAGS_TOKEN_PREFIX = ' f'
|
|
29
|
+
CAS_TOKEN_PREFIX = ' c'
|
|
30
|
+
KEY_TOKEN_PREFIX = ' k'
|
|
32
31
|
BYTE_B = 'b'.ord
|
|
33
32
|
BYTE_C = 'c'.ord
|
|
34
33
|
BYTE_F = 'f'.ord
|
|
@@ -50,15 +49,12 @@ module Dalli
|
|
|
50
49
|
@value_marshaller = value_marshaller
|
|
51
50
|
end
|
|
52
51
|
|
|
53
|
-
|
|
54
|
-
# ignoring any flags in the reply. The request didn't ask for flags, so
|
|
55
|
-
# a reply carrying them anyway must not get the value deserialized.
|
|
56
|
-
def meta_get_with_value(cache_nils: false, raw: false)
|
|
52
|
+
def meta_get_with_value(cache_nils: false)
|
|
57
53
|
line = read_line
|
|
58
54
|
# A hit ("VA <size> f<flags>") is parsed in place rather than split
|
|
59
55
|
# into tokens, which saves several allocations on the hottest path.
|
|
60
56
|
if line&.start_with?(VA_PREFIX)
|
|
61
|
-
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))
|
|
62
58
|
end
|
|
63
59
|
|
|
64
60
|
tokens = line&.split || []
|
|
@@ -66,7 +62,7 @@ module Dalli
|
|
|
66
62
|
when EN
|
|
67
63
|
cache_nils ? ::Dalli::NOT_FOUND : nil
|
|
68
64
|
when VA # only reached for an unusually formatted hit line
|
|
69
|
-
retrieve(read_data(tokens[1].to_i),
|
|
65
|
+
@value_marshaller.retrieve(read_data(tokens[1].to_i), bitflags_from_tokens(tokens))
|
|
70
66
|
when HD
|
|
71
67
|
true
|
|
72
68
|
else
|
|
@@ -74,14 +70,14 @@ module Dalli
|
|
|
74
70
|
end
|
|
75
71
|
end
|
|
76
72
|
|
|
77
|
-
def meta_get_with_value_and_cas
|
|
73
|
+
def meta_get_with_value_and_cas
|
|
78
74
|
tokens = error_on_unexpected!(T_VA_EN_HD)
|
|
79
75
|
return [nil, 0] if tokens.first == EN
|
|
80
76
|
|
|
81
77
|
cas = cas_from_tokens(tokens)
|
|
82
78
|
return [nil, cas] unless tokens.first == VA
|
|
83
79
|
|
|
84
|
-
[retrieve(read_data(tokens[1].to_i),
|
|
80
|
+
[@value_marshaller.retrieve(read_data(tokens[1].to_i), bitflags_from_tokens(tokens)), cas]
|
|
85
81
|
end
|
|
86
82
|
|
|
87
83
|
def meta_get_without_value
|
|
@@ -101,13 +97,13 @@ module Dalli
|
|
|
101
97
|
# Used by meta_get for comprehensive metadata retrieval.
|
|
102
98
|
# Supports thundering herd protection (N/R flags) and metadata flags (h/l/u).
|
|
103
99
|
def meta_get_with_metadata(cache_nils: false, return_hit_status: false, return_last_access: false,
|
|
104
|
-
return_ttl_remaining: false
|
|
100
|
+
return_ttl_remaining: false)
|
|
105
101
|
tokens = error_on_unexpected!(T_VA_EN_HD)
|
|
106
102
|
result = build_metadata_result(tokens)
|
|
107
103
|
result[:hit_before] = hit_status_from_tokens(tokens) if return_hit_status
|
|
108
104
|
result[:last_access] = last_access_from_tokens(tokens) if return_last_access
|
|
109
105
|
result[:ttl_remaining] = ttl_remaining_from_tokens(tokens) if return_ttl_remaining
|
|
110
|
-
result[:value] = parse_value_from_tokens(tokens, cache_nils
|
|
106
|
+
result[:value] = parse_value_from_tokens(tokens, cache_nils)
|
|
111
107
|
result
|
|
112
108
|
end
|
|
113
109
|
|
|
@@ -124,15 +120,21 @@ module Dalli
|
|
|
124
120
|
}
|
|
125
121
|
end
|
|
126
122
|
|
|
127
|
-
def parse_value_from_tokens(tokens, cache_nils
|
|
123
|
+
def parse_value_from_tokens(tokens, cache_nils)
|
|
128
124
|
return cache_nils ? ::Dalli::NOT_FOUND : nil if tokens.first == EN
|
|
129
125
|
return unless tokens.first == VA
|
|
130
126
|
|
|
131
|
-
retrieve(read_data(tokens[1].to_i),
|
|
127
|
+
@value_marshaller.retrieve(read_data(tokens[1].to_i), bitflags_from_tokens(tokens))
|
|
132
128
|
end
|
|
133
129
|
|
|
134
130
|
def meta_set_with_cas
|
|
135
|
-
|
|
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)
|
|
136
138
|
return false unless tokens.first == HD
|
|
137
139
|
|
|
138
140
|
cas_from_tokens(tokens)
|
|
@@ -146,7 +148,13 @@ module Dalli
|
|
|
146
148
|
end
|
|
147
149
|
|
|
148
150
|
def meta_delete
|
|
149
|
-
|
|
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)
|
|
150
158
|
tokens.first == HD
|
|
151
159
|
end
|
|
152
160
|
|
|
@@ -161,11 +169,7 @@ module Dalli
|
|
|
161
169
|
def stats
|
|
162
170
|
tokens = error_on_unexpected!(T_END_TOKEN_STAT)
|
|
163
171
|
values = {}
|
|
164
|
-
replies = 0
|
|
165
172
|
while tokens.first != END_TOKEN
|
|
166
|
-
replies += 1
|
|
167
|
-
raise Dalli::DalliError, 'Too many stats replies' if replies > MAX_STATS_ENTRIES
|
|
168
|
-
|
|
169
173
|
values[tokens[1]] = tokens[2]
|
|
170
174
|
tokens = next_line_to_tokens
|
|
171
175
|
end
|
|
@@ -189,16 +193,10 @@ module Dalli
|
|
|
189
193
|
tokens.last
|
|
190
194
|
end
|
|
191
195
|
|
|
192
|
-
def consume_all_responses_until_mn
|
|
196
|
+
def consume_all_responses_until_mn
|
|
193
197
|
tokens = next_line_to_tokens
|
|
194
198
|
|
|
195
|
-
|
|
196
|
-
while tokens.first != MN
|
|
197
|
-
replies += 1
|
|
198
|
-
raise Dalli::DalliError, 'Too many quiet replies' if replies > max_responses
|
|
199
|
-
|
|
200
|
-
tokens = next_line_to_tokens
|
|
201
|
-
end
|
|
199
|
+
tokens = next_line_to_tokens while tokens.first != MN
|
|
202
200
|
true
|
|
203
201
|
end
|
|
204
202
|
|
|
@@ -209,13 +207,11 @@ module Dalli
|
|
|
209
207
|
# was NOT deleted -- a miss (NF) or an error. Returns that count so
|
|
210
208
|
# callers can derive the number of successful deletes as
|
|
211
209
|
# (keys_sent - non_deletions).
|
|
212
|
-
def pipelined_delete_non_deletions
|
|
210
|
+
def pipelined_delete_non_deletions
|
|
213
211
|
non_deletions = 0
|
|
214
212
|
tokens = next_line_to_tokens
|
|
215
213
|
until tokens.first == MN
|
|
216
214
|
non_deletions += 1
|
|
217
|
-
raise Dalli::DalliError, 'Too many delete replies' if non_deletions > max_responses
|
|
218
|
-
|
|
219
215
|
tokens = next_line_to_tokens
|
|
220
216
|
end
|
|
221
217
|
non_deletions
|
|
@@ -231,20 +227,16 @@ module Dalli
|
|
|
231
227
|
# The remaining three values in the array are the ResponseHeader,
|
|
232
228
|
# key, and value.
|
|
233
229
|
##
|
|
234
|
-
|
|
235
|
-
# see meta_get_with_value.
|
|
236
|
-
def getk_response_from_buffer(buf, offset = 0, raw: false)
|
|
230
|
+
def getk_response_from_buffer(buf, offset = 0)
|
|
237
231
|
# Find the header terminator starting from offset
|
|
238
232
|
term_idx = buf.byteindex(TERMINATOR, offset)
|
|
239
|
-
max_line_bytes = ConnectionManager::MAX_LINE_BYTES
|
|
240
|
-
header_too_long = if term_idx
|
|
241
|
-
term_idx - offset + TERMINATOR.bytesize > max_line_bytes
|
|
242
|
-
else
|
|
243
|
-
buf.bytesize - offset >= max_line_bytes
|
|
244
|
-
end
|
|
245
|
-
raise Dalli::DalliError, "Reply line without CRLF in the first #{max_line_bytes} bytes" if header_too_long
|
|
246
233
|
return [0] unless term_idx
|
|
247
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
|
+
|
|
248
240
|
header = buf.byteslice(offset, term_idx - offset)
|
|
249
241
|
tokens = header.split
|
|
250
242
|
header_len = header.bytesize + TERMINATOR.length
|
|
@@ -262,44 +254,65 @@ module Dalli
|
|
|
262
254
|
when BYTE_B then base64 ||= token == 'b'
|
|
263
255
|
end
|
|
264
256
|
end
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
#
|
|
270
|
-
#
|
|
271
|
-
#
|
|
272
|
-
#
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
# still has a body -- just its terminator -- so it's parsed below.
|
|
276
|
-
return [tokens.first == MN, header_len] if body_len.zero? && tokens.first != VA
|
|
277
|
-
|
|
278
|
-
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
|
+
|
|
279
267
|
resp_size = header_len + body_len + TERMINATOR.length
|
|
280
268
|
# The header is in the buffer, but the body is not. As we don't have
|
|
281
269
|
# a complete response, don't advance the buffer
|
|
282
270
|
return [0] unless buf.bytesize >= offset + resp_size
|
|
283
271
|
|
|
284
|
-
term = offset + header_len + body_len
|
|
285
|
-
raise_bad_value_terminator unless buf.getbyte(term) == 13 && buf.getbyte(term + 1) == 10
|
|
286
|
-
|
|
287
272
|
# The full response is in our buffer, so parse it and return
|
|
288
273
|
# the values
|
|
289
274
|
body = buf.byteslice(offset + header_len, body_len)
|
|
290
|
-
value = retrieve(body,
|
|
291
|
-
|
|
292
|
-
# status) rather than yield it, or take it for the end of the pipeline
|
|
293
|
-
return [false, resp_size] unless key
|
|
294
|
-
|
|
295
|
-
key = key.delete_prefix!('k')
|
|
275
|
+
value = @value_marshaller.retrieve(body, flag_int(bitflags, 'f'))
|
|
276
|
+
key = key ? key.delete_prefix!('k') : 0
|
|
296
277
|
key = KeyRegularizer.decode(key) if base64
|
|
297
278
|
[tokens.first == VA, flag_int(cas, 'c'), key, value, resp_size]
|
|
298
279
|
end
|
|
299
280
|
|
|
300
|
-
#
|
|
301
|
-
|
|
302
|
-
|
|
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
|
|
303
316
|
end
|
|
304
317
|
|
|
305
318
|
# Integer value of a flag token such as "f123", or 0 when absent.
|
|
@@ -333,8 +346,29 @@ module Dalli
|
|
|
333
346
|
end
|
|
334
347
|
|
|
335
348
|
def bitflags_from_va_line(line)
|
|
336
|
-
|
|
337
|
-
|
|
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
|
|
338
372
|
end
|
|
339
373
|
|
|
340
374
|
def cas_from_tokens(tokens)
|
|
@@ -390,21 +424,6 @@ module Dalli
|
|
|
390
424
|
end
|
|
391
425
|
end
|
|
392
426
|
|
|
393
|
-
# A pipelined reply claiming an impossible size would otherwise have the
|
|
394
|
-
# buffer wait for (and accumulate) that many bytes
|
|
395
|
-
# The size given right after "VA", or 0 when it isn't a number
|
|
396
|
-
def positional_size(tokens)
|
|
397
|
-
tokens.first == VA && tokens[1]&.match?(/\A\d+\z/) ? tokens[1].to_i : 0
|
|
398
|
-
end
|
|
399
|
-
|
|
400
|
-
def check_value_size!(size)
|
|
401
|
-
limit = @value_marshaller.max_response_bytes
|
|
402
|
-
return if size >= 0 && size <= MAX_VALUE_BYTES && size <= limit # rubocop:disable Style/ComparableBetween
|
|
403
|
-
|
|
404
|
-
effective_limit = [MAX_VALUE_BYTES, limit].min
|
|
405
|
-
raise Dalli::DalliError, "Reply value size #{size} exceeds the #{effective_limit}-byte response limit"
|
|
406
|
-
end
|
|
407
|
-
|
|
408
427
|
def read_line
|
|
409
428
|
@io_source.read_line&.chomp!(TERMINATOR)
|
|
410
429
|
end
|
|
@@ -414,19 +433,8 @@ module Dalli
|
|
|
414
433
|
line&.split || []
|
|
415
434
|
end
|
|
416
435
|
|
|
417
|
-
def strip_value_terminator!(data)
|
|
418
|
-
data&.chomp!(TERMINATOR) || raise_bad_value_terminator
|
|
419
|
-
end
|
|
420
|
-
|
|
421
|
-
def raise_bad_value_terminator
|
|
422
|
-
raise Dalli::DalliError, "Reply value isn't followed by CRLF, so its size doesn't match the value"
|
|
423
|
-
end
|
|
424
|
-
|
|
425
436
|
def read_data(data_size)
|
|
426
|
-
|
|
427
|
-
# read 1 or 0 bytes and leave the connection out of step
|
|
428
|
-
check_value_size!(data_size)
|
|
429
|
-
strip_value_terminator!(@io_source.read(data_size + TERMINATOR.bytesize))
|
|
437
|
+
@io_source.read(data_size + TERMINATOR.bytesize)&.chomp!(TERMINATOR)
|
|
430
438
|
end
|
|
431
439
|
end
|
|
432
440
|
end
|
|
@@ -14,14 +14,12 @@ module Dalli
|
|
|
14
14
|
value_max_bytes: 1024 * 1024
|
|
15
15
|
}.freeze
|
|
16
16
|
|
|
17
|
-
attr_reader :value_max_bytes
|
|
17
|
+
attr_reader :value_max_bytes
|
|
18
18
|
|
|
19
19
|
def initialize(client_options)
|
|
20
20
|
@value_max_bytes = client_options.fetch(:value_max_bytes) do
|
|
21
21
|
DEFAULTS.fetch(:value_max_bytes)
|
|
22
22
|
end.to_i
|
|
23
|
-
@max_response_bytes = Integer(client_options.fetch(:max_response_bytes, @value_max_bytes))
|
|
24
|
-
raise ArgumentError, 'max_response_bytes must be positive' unless @max_response_bytes.positive?
|
|
25
23
|
end
|
|
26
24
|
|
|
27
25
|
def store(key, value, _options = nil)
|
|
@@ -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
|
|
|
@@ -28,9 +28,6 @@ module Dalli
|
|
|
28
28
|
|
|
29
29
|
@marshal_options =
|
|
30
30
|
DEFAULTS.merge(client_options.slice(*OPTIONS))
|
|
31
|
-
@marshal_options[:value_max_bytes] = @marshal_options[:value_max_bytes].to_i
|
|
32
|
-
@max_response_bytes = Integer(client_options.fetch(:max_response_bytes, value_max_bytes))
|
|
33
|
-
raise ArgumentError, 'max_response_bytes must be positive' unless @max_response_bytes.positive?
|
|
34
31
|
end
|
|
35
32
|
|
|
36
33
|
def store(key, value, options = nil)
|
|
@@ -51,8 +48,6 @@ module Dalli
|
|
|
51
48
|
@marshal_options[:value_max_bytes]
|
|
52
49
|
end
|
|
53
50
|
|
|
54
|
-
attr_reader :max_response_bytes
|
|
55
|
-
|
|
56
51
|
def error_if_over_max_value_bytes(key, value)
|
|
57
52
|
return if value.bytesize <= value_max_bytes
|
|
58
53
|
|
|
@@ -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
|