dalli 4.3.12 → 5.0.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 +23 -172
- data/Gemfile +1 -1
- data/README.md +4 -63
- data/lib/dalli/client.rb +39 -99
- data/lib/dalli/compressor.rb +5 -53
- data/lib/dalli/instrumentation.rb +1 -1
- data/lib/dalli/key_manager.rb +2 -28
- data/lib/dalli/options.rb +2 -2
- data/lib/dalli/pipelined_deleter.rb +3 -3
- data/lib/dalli/pipelined_getter.rb +31 -52
- data/lib/dalli/pipelined_setter.rb +11 -20
- data/lib/dalli/protocol/base.rb +32 -176
- data/lib/dalli/protocol/connection_manager.rb +20 -143
- data/lib/dalli/protocol/meta.rb +16 -55
- data/lib/dalli/protocol/{meta/request_formatter.rb → request_formatter.rb} +8 -21
- data/lib/dalli/protocol/response_buffer.rb +12 -27
- data/lib/dalli/protocol/{meta/response_processor.rb → response_processor.rb} +35 -100
- data/lib/dalli/protocol/server_config_parser.rb +1 -1
- 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 -4
- data/lib/dalli/protocol/value_serializer.rb +1 -1
- data/lib/dalli/protocol.rb +9 -7
- data/lib/dalli/ring.rb +2 -2
- data/lib/dalli/servers_arg_normalizer.rb +1 -1
- data/lib/dalli/socket.rb +5 -54
- data/lib/dalli/version.rb +2 -2
- data/lib/dalli.rb +0 -3
- data/lib/rack/session/dalli.rb +2 -1
- metadata +8 -18
- data/lib/dalli/json_serializer.rb +0 -28
- data/lib/dalli/protocol/binary/request_formatter.rb +0 -116
- data/lib/dalli/protocol/binary/response_header.rb +0 -36
- data/lib/dalli/protocol/binary/response_processor.rb +0 -259
- data/lib/dalli/protocol/binary/sasl_authentication.rb +0 -60
- data/lib/dalli/protocol/binary.rb +0 -202
- data/lib/dalli/protocol_deprecations.rb +0 -45
- /data/lib/dalli/protocol/{meta/key_regularizer.rb → key_regularizer.rb} +0 -0
data/lib/dalli/protocol/meta.rb
CHANGED
|
@@ -30,7 +30,7 @@ module Dalli
|
|
|
30
30
|
req = RequestFormatter.meta_get(key: encoded_key, base64: base64, skip_flags: skip_flags)
|
|
31
31
|
write(req)
|
|
32
32
|
@connection_manager.flush
|
|
33
|
-
response_processor.meta_get_with_value(cache_nils: cache_nils?(options)
|
|
33
|
+
response_processor.meta_get_with_value(cache_nils: cache_nils?(options))
|
|
34
34
|
end
|
|
35
35
|
|
|
36
36
|
def quiet_get_request(key)
|
|
@@ -47,7 +47,7 @@ module Dalli
|
|
|
47
47
|
req = RequestFormatter.meta_get(key: encoded_key, ttl: ttl, base64: base64, skip_flags: skip_flags)
|
|
48
48
|
write(req)
|
|
49
49
|
@connection_manager.flush
|
|
50
|
-
response_processor.meta_get_with_value(cache_nils: cache_nils?(options)
|
|
50
|
+
response_processor.meta_get_with_value(cache_nils: cache_nils?(options))
|
|
51
51
|
end
|
|
52
52
|
|
|
53
53
|
def touch(key, ttl)
|
|
@@ -61,13 +61,12 @@ module Dalli
|
|
|
61
61
|
|
|
62
62
|
# TODO: This is confusing, as there's a cas command in memcached
|
|
63
63
|
# and this isn't it. Maybe rename? Maybe eliminate?
|
|
64
|
-
def cas(key
|
|
64
|
+
def cas(key)
|
|
65
65
|
encoded_key, base64 = KeyRegularizer.encode(key)
|
|
66
|
-
req = RequestFormatter.meta_get(key: encoded_key, value: true, return_cas: true, base64: base64
|
|
67
|
-
skip_flags: raw_request?(options))
|
|
66
|
+
req = RequestFormatter.meta_get(key: encoded_key, value: true, return_cas: true, base64: base64)
|
|
68
67
|
write(req)
|
|
69
68
|
@connection_manager.flush
|
|
70
|
-
response_processor.meta_get_with_value_and_cas
|
|
69
|
+
response_processor.meta_get_with_value_and_cas
|
|
71
70
|
end
|
|
72
71
|
|
|
73
72
|
# Comprehensive meta get with support for all metadata flags.
|
|
@@ -97,7 +96,7 @@ module Dalli
|
|
|
97
96
|
def meta_get(key, options = {})
|
|
98
97
|
encoded_key, base64 = KeyRegularizer.encode(key)
|
|
99
98
|
req = RequestFormatter.meta_get(
|
|
100
|
-
key: encoded_key, value: true, return_cas: true, base64: base64,
|
|
99
|
+
key: encoded_key, value: true, return_cas: true, base64: base64,
|
|
101
100
|
vivify_ttl: options[:vivify_ttl], recache_ttl: options[:recache_ttl],
|
|
102
101
|
return_hit_status: options[:return_hit_status],
|
|
103
102
|
return_last_access: options[:return_last_access], skip_lru_bump: options[:skip_lru_bump]
|
|
@@ -106,7 +105,7 @@ module Dalli
|
|
|
106
105
|
@connection_manager.flush
|
|
107
106
|
response_processor.meta_get_with_metadata(
|
|
108
107
|
cache_nils: cache_nils?(options), return_hit_status: options[:return_hit_status],
|
|
109
|
-
return_last_access: options[:return_last_access]
|
|
108
|
+
return_last_access: options[:return_last_access]
|
|
110
109
|
)
|
|
111
110
|
end
|
|
112
111
|
|
|
@@ -147,7 +146,7 @@ module Dalli
|
|
|
147
146
|
response_processor.meta_set_with_cas unless quiet?
|
|
148
147
|
end
|
|
149
148
|
|
|
150
|
-
# rubocop:disable
|
|
149
|
+
# rubocop:disable Metrics/ParameterLists
|
|
151
150
|
def write_storage_req(mode, key, raw_value, ttl = nil, cas = nil, options = {}, quiet: quiet?)
|
|
152
151
|
(value, bitflags) = @value_marshaller.store(key, raw_value, options)
|
|
153
152
|
ttl = TtlSanitizer.sanitize(ttl) if ttl
|
|
@@ -160,6 +159,7 @@ module Dalli
|
|
|
160
159
|
write(TERMINATOR)
|
|
161
160
|
@connection_manager.flush unless quiet
|
|
162
161
|
end
|
|
162
|
+
# rubocop:enable Metrics/ParameterLists
|
|
163
163
|
|
|
164
164
|
def append(key, value)
|
|
165
165
|
write_append_prepend_req(:append, key, value)
|
|
@@ -171,7 +171,7 @@ module Dalli
|
|
|
171
171
|
response_processor.meta_set_append_prepend unless quiet?
|
|
172
172
|
end
|
|
173
173
|
|
|
174
|
-
# rubocop:disable
|
|
174
|
+
# rubocop:disable Metrics/ParameterLists
|
|
175
175
|
def write_append_prepend_req(mode, key, value, ttl = nil, cas = nil, _options = {})
|
|
176
176
|
ttl = TtlSanitizer.sanitize(ttl) if ttl
|
|
177
177
|
encoded_key, base64 = KeyRegularizer.encode(key)
|
|
@@ -182,6 +182,7 @@ module Dalli
|
|
|
182
182
|
write(TERMINATOR)
|
|
183
183
|
@connection_manager.flush unless quiet?
|
|
184
184
|
end
|
|
185
|
+
# rubocop:enable Metrics/ParameterLists
|
|
185
186
|
|
|
186
187
|
# Delete Commands
|
|
187
188
|
def delete(key, cas)
|
|
@@ -213,45 +214,12 @@ module Dalli
|
|
|
213
214
|
def decr_incr(incr, key, delta, ttl, initial)
|
|
214
215
|
ttl = initial ? TtlSanitizer.sanitize(ttl) : nil # Only set a TTL if we want to set a value on miss
|
|
215
216
|
encoded_key, base64 = KeyRegularizer.encode(key)
|
|
216
|
-
# memcached stores an item created by ma's auto-create (N/J) for a
|
|
217
|
-
# base64-encoded key without its binary-key flag, and later replies
|
|
218
|
-
# name it with the raw key bytes. Create such counters with an add.
|
|
219
|
-
return decr_incr_with_add(incr, key, delta, ttl, initial) if initial && base64
|
|
220
|
-
|
|
221
217
|
write(RequestFormatter.meta_arithmetic(key: encoded_key, delta: delta, initial: initial, incr: incr, ttl: ttl,
|
|
222
218
|
quiet: quiet?, base64: base64))
|
|
223
219
|
@connection_manager.flush unless quiet?
|
|
224
220
|
response_processor.decr_incr unless quiet?
|
|
225
221
|
end
|
|
226
222
|
|
|
227
|
-
# Like decr_incr with an initial value, without memcached's auto-create:
|
|
228
|
-
# on a miss the counter is added with the initial value (which is
|
|
229
|
-
# returned, as auto-create would), and the add losing a race to another
|
|
230
|
-
# client means the counter now exists, so the incr is tried again. In a
|
|
231
|
-
# quiet block both requests are sent and whichever applies takes effect.
|
|
232
|
-
def decr_incr_with_add(incr, key, delta, ttl, initial)
|
|
233
|
-
add_options = { raw: true } # counters are stored raw
|
|
234
|
-
return quiet_decr_incr_with_add(incr, key, delta, ttl, initial, add_options) if quiet?
|
|
235
|
-
|
|
236
|
-
2.times do
|
|
237
|
-
value = decr_incr(incr, key, delta, nil, nil)
|
|
238
|
-
return value unless value.nil?
|
|
239
|
-
|
|
240
|
-
write_storage_req(:add, key, initial.to_s, ttl, nil, add_options, quiet: false)
|
|
241
|
-
return initial if response_processor.meta_set_with_cas
|
|
242
|
-
end
|
|
243
|
-
decr_incr(incr, key, delta, nil, nil)
|
|
244
|
-
end
|
|
245
|
-
|
|
246
|
-
# rubocop:disable-next Metrics/ParameterLists
|
|
247
|
-
def quiet_decr_incr_with_add(incr, key, delta, ttl, initial, add_options)
|
|
248
|
-
encoded_key, base64 = KeyRegularizer.encode(key)
|
|
249
|
-
write(RequestFormatter.meta_arithmetic(key: encoded_key, delta: delta, initial: nil, incr: incr, ttl: nil,
|
|
250
|
-
quiet: true, base64: base64))
|
|
251
|
-
# Sent with the rest of the quiet block, by its closing noop
|
|
252
|
-
write_storage_req(:add, key, initial.to_s, ttl, nil, add_options, quiet: true)
|
|
253
|
-
end
|
|
254
|
-
|
|
255
223
|
# Other Commands
|
|
256
224
|
def flush(delay = 0)
|
|
257
225
|
write(RequestFormatter.flush(delay: delay))
|
|
@@ -261,12 +229,9 @@ module Dalli
|
|
|
261
229
|
|
|
262
230
|
# Noop is a keepalive operation but also used to demarcate the end of a set of pipelined commands.
|
|
263
231
|
# We need to read all the responses at once.
|
|
264
|
-
def noop
|
|
232
|
+
def noop
|
|
265
233
|
write_noop
|
|
266
|
-
|
|
267
|
-
# Everything up to and including the MN has been read
|
|
268
|
-
@connection_manager.clear_quiet_responses_pending!
|
|
269
|
-
result
|
|
234
|
+
response_processor.consume_all_responses_until_mn
|
|
270
235
|
end
|
|
271
236
|
|
|
272
237
|
def stats(info = nil)
|
|
@@ -292,13 +257,9 @@ module Dalli
|
|
|
292
257
|
@connection_manager.flush
|
|
293
258
|
end
|
|
294
259
|
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
require_relative 'meta/key_regularizer'
|
|
300
|
-
require_relative 'meta/request_formatter'
|
|
301
|
-
require_relative 'meta/response_processor'
|
|
260
|
+
require_relative 'key_regularizer'
|
|
261
|
+
require_relative 'request_formatter'
|
|
262
|
+
require_relative 'response_processor'
|
|
302
263
|
end
|
|
303
264
|
end
|
|
304
265
|
end
|
|
@@ -45,11 +45,10 @@ module Dalli
|
|
|
45
45
|
cmd << (skip_flags ? ' v' : ' v f') if value
|
|
46
46
|
cmd << ' c' if return_cas
|
|
47
47
|
cmd << ' b' if base64
|
|
48
|
-
cmd << " T#{
|
|
48
|
+
cmd << " T#{ttl}" if ttl
|
|
49
49
|
cmd << ' k q s' if quiet # Return the key in the response if quiet
|
|
50
|
-
cmd << " N#{
|
|
51
|
-
# Thundering herd: win recache if TTL below threshold
|
|
52
|
-
cmd << " R#{integer_flag(:recache_ttl, recache_ttl)}" if recache_ttl
|
|
50
|
+
cmd << " N#{vivify_ttl}" if vivify_ttl # Thundering herd: vivify on miss
|
|
51
|
+
cmd << " R#{recache_ttl}" if recache_ttl # Thundering herd: win recache if TTL below threshold
|
|
53
52
|
cmd << ' h' if return_hit_status # Return hit status (0 or 1)
|
|
54
53
|
cmd << ' l' if return_last_access # Return seconds since last access
|
|
55
54
|
cmd << ' u' if skip_lru_bump # Don't bump LRU or update access stats
|
|
@@ -62,7 +61,7 @@ module Dalli
|
|
|
62
61
|
cmd << ' b' if base64
|
|
63
62
|
cmd << " F#{bitflags}" if bitflags
|
|
64
63
|
cmd << cas_string(cas)
|
|
65
|
-
cmd << " T#{
|
|
64
|
+
cmd << " T#{ttl}" if ttl
|
|
66
65
|
cmd << " M#{mode_to_token(mode)}"
|
|
67
66
|
cmd << ' q' if quiet
|
|
68
67
|
cmd << TERMINATOR
|
|
@@ -75,7 +74,7 @@ module Dalli
|
|
|
75
74
|
cmd = "md #{key}"
|
|
76
75
|
cmd << ' b' if base64
|
|
77
76
|
cmd << cas_string(cas)
|
|
78
|
-
cmd << " T#{
|
|
77
|
+
cmd << " T#{ttl}" if ttl
|
|
79
78
|
cmd << ' I' if stale # Mark stale instead of deleting
|
|
80
79
|
cmd << ' q' if quiet
|
|
81
80
|
cmd + TERMINATOR
|
|
@@ -84,10 +83,10 @@ module Dalli
|
|
|
84
83
|
def self.meta_arithmetic(key:, delta:, initial:, incr: true, cas: nil, ttl: nil, base64: false, quiet: false)
|
|
85
84
|
cmd = "ma #{key} v"
|
|
86
85
|
cmd << ' b' if base64
|
|
87
|
-
cmd << " D#{
|
|
88
|
-
cmd << " J#{
|
|
86
|
+
cmd << " D#{delta}" if delta
|
|
87
|
+
cmd << " J#{initial}" if initial
|
|
89
88
|
# Always set a TTL if an initial value is specified
|
|
90
|
-
cmd << " N#{ttl
|
|
89
|
+
cmd << " N#{ttl || 0}" if ttl || initial
|
|
91
90
|
cmd << cas_string(cas)
|
|
92
91
|
cmd << ' q' if quiet
|
|
93
92
|
cmd << " M#{incr ? 'I' : 'D'}"
|
|
@@ -137,18 +136,6 @@ module Dalli
|
|
|
137
136
|
end
|
|
138
137
|
end
|
|
139
138
|
|
|
140
|
-
# Numeric flag values are written straight into the command line, so a
|
|
141
|
-
# value that isn't an integer -- e.g. a String carrying CRLF -- would let
|
|
142
|
-
# the caller inject further memcached commands (GHSA-6wmv-xq9m-fmp7).
|
|
143
|
-
# Converting to Integer means only digits ever reach the wire. Strings
|
|
144
|
-
# are parsed as base 10 so "010" means 10, as memcached would read it,
|
|
145
|
-
# rather than octal 8.
|
|
146
|
-
def self.integer_flag(name, value)
|
|
147
|
-
value.is_a?(String) ? Integer(value, 10) : Integer(value)
|
|
148
|
-
rescue ArgumentError, TypeError, FloatDomainError
|
|
149
|
-
raise ArgumentError, "#{name} must be an Integer, got #{value.inspect}"
|
|
150
|
-
end
|
|
151
|
-
|
|
152
139
|
def self.cas_string(cas)
|
|
153
140
|
cas = parse_to_64_bit_int(cas, nil)
|
|
154
141
|
cas.nil? || cas.zero? ? '' : " C#{cas}"
|
|
@@ -7,39 +7,38 @@ module Dalli
|
|
|
7
7
|
module Protocol
|
|
8
8
|
##
|
|
9
9
|
# Manages the buffer for responses from memcached.
|
|
10
|
-
# Uses an offset-based approach to avoid string allocations
|
|
11
|
-
# when advancing through parsed responses.
|
|
12
10
|
##
|
|
13
11
|
class ResponseBuffer
|
|
14
|
-
# Compact the buffer when the consumed portion exceeds this
|
|
15
|
-
# threshold and represents more than half the buffer
|
|
16
|
-
COMPACT_THRESHOLD = 4096
|
|
17
|
-
|
|
18
12
|
def initialize(io_source, response_processor)
|
|
19
13
|
@io_source = io_source
|
|
20
14
|
@response_processor = response_processor
|
|
21
15
|
@buffer = nil
|
|
22
|
-
@offset = 0
|
|
23
16
|
end
|
|
24
17
|
|
|
25
18
|
def read
|
|
26
19
|
@buffer << @io_source.read_nonblock
|
|
27
20
|
end
|
|
28
21
|
|
|
29
|
-
# Attempts to process a single response from the buffer
|
|
30
|
-
# advancing the
|
|
22
|
+
# Attempts to process a single response from the buffer. Starts
|
|
23
|
+
# by advancing the buffer to the specified start position
|
|
31
24
|
def process_single_getk_response
|
|
32
|
-
bytes, status, cas, key, value = @response_processor.getk_response_from_buffer(@buffer
|
|
33
|
-
|
|
34
|
-
compact_if_needed
|
|
25
|
+
bytes, status, cas, key, value = @response_processor.getk_response_from_buffer(@buffer)
|
|
26
|
+
advance(bytes)
|
|
35
27
|
[status, cas, key, value]
|
|
36
28
|
end
|
|
37
29
|
|
|
30
|
+
# Advances the internal response buffer by bytes_to_advance
|
|
31
|
+
# bytes. The
|
|
32
|
+
def advance(bytes_to_advance)
|
|
33
|
+
return unless bytes_to_advance.positive?
|
|
34
|
+
|
|
35
|
+
@buffer = @buffer.byteslice(bytes_to_advance..-1)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
38
|
# Resets the internal buffer to an empty state,
|
|
39
39
|
# so that we're ready to read pipelined responses
|
|
40
40
|
def reset
|
|
41
41
|
@buffer = ''.b
|
|
42
|
-
@offset = 0
|
|
43
42
|
end
|
|
44
43
|
|
|
45
44
|
# Ensures the buffer is initialized for reading without discarding
|
|
@@ -49,30 +48,16 @@ module Dalli
|
|
|
49
48
|
return if in_progress?
|
|
50
49
|
|
|
51
50
|
@buffer = ''.b
|
|
52
|
-
@offset = 0
|
|
53
51
|
end
|
|
54
52
|
|
|
55
53
|
# Clear the internal response buffer
|
|
56
54
|
def clear
|
|
57
55
|
@buffer = nil
|
|
58
|
-
@offset = 0
|
|
59
56
|
end
|
|
60
57
|
|
|
61
58
|
def in_progress?
|
|
62
59
|
!@buffer.nil?
|
|
63
60
|
end
|
|
64
|
-
|
|
65
|
-
private
|
|
66
|
-
|
|
67
|
-
# Only compact when we've consumed a significant portion of the buffer.
|
|
68
|
-
# This avoids per-response string allocation while preventing unbounded
|
|
69
|
-
# memory growth for large pipelines.
|
|
70
|
-
def compact_if_needed
|
|
71
|
-
return unless @offset > COMPACT_THRESHOLD && @offset > @buffer.bytesize / 2
|
|
72
|
-
|
|
73
|
-
@buffer = @buffer.byteslice(@offset..)
|
|
74
|
-
@offset = 0
|
|
75
|
-
end
|
|
76
61
|
end
|
|
77
62
|
end
|
|
78
63
|
end
|
|
@@ -9,9 +9,6 @@ module Dalli
|
|
|
9
9
|
# and parsing into local values. Handles errors on unexpected values.
|
|
10
10
|
##
|
|
11
11
|
class ResponseProcessor
|
|
12
|
-
MAX_HEADER_BYTES = 8 * 1024
|
|
13
|
-
MAX_STATS_ENTRIES = 10_000
|
|
14
|
-
MAX_QUIET_RESPONSES = 100_000
|
|
15
12
|
EN = 'EN'
|
|
16
13
|
END_TOKEN = 'END'
|
|
17
14
|
EX = 'EX'
|
|
@@ -31,25 +28,22 @@ module Dalli
|
|
|
31
28
|
@value_marshaller = value_marshaller
|
|
32
29
|
end
|
|
33
30
|
|
|
34
|
-
|
|
35
|
-
# ignoring any flags in the reply. The request didn't ask for flags, so
|
|
36
|
-
# a reply carrying them anyway must not get the value deserialized.
|
|
37
|
-
def meta_get_with_value(cache_nils: false, raw: false)
|
|
31
|
+
def meta_get_with_value(cache_nils: false)
|
|
38
32
|
tokens = error_on_unexpected!([VA, EN, HD])
|
|
39
33
|
return cache_nils ? ::Dalli::NOT_FOUND : nil if tokens.first == EN
|
|
40
34
|
return true unless tokens.first == VA
|
|
41
35
|
|
|
42
|
-
retrieve(read_data(tokens[1].to_i),
|
|
36
|
+
@value_marshaller.retrieve(read_data(tokens[1].to_i), bitflags_from_tokens(tokens))
|
|
43
37
|
end
|
|
44
38
|
|
|
45
|
-
def meta_get_with_value_and_cas
|
|
39
|
+
def meta_get_with_value_and_cas
|
|
46
40
|
tokens = error_on_unexpected!([VA, EN, HD])
|
|
47
41
|
return [nil, 0] if tokens.first == EN
|
|
48
42
|
|
|
49
43
|
cas = cas_from_tokens(tokens)
|
|
50
44
|
return [nil, cas] unless tokens.first == VA
|
|
51
45
|
|
|
52
|
-
[retrieve(read_data(tokens[1].to_i),
|
|
46
|
+
[@value_marshaller.retrieve(read_data(tokens[1].to_i), bitflags_from_tokens(tokens)), cas]
|
|
53
47
|
end
|
|
54
48
|
|
|
55
49
|
def meta_get_without_value
|
|
@@ -68,13 +62,12 @@ module Dalli
|
|
|
68
62
|
#
|
|
69
63
|
# Used by meta_get for comprehensive metadata retrieval.
|
|
70
64
|
# Supports thundering herd protection (N/R flags) and metadata flags (h/l/u).
|
|
71
|
-
def meta_get_with_metadata(cache_nils: false, return_hit_status: false, return_last_access: false
|
|
72
|
-
raw: false)
|
|
65
|
+
def meta_get_with_metadata(cache_nils: false, return_hit_status: false, return_last_access: false)
|
|
73
66
|
tokens = error_on_unexpected!([VA, EN, HD])
|
|
74
67
|
result = build_metadata_result(tokens)
|
|
75
68
|
result[:hit_before] = hit_status_from_tokens(tokens) if return_hit_status
|
|
76
69
|
result[:last_access] = last_access_from_tokens(tokens) if return_last_access
|
|
77
|
-
result[:value] = parse_value_from_tokens(tokens, cache_nils
|
|
70
|
+
result[:value] = parse_value_from_tokens(tokens, cache_nils)
|
|
78
71
|
result
|
|
79
72
|
end
|
|
80
73
|
|
|
@@ -86,16 +79,11 @@ module Dalli
|
|
|
86
79
|
}
|
|
87
80
|
end
|
|
88
81
|
|
|
89
|
-
def parse_value_from_tokens(tokens, cache_nils
|
|
82
|
+
def parse_value_from_tokens(tokens, cache_nils)
|
|
90
83
|
return cache_nils ? ::Dalli::NOT_FOUND : nil if tokens.first == EN
|
|
91
84
|
return unless tokens.first == VA
|
|
92
85
|
|
|
93
|
-
retrieve(read_data(tokens[1].to_i),
|
|
94
|
-
end
|
|
95
|
-
|
|
96
|
-
# Values from a raw request are returned as stored
|
|
97
|
-
def retrieve(value, bitflags, raw)
|
|
98
|
-
raw ? value : @value_marshaller.retrieve(value, bitflags)
|
|
86
|
+
@value_marshaller.retrieve(read_data(tokens[1].to_i), bitflags_from_tokens(tokens))
|
|
99
87
|
end
|
|
100
88
|
|
|
101
89
|
def meta_set_with_cas
|
|
@@ -128,11 +116,7 @@ module Dalli
|
|
|
128
116
|
def stats
|
|
129
117
|
tokens = error_on_unexpected!([END_TOKEN, STAT])
|
|
130
118
|
values = {}
|
|
131
|
-
replies = 0
|
|
132
119
|
while tokens.first != END_TOKEN
|
|
133
|
-
replies += 1
|
|
134
|
-
raise Dalli::DalliError, 'Too many stats replies' if replies > MAX_STATS_ENTRIES
|
|
135
|
-
|
|
136
120
|
values[tokens[1]] = tokens[2]
|
|
137
121
|
tokens = next_line_to_tokens
|
|
138
122
|
end
|
|
@@ -156,24 +140,22 @@ module Dalli
|
|
|
156
140
|
tokens.last
|
|
157
141
|
end
|
|
158
142
|
|
|
159
|
-
def consume_all_responses_until_mn
|
|
143
|
+
def consume_all_responses_until_mn
|
|
160
144
|
tokens = next_line_to_tokens
|
|
161
145
|
|
|
162
|
-
|
|
163
|
-
while tokens.first != MN
|
|
164
|
-
replies += 1
|
|
165
|
-
raise Dalli::DalliError, 'Too many quiet replies' if replies > max_responses
|
|
166
|
-
|
|
167
|
-
tokens = next_line_to_tokens
|
|
168
|
-
end
|
|
146
|
+
tokens = next_line_to_tokens while tokens.first != MN
|
|
169
147
|
true
|
|
170
148
|
end
|
|
171
149
|
|
|
172
|
-
def
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
150
|
+
def tokens_from_header_buffer(buf)
|
|
151
|
+
header = header_from_buffer(buf)
|
|
152
|
+
tokens = header.split
|
|
153
|
+
header_len = header.bytesize + TERMINATOR.length
|
|
154
|
+
body_len = body_len_from_tokens(tokens)
|
|
155
|
+
[tokens, header_len, body_len]
|
|
156
|
+
end
|
|
176
157
|
|
|
158
|
+
def full_response_from_buffer(tokens, body, resp_size)
|
|
177
159
|
value = @value_marshaller.retrieve(body, bitflags_from_tokens(tokens))
|
|
178
160
|
[resp_size, tokens.first == VA, cas_from_tokens(tokens), key_from_tokens(tokens), value]
|
|
179
161
|
end
|
|
@@ -188,67 +170,35 @@ module Dalli
|
|
|
188
170
|
# The remaining three values in the array are the ResponseHeader,
|
|
189
171
|
# key, and value.
|
|
190
172
|
##
|
|
191
|
-
def getk_response_from_buffer(buf
|
|
192
|
-
#
|
|
193
|
-
|
|
194
|
-
check_header_size!(buf, offset, term_idx)
|
|
195
|
-
return [0, nil, nil, nil, nil] unless term_idx
|
|
173
|
+
def getk_response_from_buffer(buf)
|
|
174
|
+
# There's no header in the buffer, so don't advance
|
|
175
|
+
return [0, nil, nil, nil, nil] unless contains_header?(buf)
|
|
196
176
|
|
|
197
|
-
|
|
198
|
-
tokens = header.split
|
|
199
|
-
header_len = header.bytesize + TERMINATOR.length
|
|
200
|
-
body_len = body_len_from_tokens(tokens)
|
|
177
|
+
tokens, header_len, body_len = tokens_from_header_buffer(buf)
|
|
201
178
|
|
|
202
|
-
#
|
|
203
|
-
#
|
|
204
|
-
#
|
|
205
|
-
#
|
|
206
|
-
|
|
207
|
-
# later commands to read as their own. A hit on an empty value (VA 0)
|
|
208
|
-
# still has a body -- just its terminator -- so it's parsed below.
|
|
209
|
-
return [header_len, tokens.first == MN, nil, nil, nil] if no_body?(tokens, body_len)
|
|
179
|
+
# We have a complete response that has no body.
|
|
180
|
+
# This is either the response to the terminating
|
|
181
|
+
# noop or, if the status is not MN, an intermediate
|
|
182
|
+
# error response that needs to be discarded.
|
|
183
|
+
return [header_len, true, nil, nil, nil] if body_len.zero?
|
|
210
184
|
|
|
211
185
|
resp_size = header_len + body_len + TERMINATOR.length
|
|
212
186
|
# The header is in the buffer, but the body is not. As we don't have
|
|
213
187
|
# a complete response, don't advance the buffer
|
|
214
|
-
return [0, nil, nil, nil, nil] unless buf.bytesize >=
|
|
215
|
-
|
|
216
|
-
term = offset + header_len + body_len
|
|
217
|
-
raise_bad_value_terminator unless buf.getbyte(term) == 13 && buf.getbyte(term + 1) == 10
|
|
188
|
+
return [0, nil, nil, nil, nil] unless buf.bytesize >= resp_size
|
|
218
189
|
|
|
219
190
|
# The full response is in our buffer, so parse it and return
|
|
220
191
|
# the values
|
|
221
|
-
body = buf.
|
|
192
|
+
body = buf.slice(header_len, body_len)
|
|
222
193
|
full_response_from_buffer(tokens, body, resp_size)
|
|
223
194
|
end
|
|
224
195
|
|
|
225
|
-
def
|
|
226
|
-
|
|
227
|
-
return if size + TERMINATOR.bytesize <= MAX_HEADER_BYTES
|
|
228
|
-
|
|
229
|
-
raise Dalli::DalliError, 'Reply header exceeds 8 KiB'
|
|
196
|
+
def contains_header?(buf)
|
|
197
|
+
buf.include?(TERMINATOR)
|
|
230
198
|
end
|
|
231
199
|
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
# buffer wait for (and accumulate) that many bytes.
|
|
235
|
-
MAX_VALUE_BYTES = 1024 * 1024 * 1024
|
|
236
|
-
|
|
237
|
-
# The size given right after "VA", or 0 when it isn't a number
|
|
238
|
-
def positional_size(tokens)
|
|
239
|
-
tokens.first == VA && tokens[1]&.match?(/\A\d+\z/) ? tokens[1].to_i : 0
|
|
240
|
-
end
|
|
241
|
-
|
|
242
|
-
def check_value_size!(size)
|
|
243
|
-
limit = @value_marshaller.max_response_bytes
|
|
244
|
-
return if size >= 0 && size <= MAX_VALUE_BYTES && size <= limit # rubocop:disable Style/ComparableBetween
|
|
245
|
-
|
|
246
|
-
raise Dalli::DalliError, "Reply value size #{size} is out of range"
|
|
247
|
-
end
|
|
248
|
-
|
|
249
|
-
# A zero-size reply has no body unless it's a VA (a hit on an empty value)
|
|
250
|
-
def no_body?(tokens, body_len)
|
|
251
|
-
body_len.zero? && tokens.first != VA
|
|
200
|
+
def header_from_buffer(buf)
|
|
201
|
+
buf.split(TERMINATOR, 2).first
|
|
252
202
|
end
|
|
253
203
|
|
|
254
204
|
def error_on_unexpected!(expected_codes)
|
|
@@ -291,11 +241,7 @@ module Dalli
|
|
|
291
241
|
end
|
|
292
242
|
|
|
293
243
|
def body_len_from_tokens(tokens)
|
|
294
|
-
|
|
295
|
-
# when there's no s flag rather than reading the value as replies
|
|
296
|
-
size = tokens.any? { |t| t.start_with?('s') } ? value_from_tokens(tokens, 's')&.to_i : positional_size(tokens)
|
|
297
|
-
check_value_size!(size) if size
|
|
298
|
-
size
|
|
244
|
+
value_from_tokens(tokens, 's')&.to_i
|
|
299
245
|
end
|
|
300
246
|
|
|
301
247
|
def value_from_tokens(tokens, flag)
|
|
@@ -314,19 +260,8 @@ module Dalli
|
|
|
314
260
|
line&.split || []
|
|
315
261
|
end
|
|
316
262
|
|
|
317
|
-
def strip_value_terminator!(data)
|
|
318
|
-
data&.chomp!(TERMINATOR) || raise_bad_value_terminator
|
|
319
|
-
end
|
|
320
|
-
|
|
321
|
-
def raise_bad_value_terminator
|
|
322
|
-
raise Dalli::DalliError, "Reply value isn't followed by CRLF, so its size doesn't match the value"
|
|
323
|
-
end
|
|
324
|
-
|
|
325
263
|
def read_data(data_size)
|
|
326
|
-
|
|
327
|
-
# read 1 or 0 bytes and leave the connection out of step
|
|
328
|
-
check_value_size!(data_size)
|
|
329
|
-
strip_value_terminator!(@io_source.read(data_size + TERMINATOR.bytesize))
|
|
264
|
+
@io_source.read(data_size + TERMINATOR.bytesize)&.chomp!(TERMINATOR)
|
|
330
265
|
end
|
|
331
266
|
end
|
|
332
267
|
end
|
|
@@ -6,7 +6,7 @@ module Dalli
|
|
|
6
6
|
module Protocol
|
|
7
7
|
##
|
|
8
8
|
# Dalli::Protocol::ServerConfigParser parses a server string passed to
|
|
9
|
-
# a Dalli::Protocol::
|
|
9
|
+
# a Dalli::Protocol::Meta instance into the hostname, port, weight, and
|
|
10
10
|
# socket_type.
|
|
11
11
|
##
|
|
12
12
|
class ServerConfigParser
|
|
@@ -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
|
# https://www.hjp.at/zettel/m/memcached_flags.rxml
|
|
30
24
|
# Looks like most clients use bit 1 to indicate gzip compression.
|
|
31
25
|
FLAG_COMPRESSED = 0x2
|
|
@@ -44,16 +38,8 @@ module Dalli
|
|
|
44
38
|
end
|
|
45
39
|
|
|
46
40
|
def retrieve(value, bitflags)
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
max_bytes = @compression_options[:decompressed_max_bytes]
|
|
50
|
-
# Custom compressors that only define decompress(data) keep working,
|
|
51
|
-
# without the limit
|
|
52
|
-
if max_bytes && compressor_accepts_max_bytes?
|
|
53
|
-
compressor.decompress(value, max_bytes: max_bytes)
|
|
54
|
-
else
|
|
55
|
-
compressor.decompress(value)
|
|
56
|
-
end
|
|
41
|
+
compressed = bitflags.anybits?(FLAG_COMPRESSED)
|
|
42
|
+
compressed ? compressor.decompress(value) : value
|
|
57
43
|
|
|
58
44
|
# TODO: We likely want to move this rescue into the Dalli::Compressor / Dalli::GzipCompressor
|
|
59
45
|
# itself, since not all compressors necessarily use Zlib. For now keep it here, so the behavior
|
|
@@ -62,19 +48,6 @@ module Dalli
|
|
|
62
48
|
raise UnmarshalError, "Unable to uncompress value: #{$ERROR_INFO.message}"
|
|
63
49
|
end
|
|
64
50
|
|
|
65
|
-
def compressor_accepts_max_bytes?
|
|
66
|
-
return @compressor_accepts_max_bytes if defined?(@compressor_accepts_max_bytes)
|
|
67
|
-
|
|
68
|
-
@compressor_accepts_max_bytes =
|
|
69
|
-
begin
|
|
70
|
-
compressor.method(:decompress).parameters.any? do |type, name|
|
|
71
|
-
name == :max_bytes && KEYWORD_PARAMETER_TYPES.include?(type)
|
|
72
|
-
end
|
|
73
|
-
rescue NameError # an object without a reflectable decompress method
|
|
74
|
-
false
|
|
75
|
-
end
|
|
76
|
-
end
|
|
77
|
-
|
|
78
51
|
def compress_by_default?
|
|
79
52
|
@compression_options[:compress]
|
|
80
53
|
end
|
|
@@ -88,13 +61,11 @@ module Dalli
|
|
|
88
61
|
end
|
|
89
62
|
|
|
90
63
|
# Checks whether we should apply compression when serializing a value
|
|
91
|
-
# based on the specified options. Returns false
|
|
92
|
-
#
|
|
93
|
-
#
|
|
94
|
-
#
|
|
95
|
-
# falling back to the server default.
|
|
64
|
+
# based on the specified options. Returns false unless the value
|
|
65
|
+
# is greater than the minimum compression size. Otherwise returns
|
|
66
|
+
# based on a method-level option if specified, falling back to the
|
|
67
|
+
# server default.
|
|
96
68
|
def compress_value?(value, req_options)
|
|
97
|
-
return false if req_options&.dig(:raw)
|
|
98
69
|
return false unless value.bytesize >= compression_min_size
|
|
99
70
|
return compress_by_default? unless req_options && !req_options[:compress].nil?
|
|
100
71
|
|
|
@@ -28,8 +28,6 @@ module Dalli
|
|
|
28
28
|
|
|
29
29
|
@marshal_options =
|
|
30
30
|
DEFAULTS.merge(client_options.slice(*OPTIONS))
|
|
31
|
-
@max_response_bytes = Integer(client_options.fetch(:max_response_bytes, value_max_bytes))
|
|
32
|
-
raise ArgumentError, 'max_response_bytes must be positive' unless @max_response_bytes.positive?
|
|
33
31
|
end
|
|
34
32
|
|
|
35
33
|
def store(key, value, options = nil)
|
|
@@ -50,8 +48,6 @@ module Dalli
|
|
|
50
48
|
@marshal_options[:value_max_bytes]
|
|
51
49
|
end
|
|
52
50
|
|
|
53
|
-
attr_reader :max_response_bytes
|
|
54
|
-
|
|
55
51
|
def error_if_over_max_value_bytes(key, value)
|
|
56
52
|
return if value.bytesize <= value_max_bytes
|
|
57
53
|
|