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
|
@@ -26,9 +26,6 @@ module Dalli
|
|
|
26
26
|
|
|
27
27
|
attr_accessor :hostname, :port, :socket_type, :options
|
|
28
28
|
attr_reader :sock
|
|
29
|
-
# The buffer replies are read into (see Base#response_buffer). It's
|
|
30
|
-
# cleared with the write buffer whenever the connection closes.
|
|
31
|
-
attr_writer :response_buffer
|
|
32
29
|
|
|
33
30
|
def initialize(hostname, port, socket_type, client_options)
|
|
34
31
|
@hostname = hostname
|
|
@@ -39,11 +36,7 @@ module Dalli
|
|
|
39
36
|
@quiet_responses_pending = false
|
|
40
37
|
@sock = nil
|
|
41
38
|
@pid = nil
|
|
42
|
-
@write_buffer = []
|
|
43
|
-
@write_buffer_bytes = 0
|
|
44
|
-
@response_buffer = nil
|
|
45
39
|
|
|
46
|
-
@fail_count = 0
|
|
47
40
|
reset_down_info
|
|
48
41
|
end
|
|
49
42
|
|
|
@@ -59,13 +52,12 @@ module Dalli
|
|
|
59
52
|
Dalli.logger.debug { "Dalli::Server#connect #{name}" }
|
|
60
53
|
|
|
61
54
|
@sock = memcached_socket
|
|
62
|
-
#
|
|
63
|
-
@sock.sync = true
|
|
55
|
+
@sock.sync = false # Enable buffered I/O for better performance
|
|
64
56
|
@pid = Process.pid
|
|
65
57
|
@request_in_progress = false
|
|
66
58
|
@quiet_responses_pending = false
|
|
67
|
-
rescue SystemCallError, *TIMEOUT_ERRORS,
|
|
68
|
-
# SocketError = DNS resolution failure
|
|
59
|
+
rescue SystemCallError, *TIMEOUT_ERRORS, EOFError, SocketError => e
|
|
60
|
+
# SocketError = DNS resolution failure
|
|
69
61
|
error_on_request!(e)
|
|
70
62
|
end
|
|
71
63
|
|
|
@@ -93,8 +85,6 @@ module Dalli
|
|
|
93
85
|
def down!
|
|
94
86
|
close
|
|
95
87
|
log_down_detected
|
|
96
|
-
# Once down_retry_delay passes, the server gets a full set of attempts
|
|
97
|
-
@fail_count = 0
|
|
98
88
|
|
|
99
89
|
@error = $ERROR_INFO&.class&.name
|
|
100
90
|
@msg ||= $ERROR_INFO&.message
|
|
@@ -124,7 +114,7 @@ module Dalli
|
|
|
124
114
|
return unless @sock
|
|
125
115
|
|
|
126
116
|
begin
|
|
127
|
-
|
|
117
|
+
@sock.close
|
|
128
118
|
rescue StandardError
|
|
129
119
|
nil
|
|
130
120
|
ensure
|
|
@@ -136,19 +126,10 @@ module Dalli
|
|
|
136
126
|
@sock = nil
|
|
137
127
|
@pid = nil
|
|
138
128
|
@quiet_responses_pending = false
|
|
139
|
-
discard_write_buffer
|
|
140
|
-
@response_buffer&.clear
|
|
141
129
|
abort_request!
|
|
142
130
|
end
|
|
143
131
|
end
|
|
144
132
|
|
|
145
|
-
# A forked child shares the parent's connection, so it closes only its
|
|
146
|
-
# own file descriptor. Closing the TLS socket itself would send
|
|
147
|
-
# close_notify and end the parent's TLS session.
|
|
148
|
-
def close_socket
|
|
149
|
-
fork_detected? ? @sock.to_io.close : @sock.close
|
|
150
|
-
end
|
|
151
|
-
|
|
152
133
|
def connected?
|
|
153
134
|
!@sock.nil?
|
|
154
135
|
end
|
|
@@ -182,33 +163,15 @@ module Dalli
|
|
|
182
163
|
raise '[Dalli] No request in progress. This may be a bug in Dalli.' unless @request_in_progress
|
|
183
164
|
|
|
184
165
|
@request_in_progress = false
|
|
185
|
-
# A completed request is what proves the server healthy again, so the
|
|
186
|
-
# failure count resets here rather than on reconnect: a server that
|
|
187
|
-
# accepts connections but never answers would otherwise reset it on
|
|
188
|
-
# every retry and never reach socket_max_failures.
|
|
189
|
-
@fail_count = 0
|
|
190
166
|
end
|
|
191
167
|
|
|
192
168
|
def abort_request!
|
|
193
169
|
@request_in_progress = false
|
|
194
170
|
end
|
|
195
171
|
|
|
196
|
-
# The longest reply line Dalli reads, in bytes, CRLF included. A meta
|
|
197
|
-
# reply header is at most about 500 bytes: "VA <size>" plus its flags,
|
|
198
|
-
# the largest being a 250-byte key base64-encoded to 336 bytes, a
|
|
199
|
-
# 20-digit CAS, a 32-byte opaque and a few numeric fields. The longest
|
|
200
|
-
# stats line (stats settings) carries a file path such as ext_path, up
|
|
201
|
-
# to PATH_MAX (4096). 8 KiB leaves room for both, while a server that
|
|
202
|
-
# never sends CRLF can't grow the read buffer without bound.
|
|
203
|
-
MAX_LINE_BYTES = 8 * 1024
|
|
204
|
-
|
|
205
172
|
def read_line
|
|
206
|
-
|
|
207
|
-
data = @sock.gets("\r\n", MAX_LINE_BYTES)
|
|
173
|
+
data = @sock.gets("\r\n")
|
|
208
174
|
error_on_request!('EOF in read_line') if data.nil?
|
|
209
|
-
# Without its CRLF the line either ran past MAX_LINE_BYTES or was cut
|
|
210
|
-
# short by EOF; either way the connection is out of step
|
|
211
|
-
error_on_request!("Reply line without CRLF in the first #{MAX_LINE_BYTES} bytes") unless data.end_with?("\r\n")
|
|
212
175
|
data
|
|
213
176
|
rescue SystemCallError, *TIMEOUT_ERRORS, *SSL_ERRORS, EOFError => e
|
|
214
177
|
error_on_request!(e)
|
|
@@ -216,29 +179,14 @@ module Dalli
|
|
|
216
179
|
|
|
217
180
|
# JRuby doesn't support IO#timeout=, so use custom readfull implementation
|
|
218
181
|
# CRuby 3.3+ has IO#timeout= which makes IO#read work with timeouts
|
|
219
|
-
# memcached can't store an item larger than 1 GiB (its -I maximum), so a
|
|
220
|
-
# reply claiming more (or a negative size) is malformed or hostile.
|
|
221
|
-
# IO#read allocates the full count up front, so check before reading.
|
|
222
|
-
MAX_READ_BYTES = (1024 * 1024 * 1024) + 2 # plus the value's trailing "\r\n"
|
|
223
|
-
|
|
224
|
-
def check_read_size!(count)
|
|
225
|
-
return if count.between?(0, MAX_READ_BYTES)
|
|
226
|
-
|
|
227
|
-
raise Dalli::DalliError, "Reply size #{count} from #{name} is out of range"
|
|
228
|
-
end
|
|
229
|
-
|
|
230
182
|
if RUBY_ENGINE == 'jruby'
|
|
231
183
|
def read(count)
|
|
232
|
-
check_read_size!(count)
|
|
233
|
-
flush_write_buffer
|
|
234
184
|
@sock.readfull(count)
|
|
235
185
|
rescue SystemCallError, *TIMEOUT_ERRORS, *SSL_ERRORS, EOFError => e
|
|
236
186
|
error_on_request!(e)
|
|
237
187
|
end
|
|
238
188
|
else
|
|
239
189
|
def read(count)
|
|
240
|
-
check_read_size!(count)
|
|
241
|
-
flush_write_buffer
|
|
242
190
|
read_bytes(count)
|
|
243
191
|
rescue SystemCallError, *TIMEOUT_ERRORS, *SSL_ERRORS, EOFError => e
|
|
244
192
|
error_on_request!(e)
|
|
@@ -249,34 +197,22 @@ module Dalli
|
|
|
249
197
|
# at the call site.
|
|
250
198
|
alias read_exact read
|
|
251
199
|
|
|
252
|
-
# Requests are buffered here rather than in the socket's own IO buffer
|
|
253
|
-
# (the socket is sync). Ruby flushes an IO's write buffer when any process
|
|
254
|
-
# holding it closes or finalizes the IO, so after a fork a child could
|
|
255
|
-
# send bytes the parent had buffered but not sent yet: a quiet write would
|
|
256
|
-
# run twice, or the parent's replies would shift onto the wrong requests.
|
|
257
|
-
# A forked child discards this buffer instead. Like IO's own buffer, it
|
|
258
|
-
# is sent once it grows past this size, or when a reply is read.
|
|
259
|
-
WRITE_BUFFER_FLUSH_BYTES = 64 * 1024
|
|
260
|
-
|
|
261
200
|
def write(bytes)
|
|
262
|
-
@
|
|
263
|
-
@write_buffer_bytes += bytes.bytesize
|
|
264
|
-
flush_write_buffer if @write_buffer_bytes >= WRITE_BUFFER_FLUSH_BYTES
|
|
265
|
-
bytes.bytesize
|
|
201
|
+
@sock.write(bytes)
|
|
266
202
|
rescue SystemCallError, *TIMEOUT_ERRORS, *SSL_ERRORS, IOError => e
|
|
267
203
|
error_on_request!(e)
|
|
268
204
|
end
|
|
269
205
|
|
|
270
206
|
def flush
|
|
271
|
-
|
|
207
|
+
@sock.flush
|
|
272
208
|
rescue SystemCallError, *TIMEOUT_ERRORS, *SSL_ERRORS, IOError => e
|
|
273
209
|
error_on_request!(e)
|
|
274
210
|
end
|
|
275
211
|
|
|
276
212
|
def flushed_write(bytes)
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
213
|
+
written = @sock.write(bytes)
|
|
214
|
+
@sock.flush
|
|
215
|
+
written
|
|
280
216
|
rescue SystemCallError, *TIMEOUT_ERRORS, *SSL_ERRORS, IOError => e
|
|
281
217
|
error_on_request!(e)
|
|
282
218
|
end
|
|
@@ -284,7 +220,6 @@ module Dalli
|
|
|
284
220
|
# Non-blocking read. Here to support the operation
|
|
285
221
|
# of the get_multi operation
|
|
286
222
|
def read_available(...)
|
|
287
|
-
flush_write_buffer
|
|
288
223
|
@sock.read_available(...)
|
|
289
224
|
end
|
|
290
225
|
|
|
@@ -311,9 +246,8 @@ module Dalli
|
|
|
311
246
|
raise Dalli::RetryableNetworkError, message
|
|
312
247
|
end
|
|
313
248
|
|
|
314
|
-
# Called on connect. Deliberately leaves @fail_count alone; see
|
|
315
|
-
# finish_request!.
|
|
316
249
|
def reset_down_info
|
|
250
|
+
@fail_count = 0
|
|
317
251
|
@down_at = nil
|
|
318
252
|
@last_down_at = nil
|
|
319
253
|
@msg = nil
|
|
@@ -338,8 +272,7 @@ module Dalli
|
|
|
338
272
|
def reconnect_on_fork
|
|
339
273
|
message = 'Fork detected, re-connecting child process...'
|
|
340
274
|
Dalli.logger.info { message }
|
|
341
|
-
#
|
|
342
|
-
# without touching the parent's connection, and reconnects immediately
|
|
275
|
+
# Close socket on a fork and reconnect immediately
|
|
343
276
|
close
|
|
344
277
|
establish_connection
|
|
345
278
|
end
|
|
@@ -369,37 +302,6 @@ module Dalli
|
|
|
369
302
|
|
|
370
303
|
private
|
|
371
304
|
|
|
372
|
-
# One write per flush. A TLS socket sends each argument of
|
|
373
|
-
# write(*parts) as its own record and system call, so the parts are
|
|
374
|
-
# joined first.
|
|
375
|
-
def flush_write_buffer
|
|
376
|
-
return if @write_buffer.empty?
|
|
377
|
-
|
|
378
|
-
@sock.write(@write_buffer.size == 1 ? @write_buffer.first : joined_write_buffer)
|
|
379
|
-
discard_write_buffer
|
|
380
|
-
end
|
|
381
|
-
|
|
382
|
-
# Joined as bytes: requests can mix UTF-8 and binary strings, which
|
|
383
|
-
# String#<< refuses to combine when both hold non-ASCII bytes
|
|
384
|
-
if String.method_defined?(:append_as_bytes)
|
|
385
|
-
def joined_write_buffer
|
|
386
|
-
out = String.new(capacity: @write_buffer_bytes, encoding: Encoding::BINARY)
|
|
387
|
-
@write_buffer.each { |part| out.append_as_bytes(part) }
|
|
388
|
-
out
|
|
389
|
-
end
|
|
390
|
-
else
|
|
391
|
-
def joined_write_buffer
|
|
392
|
-
out = String.new(capacity: @write_buffer_bytes, encoding: Encoding::BINARY)
|
|
393
|
-
@write_buffer.each { |part| out << part.b }
|
|
394
|
-
out
|
|
395
|
-
end
|
|
396
|
-
end
|
|
397
|
-
|
|
398
|
-
def discard_write_buffer
|
|
399
|
-
@write_buffer.clear
|
|
400
|
-
@write_buffer_bytes = 0
|
|
401
|
-
end
|
|
402
|
-
|
|
403
305
|
# Reads exactly `count` bytes. IO#read(count) on a blocking socket blocks
|
|
404
306
|
# until it has `count` bytes, accumulating across TCP chunks internally,
|
|
405
307
|
# and only hands back a shorter (or nil) buffer when the stream hits EOF.
|
data/lib/dalli/protocol/meta.rb
CHANGED
|
@@ -38,20 +38,20 @@ module Dalli
|
|
|
38
38
|
RequestFormatter.plain_meta_get(key, skip_flags)
|
|
39
39
|
end
|
|
40
40
|
flushed_write(req)
|
|
41
|
-
response_processor.meta_get_with_value(cache_nils: cache_nils?(options)
|
|
41
|
+
response_processor.meta_get_with_value(cache_nils: cache_nils?(options))
|
|
42
42
|
end
|
|
43
43
|
|
|
44
44
|
def quiet_get_request(key, options = nil)
|
|
45
45
|
# Skip bitflags in raw mode - saves 2 bytes per request and skips parsing
|
|
46
|
-
RequestFormatter.meta_get(key: key, return_cas: true, quiet: true, skip_flags:
|
|
46
|
+
RequestFormatter.meta_get(key: key, return_cas: true, quiet: true, skip_flags: raw_mode?,
|
|
47
47
|
**routing_token_kwargs(options))
|
|
48
48
|
end
|
|
49
49
|
|
|
50
50
|
# Same requests as quiet_get_request for each key, built in one pass
|
|
51
51
|
# and without the trailing noop.
|
|
52
52
|
def quiet_get_requests(keys, options = nil, return_cas: true)
|
|
53
|
-
RequestFormatter.multi_meta_get(keys, skip_flags:
|
|
54
|
-
|
|
53
|
+
RequestFormatter.multi_meta_get(keys, skip_flags: raw_mode?, return_cas: return_cas, terminate: false,
|
|
54
|
+
**routing_token_kwargs(options))
|
|
55
55
|
end
|
|
56
56
|
|
|
57
57
|
def gat(key, ttl, options = nil)
|
|
@@ -59,7 +59,7 @@ module Dalli
|
|
|
59
59
|
skip_flags = raw_mode? || (options && options[:raw])
|
|
60
60
|
req = RequestFormatter.meta_get(key: key, ttl: ttl, skip_flags: skip_flags, **routing_token_kwargs(options))
|
|
61
61
|
flushed_write(req)
|
|
62
|
-
response_processor.meta_get_with_value(cache_nils: cache_nils?(options)
|
|
62
|
+
response_processor.meta_get_with_value(cache_nils: cache_nils?(options))
|
|
63
63
|
end
|
|
64
64
|
|
|
65
65
|
def touch(key, ttl)
|
|
@@ -72,10 +72,9 @@ module Dalli
|
|
|
72
72
|
# TODO: This is confusing, as there's a cas command in memcached
|
|
73
73
|
# and this isn't it. Maybe rename? Maybe eliminate?
|
|
74
74
|
def cas(key, options = nil)
|
|
75
|
-
req = RequestFormatter.meta_get(key: key, value: true, return_cas: true,
|
|
76
|
-
**routing_token_kwargs(options))
|
|
75
|
+
req = RequestFormatter.meta_get(key: key, value: true, return_cas: true, **routing_token_kwargs(options))
|
|
77
76
|
flushed_write(req)
|
|
78
|
-
response_processor.meta_get_with_value_and_cas
|
|
77
|
+
response_processor.meta_get_with_value_and_cas
|
|
79
78
|
end
|
|
80
79
|
|
|
81
80
|
# Comprehensive meta get with support for all metadata flags.
|
|
@@ -104,7 +103,7 @@ module Dalli
|
|
|
104
103
|
# - :last_access - seconds since last access (only if return_last_access: true)
|
|
105
104
|
def meta_get(key, options = {})
|
|
106
105
|
req = RequestFormatter.meta_get(
|
|
107
|
-
key: key, value: true, return_cas: true,
|
|
106
|
+
key: key, value: true, return_cas: true,
|
|
108
107
|
vivify_ttl: options[:vivify_ttl], recache_ttl: options[:recache_ttl],
|
|
109
108
|
return_hit_status: options[:return_hit_status],
|
|
110
109
|
return_last_access: options[:return_last_access],
|
|
@@ -116,7 +115,7 @@ module Dalli
|
|
|
116
115
|
response_processor.meta_get_with_metadata(
|
|
117
116
|
cache_nils: cache_nils?(options), return_hit_status: options[:return_hit_status],
|
|
118
117
|
return_last_access: options[:return_last_access],
|
|
119
|
-
return_ttl_remaining: options[:return_ttl_remaining]
|
|
118
|
+
return_ttl_remaining: options[:return_ttl_remaining]
|
|
120
119
|
)
|
|
121
120
|
end
|
|
122
121
|
|
|
@@ -158,12 +157,17 @@ module Dalli
|
|
|
158
157
|
def write_storage_req(mode, key, raw_value, ttl = nil, cas = nil, options = {}, quiet: quiet?)
|
|
159
158
|
(value, bitflags) = @value_marshaller.store(key, raw_value, options)
|
|
160
159
|
ttl = TtlSanitizer.sanitize(ttl) if ttl
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
160
|
+
# eql?(0): only Integer 0 means "no CAS" here; anything else takes the general path
|
|
161
|
+
req = if mode == :set && !quiet && (cas.nil? || cas.eql?(0)) && !routing_tokens?(options)
|
|
162
|
+
RequestFormatter.plain_meta_set(key, value.bytesize, bitflags, ttl)
|
|
163
|
+
else
|
|
164
|
+
RequestFormatter.meta_set(key: key, value: value,
|
|
165
|
+
bitflags: bitflags, cas: cas,
|
|
166
|
+
ttl: ttl, mode: mode, quiet: quiet,
|
|
167
|
+
**routing_token_kwargs(options))
|
|
168
|
+
end
|
|
169
|
+
write("#{req}#{value}#{TERMINATOR}")
|
|
170
|
+
finish_write(quiet)
|
|
167
171
|
end
|
|
168
172
|
# rubocop:enable Metrics/ParameterLists
|
|
169
173
|
|
|
@@ -183,18 +187,8 @@ module Dalli
|
|
|
183
187
|
req = RequestFormatter.meta_set(key: key, value: value,
|
|
184
188
|
cas: cas, ttl: ttl, mode: mode, quiet: quiet?,
|
|
185
189
|
**routing_token_kwargs(options))
|
|
186
|
-
write(
|
|
187
|
-
|
|
188
|
-
end
|
|
189
|
-
|
|
190
|
-
# Ruby refuses to join Strings whose encodings clash, such as a raw
|
|
191
|
-
# UTF-16 value, or a routing token with non-ASCII characters next to a
|
|
192
|
-
# binary value. memcached stores bytes, so join the bytes then.
|
|
193
|
-
# Joining the Strings first keeps the common case free of extra checks.
|
|
194
|
-
def request_with_value(req, value)
|
|
195
|
-
"#{req}#{value}#{TERMINATOR}"
|
|
196
|
-
rescue Encoding::CompatibilityError
|
|
197
|
-
"#{req.b}#{value.b}#{TERMINATOR}"
|
|
190
|
+
write("#{req}#{value}#{TERMINATOR}")
|
|
191
|
+
finish_write(quiet?)
|
|
198
192
|
end
|
|
199
193
|
|
|
200
194
|
# Delete Commands
|
|
@@ -202,10 +196,15 @@ module Dalli
|
|
|
202
196
|
# `options` supports the meta-delete keys :invalidate, :tombstone_ttl and
|
|
203
197
|
# :drop_value, plus :p_token/:l_token; see Dalli::Client#delete.
|
|
204
198
|
def delete(key, cas, options = nil)
|
|
205
|
-
|
|
206
|
-
|
|
199
|
+
# Fast path for a plain delete: no options, no CAS, not quiet
|
|
200
|
+
req = if options.nil? && (cas.nil? || cas.eql?(0)) && !quiet?
|
|
201
|
+
RequestFormatter.plain_meta_delete(key)
|
|
202
|
+
else
|
|
203
|
+
RequestFormatter.meta_delete(key: key, cas: cas, quiet: quiet?,
|
|
204
|
+
**tombstone_kwargs(options), **routing_token_kwargs(options))
|
|
205
|
+
end
|
|
207
206
|
write(req)
|
|
208
|
-
|
|
207
|
+
finish_write(quiet?)
|
|
209
208
|
response_processor.meta_delete unless quiet?
|
|
210
209
|
end
|
|
211
210
|
|
|
@@ -234,54 +233,30 @@ module Dalli
|
|
|
234
233
|
# rubocop:disable-next Metrics/ParameterLists
|
|
235
234
|
def decr_incr(incr, key, delta, ttl, initial, options = nil)
|
|
236
235
|
ttl = initial ? TtlSanitizer.sanitize(ttl) : nil # Only set a TTL if we want to set a value on miss
|
|
237
|
-
# memcached stores an item created by ma's auto-create (N/J) for a
|
|
238
|
-
# base64-encoded key without its binary-key flag, and later replies
|
|
239
|
-
# name it with the raw key bytes. Create such counters with an add.
|
|
240
|
-
return decr_incr_with_add(incr, key, delta, ttl, initial, options) if initial && KeyRegularizer.required?(key)
|
|
241
|
-
|
|
242
236
|
write(RequestFormatter.meta_arithmetic(key: key, delta: delta, initial: initial, incr: incr, ttl: ttl,
|
|
243
237
|
quiet: quiet?, **routing_token_kwargs(options)))
|
|
244
|
-
|
|
238
|
+
finish_write(quiet?)
|
|
245
239
|
response_processor.decr_incr unless quiet?
|
|
246
240
|
end
|
|
247
241
|
|
|
248
|
-
# Like decr_incr with an initial value, without memcached's auto-create:
|
|
249
|
-
# on a miss the counter is added with the initial value (which is
|
|
250
|
-
# returned, as auto-create would), and the add losing a race to another
|
|
251
|
-
# client means the counter now exists, so the incr is tried again. In a
|
|
252
|
-
# quiet block both requests are sent and whichever applies takes effect.
|
|
253
|
-
# rubocop:disable-next Metrics/ParameterLists
|
|
254
|
-
def decr_incr_with_add(incr, key, delta, ttl, initial, options)
|
|
255
|
-
add_options = (options || {}).merge(raw: true) # counters are stored raw
|
|
256
|
-
return quiet_decr_incr_with_add(incr, key, delta, ttl, initial, add_options) if quiet?
|
|
257
|
-
|
|
258
|
-
2.times do
|
|
259
|
-
value = decr_incr(incr, key, delta, nil, nil, options)
|
|
260
|
-
return value unless value.nil?
|
|
261
|
-
|
|
262
|
-
write_storage_req(:add, key, initial.to_s, ttl, nil, add_options, quiet: false)
|
|
263
|
-
return initial if response_processor.meta_set_with_cas
|
|
264
|
-
end
|
|
265
|
-
decr_incr(incr, key, delta, nil, nil, options)
|
|
266
|
-
end
|
|
267
|
-
|
|
268
|
-
# rubocop:disable-next Metrics/ParameterLists
|
|
269
|
-
def quiet_decr_incr_with_add(incr, key, delta, ttl, initial, add_options)
|
|
270
|
-
write(RequestFormatter.meta_arithmetic(key: key, delta: delta, initial: nil, incr: incr, ttl: nil,
|
|
271
|
-
quiet: true, **routing_token_kwargs(add_options)))
|
|
272
|
-
# Sent with the rest of the quiet block, by its closing noop
|
|
273
|
-
write_storage_req(:add, key, initial.to_s, ttl, nil, add_options, quiet: true)
|
|
274
|
-
end
|
|
275
|
-
|
|
276
242
|
# Other Commands
|
|
277
243
|
def flush(delay = 0)
|
|
278
244
|
write(RequestFormatter.flush(delay: delay))
|
|
279
|
-
|
|
245
|
+
finish_write(quiet?)
|
|
280
246
|
response_processor.flush unless quiet?
|
|
281
247
|
end
|
|
282
248
|
|
|
283
249
|
# Noop is a keepalive operation but also used to demarcate the end of a set of pipelined commands.
|
|
284
250
|
# We need to read all the responses at once.
|
|
251
|
+
# A write that expects a reply is flushed so the reply can be read. A write
|
|
252
|
+
# in a quiet block stays buffered until the block's noop, unless
|
|
253
|
+
# defer_drain is on: then no noop follows at the end of the block, so it is
|
|
254
|
+
# flushed right away. (pipelined_set passes quiet: true outside a quiet
|
|
255
|
+
# block; its caller flushes with a terminating noop.)
|
|
256
|
+
def finish_write(quiet)
|
|
257
|
+
@connection_manager.flush if !quiet || (@defer_drain && quiet?)
|
|
258
|
+
end
|
|
259
|
+
|
|
285
260
|
def noop
|
|
286
261
|
write_noop
|
|
287
262
|
result = response_processor.consume_all_responses_until_mn
|
|
@@ -313,11 +288,11 @@ module Dalli
|
|
|
313
288
|
# response parsing to minimize per-key overhead. Avoids the PipelinedGetter
|
|
314
289
|
# machinery (IO.select, response buffering, server grouping).
|
|
315
290
|
def read_multi_req(keys, options = nil)
|
|
316
|
-
is_raw =
|
|
291
|
+
is_raw = raw_mode?
|
|
317
292
|
buffer = RequestFormatter.multi_meta_get(keys, skip_flags: is_raw, **routing_token_kwargs(options))
|
|
318
293
|
flushed_write(buffer)
|
|
319
294
|
buffer.clear
|
|
320
|
-
read_multi_get_responses(is_raw
|
|
295
|
+
read_multi_get_responses(is_raw)
|
|
321
296
|
end
|
|
322
297
|
|
|
323
298
|
# Stale-aware bulk get. Returns { key => { value:, cas:, stale:, miss: } }
|
|
@@ -329,99 +304,71 @@ module Dalli
|
|
|
329
304
|
# Shared by both the single-server fast path and PipelinedGetter's
|
|
330
305
|
# per-server-group request, so this one change covers both routes.
|
|
331
306
|
def read_multi_with_metadata_req(keys, options = nil)
|
|
332
|
-
is_raw =
|
|
307
|
+
is_raw = raw_mode?
|
|
333
308
|
buffer = RequestFormatter.multi_meta_get(keys, skip_flags: is_raw, return_cas: true,
|
|
334
309
|
**routing_token_kwargs(options))
|
|
335
310
|
flushed_write(buffer)
|
|
336
311
|
buffer.clear
|
|
337
|
-
read_multi_metadata_responses(is_raw
|
|
312
|
+
read_multi_metadata_responses(is_raw)
|
|
338
313
|
end
|
|
339
314
|
|
|
340
315
|
# Unlike read_multi_get_responses this locates tokens by flag rather than
|
|
341
316
|
# by position, because the c flag shifts the key's index.
|
|
342
|
-
def read_multi_metadata_responses(is_raw
|
|
343
|
-
expect_reply_keys(keys)
|
|
317
|
+
def read_multi_metadata_responses(is_raw)
|
|
344
318
|
hash = {}
|
|
345
319
|
while (line = @connection_manager.read_line)
|
|
346
320
|
break if line.start_with?('MN')
|
|
347
321
|
next unless line.start_with?('VA ')
|
|
348
322
|
|
|
349
323
|
tokens = line.chomp!(TERMINATOR).split
|
|
350
|
-
|
|
351
|
-
response_processor.check_value_size!(size)
|
|
352
|
-
value = @connection_manager.read(size + TERMINATOR.bytesize)&.chomp!(TERMINATOR)
|
|
324
|
+
value = @connection_manager.read(tokens[1].to_i + TERMINATOR.bytesize)&.chomp!(TERMINATOR)
|
|
353
325
|
stale = response_processor.stale_from_tokens(tokens)
|
|
354
326
|
cas = response_processor.cas_from_tokens(tokens)
|
|
355
327
|
bitflags = is_raw ? 0 : response_processor.bitflags_from_tokens(tokens)
|
|
356
328
|
key = response_processor.key_from_tokens(tokens)
|
|
357
329
|
next if key.nil?
|
|
358
330
|
|
|
359
|
-
raise_unexpected_reply_key(key) if unexpected_reply_key?(key)
|
|
360
|
-
|
|
361
331
|
hash[key] = { value: @value_marshaller.retrieve(value, bitflags), cas: cas, stale: stale, miss: false }
|
|
362
332
|
end
|
|
363
333
|
hash
|
|
364
334
|
end
|
|
365
335
|
|
|
366
|
-
def read_multi_get_responses(is_raw
|
|
367
|
-
expect_reply_keys(keys)
|
|
336
|
+
def read_multi_get_responses(is_raw)
|
|
368
337
|
hash = {}
|
|
369
|
-
key_index = is_raw ? 2 : 3
|
|
370
338
|
while (line = @connection_manager.read_line)
|
|
371
339
|
break if line.start_with?('MN')
|
|
372
340
|
next unless line.start_with?('VA ')
|
|
373
341
|
|
|
374
|
-
key, value = parse_multi_get_value(line,
|
|
375
|
-
raise_unexpected_reply_key(key) if unexpected_reply_key?(key)
|
|
342
|
+
key, value = parse_multi_get_value(line, is_raw)
|
|
376
343
|
hash[key] = value if key
|
|
377
344
|
end
|
|
378
345
|
hash
|
|
379
346
|
end
|
|
380
347
|
|
|
381
|
-
#
|
|
382
|
-
#
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
def parse_multi_get_value(line, key_index, is_raw)
|
|
389
|
-
tokens = line.chomp!(TERMINATOR).split
|
|
390
|
-
size = tokens[1].to_i
|
|
391
|
-
response_processor.check_value_size!(size)
|
|
392
|
-
value = @connection_manager.read(size + TERMINATOR.bytesize)&.chomp!(TERMINATOR)
|
|
393
|
-
raw_key = tokens[key_index]
|
|
394
|
-
return unless raw_key
|
|
348
|
+
# Reads the "VA <size> [f<flags>] k<key> ..." line in place rather than
|
|
349
|
+
# splitting it into tokens
|
|
350
|
+
def parse_multi_get_value(line, is_raw)
|
|
351
|
+
processor = response_processor
|
|
352
|
+
value = @connection_manager.read(processor.size_from_va_line(line) + TERMINATOR.bytesize)&.chomp!(TERMINATOR)
|
|
353
|
+
key = processor.key_from_va_line(line)
|
|
354
|
+
return unless key
|
|
395
355
|
|
|
396
|
-
|
|
397
|
-
key = KeyRegularizer.decode(key) if tokens.include?('b')
|
|
398
|
-
bitflags = is_raw ? 0 : response_processor.bitflags_from_tokens(tokens)
|
|
356
|
+
bitflags = is_raw ? 0 : processor.bitflags_from_va_line(line)
|
|
399
357
|
[key, @value_marshaller.retrieve(value, bitflags)]
|
|
400
358
|
end
|
|
401
359
|
|
|
402
360
|
# Single-server fast path for set_multi. Inlines request formatting to
|
|
403
361
|
# minimize per-key overhead. Avoids PipelinedSetter server grouping.
|
|
404
|
-
#
|
|
405
|
-
# A value that can't be marshalled or is over value_max_bytes is skipped;
|
|
406
|
-
# the other keys are still stored, then the first such error is raised
|
|
407
|
-
# (as PipelinedSetter does on the multi-server path).
|
|
408
362
|
def write_multi_req(pairs, ttl, req_options)
|
|
409
363
|
ttl = TtlSanitizer.sanitize(ttl) if ttl
|
|
410
|
-
|
|
411
|
-
entries = pairs.filter_map do |key, raw_value|
|
|
364
|
+
entries = pairs.map do |key, raw_value|
|
|
412
365
|
[key, @value_marshaller.store(key, raw_value, req_options)]
|
|
413
|
-
rescue Dalli::MarshalError, Dalli::ValueOverMaxSize => e
|
|
414
|
-
value_error ||= e
|
|
415
|
-
nil
|
|
416
366
|
end
|
|
417
367
|
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
response_processor.consume_all_responses_until_mn
|
|
423
|
-
end
|
|
424
|
-
raise value_error if value_error
|
|
368
|
+
buffer = RequestFormatter.multi_meta_set(entries, ttl: ttl, **routing_token_kwargs(req_options))
|
|
369
|
+
flushed_write(buffer)
|
|
370
|
+
buffer.clear
|
|
371
|
+
response_processor.consume_all_responses_until_mn
|
|
425
372
|
end
|
|
426
373
|
|
|
427
374
|
# Single-server fast path for delete_multi. Writes all quiet delete requests
|