dalli 5.0.16 → 5.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 +48 -142
- data/Gemfile +1 -1
- data/README.md +7 -33
- data/lib/dalli/client.rb +275 -116
- 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_deleter.rb +29 -12
- data/lib/dalli/pipelined_getter.rb +74 -52
- data/lib/dalli/pipelined_setter.rb +22 -19
- data/lib/dalli/protocol/base.rb +64 -151
- data/lib/dalli/protocol/connection_manager.rb +13 -139
- data/lib/dalli/protocol/key_regularizer.rb +18 -1
- data/lib/dalli/protocol/meta.rb +99 -117
- data/lib/dalli/protocol/request_formatter.rb +110 -50
- data/lib/dalli/protocol/response_processor.rb +42 -93
- 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/socket.rb +9 -87
- data/lib/dalli/version.rb +2 -2
- data/lib/dalli.rb +0 -5
- data/lib/rack/session/dalli.rb +2 -1
- metadata +3 -7
- data/lib/dalli/json_serializer.rb +0 -28
|
@@ -23,11 +23,6 @@ module Dalli
|
|
|
23
23
|
VERSION = 'VERSION'
|
|
24
24
|
SERVER_ERROR = 'SERVER_ERROR'
|
|
25
25
|
|
|
26
|
-
# memcached can't store an item larger than 1 GiB (its -I maximum)
|
|
27
|
-
MAX_VALUE_BYTES = 1024 * 1024 * 1024
|
|
28
|
-
MAX_STATS_ENTRIES = 10_000
|
|
29
|
-
MAX_QUIET_RESPONSES = 100_000
|
|
30
|
-
|
|
31
26
|
T_OK = [OK].freeze
|
|
32
27
|
T_RESET = [RESET].freeze
|
|
33
28
|
T_EN_HD = [EN, HD].freeze
|
|
@@ -43,25 +38,22 @@ module Dalli
|
|
|
43
38
|
@value_marshaller = value_marshaller
|
|
44
39
|
end
|
|
45
40
|
|
|
46
|
-
|
|
47
|
-
# ignoring any flags in the reply. The request didn't ask for flags, so
|
|
48
|
-
# a reply carrying them anyway must not get the value deserialized.
|
|
49
|
-
def meta_get_with_value(cache_nils: false, raw: false)
|
|
41
|
+
def meta_get_with_value(cache_nils: false)
|
|
50
42
|
tokens = error_on_unexpected!(T_VA_EN_HD)
|
|
51
43
|
return cache_nils ? ::Dalli::NOT_FOUND : nil if tokens.first == EN
|
|
52
44
|
return true unless tokens.first == VA
|
|
53
45
|
|
|
54
|
-
retrieve(read_data(tokens[1].to_i),
|
|
46
|
+
@value_marshaller.retrieve(read_data(tokens[1].to_i), bitflags_from_tokens(tokens))
|
|
55
47
|
end
|
|
56
48
|
|
|
57
|
-
def meta_get_with_value_and_cas
|
|
49
|
+
def meta_get_with_value_and_cas
|
|
58
50
|
tokens = error_on_unexpected!(T_VA_EN_HD)
|
|
59
51
|
return [nil, 0] if tokens.first == EN
|
|
60
52
|
|
|
61
53
|
cas = cas_from_tokens(tokens)
|
|
62
54
|
return [nil, cas] unless tokens.first == VA
|
|
63
55
|
|
|
64
|
-
[retrieve(read_data(tokens[1].to_i),
|
|
56
|
+
[@value_marshaller.retrieve(read_data(tokens[1].to_i), bitflags_from_tokens(tokens)), cas]
|
|
65
57
|
end
|
|
66
58
|
|
|
67
59
|
def meta_get_without_value
|
|
@@ -81,12 +73,13 @@ module Dalli
|
|
|
81
73
|
# Used by meta_get for comprehensive metadata retrieval.
|
|
82
74
|
# Supports thundering herd protection (N/R flags) and metadata flags (h/l/u).
|
|
83
75
|
def meta_get_with_metadata(cache_nils: false, return_hit_status: false, return_last_access: false,
|
|
84
|
-
|
|
76
|
+
return_ttl_remaining: false)
|
|
85
77
|
tokens = error_on_unexpected!(T_VA_EN_HD)
|
|
86
78
|
result = build_metadata_result(tokens)
|
|
87
79
|
result[:hit_before] = hit_status_from_tokens(tokens) if return_hit_status
|
|
88
80
|
result[:last_access] = last_access_from_tokens(tokens) if return_last_access
|
|
89
|
-
result[:
|
|
81
|
+
result[:ttl_remaining] = ttl_remaining_from_tokens(tokens) if return_ttl_remaining
|
|
82
|
+
result[:value] = parse_value_from_tokens(tokens, cache_nils)
|
|
90
83
|
result
|
|
91
84
|
end
|
|
92
85
|
|
|
@@ -94,20 +87,20 @@ module Dalli
|
|
|
94
87
|
{
|
|
95
88
|
value: nil, cas: cas_from_tokens(tokens),
|
|
96
89
|
won_recache: tokens.include?('W'), stale: tokens.include?('X'),
|
|
97
|
-
lost_recache: tokens.include?('Z')
|
|
90
|
+
lost_recache: tokens.include?('Z'),
|
|
91
|
+
# Explicit miss marker: EN means the key does not exist. A tombstoned
|
|
92
|
+
# item is NOT a miss -- it answers VA/HD with the X flag set -- and a
|
|
93
|
+
# stored nil under cache_nils is not one either, so neither can be
|
|
94
|
+
# inferred from value or cas alone.
|
|
95
|
+
miss: tokens.first == EN
|
|
98
96
|
}
|
|
99
97
|
end
|
|
100
98
|
|
|
101
|
-
def parse_value_from_tokens(tokens, cache_nils
|
|
99
|
+
def parse_value_from_tokens(tokens, cache_nils)
|
|
102
100
|
return cache_nils ? ::Dalli::NOT_FOUND : nil if tokens.first == EN
|
|
103
101
|
return unless tokens.first == VA
|
|
104
102
|
|
|
105
|
-
retrieve(read_data(tokens[1].to_i),
|
|
106
|
-
end
|
|
107
|
-
|
|
108
|
-
# Values from a raw request are returned as stored
|
|
109
|
-
def retrieve(value, bitflags, raw)
|
|
110
|
-
raw ? value : @value_marshaller.retrieve(value, bitflags)
|
|
103
|
+
@value_marshaller.retrieve(read_data(tokens[1].to_i), bitflags_from_tokens(tokens))
|
|
111
104
|
end
|
|
112
105
|
|
|
113
106
|
def meta_set_with_cas
|
|
@@ -140,11 +133,7 @@ module Dalli
|
|
|
140
133
|
def stats
|
|
141
134
|
tokens = error_on_unexpected!(T_END_TOKEN_STAT)
|
|
142
135
|
values = {}
|
|
143
|
-
replies = 0
|
|
144
136
|
while tokens.first != END_TOKEN
|
|
145
|
-
replies += 1
|
|
146
|
-
raise Dalli::DalliError, 'Too many stats replies' if replies > MAX_STATS_ENTRIES
|
|
147
|
-
|
|
148
137
|
values[tokens[1]] = tokens[2]
|
|
149
138
|
tokens = next_line_to_tokens
|
|
150
139
|
end
|
|
@@ -168,16 +157,10 @@ module Dalli
|
|
|
168
157
|
tokens.last
|
|
169
158
|
end
|
|
170
159
|
|
|
171
|
-
def consume_all_responses_until_mn
|
|
160
|
+
def consume_all_responses_until_mn
|
|
172
161
|
tokens = next_line_to_tokens
|
|
173
162
|
|
|
174
|
-
|
|
175
|
-
while tokens.first != MN
|
|
176
|
-
replies += 1
|
|
177
|
-
raise Dalli::DalliError, 'Too many quiet replies' if replies > max_responses
|
|
178
|
-
|
|
179
|
-
tokens = next_line_to_tokens
|
|
180
|
-
end
|
|
163
|
+
tokens = next_line_to_tokens while tokens.first != MN
|
|
181
164
|
true
|
|
182
165
|
end
|
|
183
166
|
|
|
@@ -188,23 +171,17 @@ module Dalli
|
|
|
188
171
|
# was NOT deleted -- a miss (NF) or an error. Returns that count so
|
|
189
172
|
# callers can derive the number of successful deletes as
|
|
190
173
|
# (keys_sent - non_deletions).
|
|
191
|
-
def pipelined_delete_non_deletions
|
|
174
|
+
def pipelined_delete_non_deletions
|
|
192
175
|
non_deletions = 0
|
|
193
176
|
tokens = next_line_to_tokens
|
|
194
177
|
until tokens.first == MN
|
|
195
178
|
non_deletions += 1
|
|
196
|
-
raise Dalli::DalliError, 'Too many delete replies' if non_deletions > max_responses
|
|
197
|
-
|
|
198
179
|
tokens = next_line_to_tokens
|
|
199
180
|
end
|
|
200
181
|
non_deletions
|
|
201
182
|
end
|
|
202
183
|
|
|
203
184
|
def full_response_from_buffer(tokens, body, resp_size)
|
|
204
|
-
# A hit with no key can't be matched to a request: skip it (false
|
|
205
|
-
# status) rather than yield it, or take it for the end of the pipeline
|
|
206
|
-
return [false, resp_size] unless tokens.any? { |t| t.start_with?('k') }
|
|
207
|
-
|
|
208
185
|
value = @value_marshaller.retrieve(body, bitflags_from_tokens(tokens))
|
|
209
186
|
[tokens.first == VA, cas_from_tokens(tokens), key_from_tokens(tokens), value, resp_size]
|
|
210
187
|
end
|
|
@@ -222,42 +199,26 @@ module Dalli
|
|
|
222
199
|
def getk_response_from_buffer(buf, offset = 0)
|
|
223
200
|
# Find the header terminator starting from offset
|
|
224
201
|
term_idx = buf.byteindex(TERMINATOR, offset)
|
|
225
|
-
max_line_bytes = ConnectionManager::MAX_LINE_BYTES
|
|
226
|
-
header_too_long = if term_idx
|
|
227
|
-
term_idx - offset + TERMINATOR.bytesize > max_line_bytes
|
|
228
|
-
else
|
|
229
|
-
buf.bytesize - offset >= max_line_bytes
|
|
230
|
-
end
|
|
231
|
-
raise Dalli::DalliError, "Reply line without CRLF in the first #{max_line_bytes} bytes" if header_too_long
|
|
232
202
|
return [0] unless term_idx
|
|
233
203
|
|
|
234
204
|
header = buf.byteslice(offset, term_idx - offset)
|
|
235
205
|
tokens = header.split
|
|
236
206
|
header_len = header.bytesize + TERMINATOR.length
|
|
237
207
|
|
|
238
|
-
# The body len is removed from the tokens array
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
#
|
|
244
|
-
#
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
# as the end would leave the remaining replies on the connection for
|
|
248
|
-
# later commands to read as their own. A hit on an empty value (VA 0)
|
|
249
|
-
# still has a body -- just its terminator -- so it's parsed below.
|
|
250
|
-
return [tokens.first == MN, header_len] if body_len.zero? && tokens.first != VA
|
|
251
|
-
|
|
252
|
-
check_value_size!(body_len)
|
|
208
|
+
# The body len is removed from the tokens array
|
|
209
|
+
body_len = body_len_from_tokens(tokens)
|
|
210
|
+
|
|
211
|
+
# We have a complete response that has no body.
|
|
212
|
+
# This is either the response to the terminating
|
|
213
|
+
# noop or, if the status is not MN, an intermediate
|
|
214
|
+
# error response that needs to be discarded.
|
|
215
|
+
return [true, header_len] if body_len.zero?
|
|
216
|
+
|
|
253
217
|
resp_size = header_len + body_len + TERMINATOR.length
|
|
254
218
|
# The header is in the buffer, but the body is not. As we don't have
|
|
255
219
|
# a complete response, don't advance the buffer
|
|
256
220
|
return [0] unless buf.bytesize >= offset + resp_size
|
|
257
221
|
|
|
258
|
-
term = offset + header_len + body_len
|
|
259
|
-
raise_bad_value_terminator unless buf.getbyte(term) == 13 && buf.getbyte(term + 1) == 10
|
|
260
|
-
|
|
261
222
|
# The full response is in our buffer, so parse it and return
|
|
262
223
|
# the values
|
|
263
224
|
body = buf.byteslice(offset + header_len, body_len)
|
|
@@ -282,6 +243,14 @@ module Dalli
|
|
|
282
243
|
value_from_tokens(tokens, 'c').to_i
|
|
283
244
|
end
|
|
284
245
|
|
|
246
|
+
# Detects the X presence flag, set when an item has been marked stale by a
|
|
247
|
+
# prior `md key I`. Uses strict equality (Array#any? with a String pattern
|
|
248
|
+
# compares with ==) so a future value-bearing flag beginning with X cannot
|
|
249
|
+
# be mistaken for it.
|
|
250
|
+
def stale_from_tokens(tokens)
|
|
251
|
+
tokens.any?('X')
|
|
252
|
+
end
|
|
253
|
+
|
|
285
254
|
def key_from_tokens(tokens)
|
|
286
255
|
encoded_key = value_from_tokens(tokens, 'k')
|
|
287
256
|
if tokens.delete('b')
|
|
@@ -306,6 +275,12 @@ module Dalli
|
|
|
306
275
|
value_from_tokens(tokens, 'l').to_i
|
|
307
276
|
end
|
|
308
277
|
|
|
278
|
+
# Returns seconds of TTL remaining; -1 when the item has no expiry.
|
|
279
|
+
# The t flag returns t<seconds>.
|
|
280
|
+
def ttl_remaining_from_tokens(tokens)
|
|
281
|
+
value_from_tokens(tokens, 't').to_i
|
|
282
|
+
end
|
|
283
|
+
|
|
309
284
|
def body_len_from_tokens(tokens)
|
|
310
285
|
value_from_tokens(tokens, 's').to_i
|
|
311
286
|
end
|
|
@@ -321,21 +296,6 @@ module Dalli
|
|
|
321
296
|
end
|
|
322
297
|
end
|
|
323
298
|
|
|
324
|
-
# A pipelined reply claiming an impossible size would otherwise have the
|
|
325
|
-
# buffer wait for (and accumulate) that many bytes
|
|
326
|
-
# The size given right after "VA", or 0 when it isn't a number
|
|
327
|
-
def positional_size(tokens)
|
|
328
|
-
tokens.first == VA && tokens[1]&.match?(/\A\d+\z/) ? tokens[1].to_i : 0
|
|
329
|
-
end
|
|
330
|
-
|
|
331
|
-
def check_value_size!(size)
|
|
332
|
-
limit = @value_marshaller.max_response_bytes
|
|
333
|
-
return if size >= 0 && size <= MAX_VALUE_BYTES && size <= limit # rubocop:disable Style/ComparableBetween
|
|
334
|
-
|
|
335
|
-
effective_limit = [MAX_VALUE_BYTES, limit].min
|
|
336
|
-
raise Dalli::DalliError, "Reply value size #{size} exceeds the #{effective_limit}-byte response limit"
|
|
337
|
-
end
|
|
338
|
-
|
|
339
299
|
def read_line
|
|
340
300
|
@io_source.read_line&.chomp!(TERMINATOR)
|
|
341
301
|
end
|
|
@@ -345,19 +305,8 @@ module Dalli
|
|
|
345
305
|
line&.split || []
|
|
346
306
|
end
|
|
347
307
|
|
|
348
|
-
def strip_value_terminator!(data)
|
|
349
|
-
data&.chomp!(TERMINATOR) || raise_bad_value_terminator
|
|
350
|
-
end
|
|
351
|
-
|
|
352
|
-
def raise_bad_value_terminator
|
|
353
|
-
raise Dalli::DalliError, "Reply value isn't followed by CRLF, so its size doesn't match the value"
|
|
354
|
-
end
|
|
355
|
-
|
|
356
308
|
def read_data(data_size)
|
|
357
|
-
|
|
358
|
-
# read 1 or 0 bytes and leave the connection out of step
|
|
359
|
-
check_value_size!(data_size)
|
|
360
|
-
strip_value_terminator!(@io_source.read(data_size + TERMINATOR.bytesize))
|
|
309
|
+
@io_source.read(data_size + TERMINATOR.bytesize)&.chomp!(TERMINATOR)
|
|
361
310
|
end
|
|
362
311
|
end
|
|
363
312
|
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
|
data/lib/dalli/socket.rb
CHANGED
|
@@ -12,35 +12,22 @@ module Dalli
|
|
|
12
12
|
# Common methods for all socket implementations.
|
|
13
13
|
##
|
|
14
14
|
module InstanceMethods
|
|
15
|
-
READ_CHUNK_SIZE = 8196
|
|
16
|
-
# Give the response parser control between bounded socket reads. In
|
|
17
|
-
# particular, a peer continuously sending a header without CRLF must
|
|
18
|
-
# not keep this method appending forever.
|
|
19
|
-
MAX_AVAILABLE_BYTES = READ_CHUNK_SIZE * 8
|
|
20
|
-
|
|
21
|
-
# Reads whatever is available without blocking. A read that returns less
|
|
22
|
-
# than a full chunk means the socket was drained, so we stop there rather
|
|
23
|
-
# than spend another read_nonblock call just to get :wait_readable back.
|
|
24
|
-
# Callers keep waiting on IO.select for anything that arrives later.
|
|
25
15
|
def read_available(reusable_buffer = nil)
|
|
26
16
|
if reusable_buffer
|
|
27
|
-
value = read_nonblock(
|
|
17
|
+
value = read_nonblock(8196, reusable_buffer, exception: false)
|
|
28
18
|
case value
|
|
29
19
|
when :wait_writable, :wait_readable
|
|
30
20
|
return reusable_buffer.clear
|
|
31
21
|
when nil
|
|
32
22
|
raise Errno::ECONNRESET, "Connection reset: #{logged_options.inspect}"
|
|
33
23
|
end
|
|
34
|
-
return value if drained_after?(value)
|
|
35
|
-
return value if value.bytesize >= MAX_AVAILABLE_BYTES
|
|
36
24
|
else
|
|
37
25
|
value = ''.b
|
|
38
26
|
end
|
|
39
27
|
|
|
40
28
|
buffer = ''.b
|
|
41
29
|
loop do
|
|
42
|
-
result = read_nonblock(
|
|
43
|
-
buffer, exception: false)
|
|
30
|
+
result = read_nonblock(8196, buffer, exception: false)
|
|
44
31
|
case result
|
|
45
32
|
when :wait_writable, :wait_readable
|
|
46
33
|
buffer.clear
|
|
@@ -49,20 +36,10 @@ module Dalli
|
|
|
49
36
|
raise Errno::ECONNRESET, "Connection reset: #{logged_options.inspect}"
|
|
50
37
|
else
|
|
51
38
|
value << result
|
|
52
|
-
return value if drained_after?(result)
|
|
53
|
-
return value if value.bytesize >= MAX_AVAILABLE_BYTES
|
|
54
39
|
end
|
|
55
40
|
end
|
|
56
41
|
end
|
|
57
42
|
|
|
58
|
-
def drained_after?(chunk)
|
|
59
|
-
chunk.bytesize < READ_CHUNK_SIZE && !buffered_data?
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
def buffered_data?
|
|
63
|
-
false
|
|
64
|
-
end
|
|
65
|
-
|
|
66
43
|
FILTERED_OUT_OPTIONS = %i[username password].freeze
|
|
67
44
|
def logged_options
|
|
68
45
|
options.except(*FILTERED_OUT_OPTIONS)
|
|
@@ -106,27 +83,6 @@ module Dalli
|
|
|
106
83
|
io.options
|
|
107
84
|
end
|
|
108
85
|
|
|
109
|
-
# OpenSSL can hold decrypted bytes that IO.select won't report as readable.
|
|
110
|
-
# With OpenSSL's default (no read-ahead) a short read has already used up
|
|
111
|
-
# the current record, so this is normally 0; it guards read-ahead setups.
|
|
112
|
-
#
|
|
113
|
-
# jruby-openssl can take the next record off the socket during a short
|
|
114
|
-
# read and hold it where neither pending nor IO.select sees it, so on
|
|
115
|
-
# JRuby a read continues until the socket would block.
|
|
116
|
-
def pending_data?
|
|
117
|
-
pending.positive? || (@rbuffer && !@rbuffer.empty?)
|
|
118
|
-
end
|
|
119
|
-
|
|
120
|
-
if RUBY_ENGINE == 'jruby'
|
|
121
|
-
def buffered_data?
|
|
122
|
-
true
|
|
123
|
-
end
|
|
124
|
-
else
|
|
125
|
-
def buffered_data?
|
|
126
|
-
pending_data?
|
|
127
|
-
end
|
|
128
|
-
end
|
|
129
|
-
|
|
130
86
|
unless method_defined?(:wait_readable)
|
|
131
87
|
def wait_readable(timeout = nil)
|
|
132
88
|
to_io.wait_readable(timeout)
|
|
@@ -160,12 +116,7 @@ module Dalli
|
|
|
160
116
|
sock.options = { host: host, port: port }.merge(options)
|
|
161
117
|
init_socket_options(sock, options)
|
|
162
118
|
|
|
163
|
-
|
|
164
|
-
wrapping_ssl_socket(sock, host, options[:ssl_context],
|
|
165
|
-
options[:socket_timeout])
|
|
166
|
-
else
|
|
167
|
-
sock
|
|
168
|
-
end
|
|
119
|
+
options[:ssl_context] ? wrapping_ssl_socket(sock, host, options[:ssl_context]) : sock
|
|
169
120
|
end
|
|
170
121
|
end
|
|
171
122
|
|
|
@@ -173,7 +124,7 @@ module Dalli
|
|
|
173
124
|
# Returns true for an unmodified TCPSocket on Ruby 3.0+, or for resolv-replace >= 0.2.0
|
|
174
125
|
# which forwards keyword arguments through its patch.
|
|
175
126
|
# Returns false when monkey-patched by gems like socksify or resolv-replace < 0.2.0.
|
|
176
|
-
# rubocop:disable
|
|
127
|
+
# rubocop:disable ThreadSafety/ClassInstanceVariable
|
|
177
128
|
def self.supports_connect_timeout?
|
|
178
129
|
return @supports_connect_timeout if defined?(@supports_connect_timeout)
|
|
179
130
|
|
|
@@ -184,6 +135,7 @@ module Dalli
|
|
|
184
135
|
end
|
|
185
136
|
end
|
|
186
137
|
end
|
|
138
|
+
# rubocop:enable ThreadSafety/ClassInstanceVariable
|
|
187
139
|
|
|
188
140
|
def self.create_socket_with_timeout(host, port, options)
|
|
189
141
|
if supports_connect_timeout?
|
|
@@ -240,55 +192,25 @@ module Dalli
|
|
|
240
192
|
|
|
241
193
|
# Detect and cache the correct pack format for struct timeval on this platform.
|
|
242
194
|
# Different architectures have different sizes for time_t and suseconds_t.
|
|
243
|
-
# rubocop:disable
|
|
195
|
+
# rubocop:disable ThreadSafety/ClassInstanceVariable
|
|
244
196
|
def self.timeval_pack_format(sock)
|
|
245
197
|
@timeval_pack_format ||= begin
|
|
246
198
|
expected_size = sock.getsockopt(::Socket::SOL_SOCKET, ::Socket::SO_RCVTIMEO).data.bytesize
|
|
247
199
|
TIMEVAL_PACK_FORMATS.find { |fmt| TIMEVAL_TEST_VALUES.pack(fmt).bytesize == expected_size } || 'll'
|
|
248
200
|
end
|
|
249
201
|
end
|
|
202
|
+
# rubocop:enable ThreadSafety/ClassInstanceVariable
|
|
250
203
|
|
|
251
204
|
def self.pack_timeval(sock, seconds, microseconds)
|
|
252
205
|
[seconds, microseconds].pack(timeval_pack_format(sock))
|
|
253
206
|
end
|
|
254
207
|
|
|
255
|
-
|
|
256
|
-
# attempt doesn't hold a file descriptor until GC.
|
|
257
|
-
def self.wrapping_ssl_socket(tcp_socket, host, ssl_context, timeout = nil)
|
|
258
|
-
warn_if_unverified(ssl_context)
|
|
259
|
-
connected = false
|
|
208
|
+
def self.wrapping_ssl_socket(tcp_socket, host, ssl_context)
|
|
260
209
|
ssl_socket = Dalli::Socket::SSLSocket.new(tcp_socket, ssl_context)
|
|
261
210
|
ssl_socket.hostname = host
|
|
262
211
|
ssl_socket.sync_close = true
|
|
263
|
-
|
|
264
|
-
# OpenSSL's blocking handshake on Ruby 3.3.
|
|
265
|
-
Timeout.timeout(timeout) { ssl_socket.connect }
|
|
266
|
-
connected = true
|
|
212
|
+
ssl_socket.connect
|
|
267
213
|
ssl_socket
|
|
268
|
-
ensure
|
|
269
|
-
tcp_socket.close unless connected
|
|
270
|
-
end
|
|
271
|
-
|
|
272
|
-
# Class variable to track whether the TLS verification warning has been logged
|
|
273
|
-
@@ssl_verification_warning_logged = false # rubocop:disable Style/ClassVars
|
|
274
|
-
|
|
275
|
-
# Verification comes entirely from the caller's ssl_context, and a bare
|
|
276
|
-
# SSLContext.new verifies nothing, so warn (once per process) when the
|
|
277
|
-
# certificate or the hostname won't be checked.
|
|
278
|
-
def self.warn_if_unverified(ssl_context)
|
|
279
|
-
return if @@ssl_verification_warning_logged
|
|
280
|
-
|
|
281
|
-
problem = if !ssl_context.verify_mode.to_i.anybits?(::OpenSSL::SSL::VERIFY_PEER)
|
|
282
|
-
'verify_mode is VERIFY_NONE, so the server certificate is not checked'
|
|
283
|
-
elsif !ssl_context.verify_hostname
|
|
284
|
-
'verify_hostname is false, so the certificate is not checked against the hostname'
|
|
285
|
-
end
|
|
286
|
-
return unless problem
|
|
287
|
-
|
|
288
|
-
Dalli.logger.warn "SECURITY WARNING: Dalli's ssl_context #{problem}. " \
|
|
289
|
-
'Use ssl_context.set_params(verify_mode: OpenSSL::SSL::VERIFY_PEER, ' \
|
|
290
|
-
'verify_hostname: true) to verify the server.'
|
|
291
|
-
@@ssl_verification_warning_logged = true # rubocop:disable Style/ClassVars
|
|
292
214
|
end
|
|
293
215
|
end
|
|
294
216
|
|
data/lib/dalli/version.rb
CHANGED
data/lib/dalli.rb
CHANGED
|
@@ -31,10 +31,6 @@ module Dalli
|
|
|
31
31
|
# socket/server communication error that can be retried
|
|
32
32
|
class RetryableNetworkError < NetworkError; end
|
|
33
33
|
|
|
34
|
-
# a multi-get reply named a key that wasn't requested, so the rest of that
|
|
35
|
-
# server's replies can't be trusted
|
|
36
|
-
class UnexpectedReplyKeyError < DalliError; end
|
|
37
|
-
|
|
38
34
|
# Implements the NullObject pattern to store an application-defined value for 'Key not found' responses.
|
|
39
35
|
class NilObject; end # rubocop:disable Lint/EmptyClass
|
|
40
36
|
NOT_FOUND = NilObject.new
|
|
@@ -66,7 +62,6 @@ require_relative 'dalli/version'
|
|
|
66
62
|
require_relative 'dalli/instrumentation'
|
|
67
63
|
|
|
68
64
|
require_relative 'dalli/flags'
|
|
69
|
-
require_relative 'dalli/json_serializer'
|
|
70
65
|
require_relative 'dalli/compressor'
|
|
71
66
|
require_relative 'dalli/client'
|
|
72
67
|
require_relative 'dalli/key_manager'
|
data/lib/rack/session/dalli.rb
CHANGED
|
@@ -16,10 +16,11 @@ module Rack
|
|
|
16
16
|
attr_reader :data
|
|
17
17
|
|
|
18
18
|
# Don't freeze this until we fix the specs/implementation
|
|
19
|
-
# rubocop:disable
|
|
19
|
+
# rubocop:disable Style/MutableConstant
|
|
20
20
|
DEFAULT_DALLI_OPTIONS = {
|
|
21
21
|
namespace: 'rack:session'
|
|
22
22
|
}
|
|
23
|
+
# rubocop:enable Style/MutableConstant
|
|
23
24
|
|
|
24
25
|
# Brings in a new Rack::Session::Dalli middleware with the given
|
|
25
26
|
# `:memcache_server`. The server is either a hostname, or a
|