dalli 5.3.3 → 5.4.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 +15 -0
- data/lib/dalli/client.rb +37 -3
- data/lib/dalli/deadline.rb +38 -0
- data/lib/dalli/pipelined_deleter.rb +4 -3
- data/lib/dalli/pipelined_getter.rb +26 -13
- data/lib/dalli/pipelined_setter.rb +4 -3
- data/lib/dalli/protocol/base.rb +14 -4
- data/lib/dalli/protocol/connection_manager.rb +262 -7
- data/lib/dalli/protocol/held_replies.rb +169 -0
- data/lib/dalli/version.rb +1 -1
- data/lib/dalli.rb +6 -0
- metadata +75 -69
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a9ff0cea6b9397fdb4bda61cb43998982e5972b859931780354a6540bf975187
|
|
4
|
+
data.tar.gz: 345a0407d7aaf23515b4711ba5469b19d43512781bb69413b813ac183186f3bd
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 4912d5a7481d10b7ca10257d21408496db84f54ac92c08102934ba29575d661e5b3c03ffb669736d72ea6f631ff71815351e37d7b43df3287ca6c83393f9ee40
|
|
7
|
+
data.tar.gz: 93309cfff89840ba1b4c4c1b9d7824f4496044877f1b509b008a3175db73d40c076d58b3acfadc566fb5c79fc84756ab8a7170f9f8f36da64344d010c23c4077
|
data/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,21 @@ Dalli Changelog
|
|
|
4
4
|
Unreleased
|
|
5
5
|
==========
|
|
6
6
|
|
|
7
|
+
5.4.0
|
|
8
|
+
==========
|
|
9
|
+
|
|
10
|
+
New features:
|
|
11
|
+
|
|
12
|
+
- `Client#with_deadline(seconds) { }` gives the calls in the block a deadline: each socket wait, reconnect, retry and `get_multi` wait is cut to the time left, and once it has passed the call raises `Dalli::DeadlineExceededError`. That error isn't a `NetworkError`, so the call isn't retried or failed over and the server isn't counted toward `socket_max_failures`. The deadline applies only to the calling thread or fiber, as `quiet` does (#1322, proposed by @byroot in #1319)
|
|
13
|
+
|
|
14
|
+
5.3.4
|
|
15
|
+
==========
|
|
16
|
+
|
|
17
|
+
Bug fixes:
|
|
18
|
+
|
|
19
|
+
- `Client#with`, which lets a bare client stand in for a connection pool, now accepts and ignores the arguments `ConnectionPool#with` takes, such as `timeout:`. Before, passing any raised `ArgumentError` (#1323, @byroot)
|
|
20
|
+
- A request larger than the socket buffers no longer stalls until `socket_timeout` and marks the server down. memcached stops reading a connection while its replies go unread, so a large `get_multi`, `get_multi_cas`, `get_multi_with_metadata` or `delete_multi` (whose missing keys each get a reply) waited on the server. Over a Unix socket, whose buffer doesn't grow, this started at about 1,000 keys for `get_multi_cas`. Replies that arrive while a request is being sent are now read and held, checked as they arrive against what the requests sent could get back (#1328, based on #1326 by @edcarvio)
|
|
21
|
+
|
|
7
22
|
5.3.3
|
|
8
23
|
==========
|
|
9
24
|
|
data/lib/dalli/client.rb
CHANGED
|
@@ -83,6 +83,8 @@ module Dalli
|
|
|
83
83
|
@options = normalize_options(options)
|
|
84
84
|
@key_manager = ::Dalli::KeyManager.new(@options)
|
|
85
85
|
@ring = nil
|
|
86
|
+
# The fiber-local key where with_deadline keeps this client's deadline
|
|
87
|
+
@deadline_key = :"dalli_deadline_#{object_id}"
|
|
86
88
|
end
|
|
87
89
|
|
|
88
90
|
#
|
|
@@ -474,6 +476,38 @@ module Dalli
|
|
|
474
476
|
end
|
|
475
477
|
alias multi quiet
|
|
476
478
|
|
|
479
|
+
##
|
|
480
|
+
# Runs the block with a deadline: the calls this client makes in it must
|
|
481
|
+
# finish within +seconds+, including reconnects, retries and get_multi's
|
|
482
|
+
# waits. Each wait is cut to the time left, and once it's used up the
|
|
483
|
+
# call raises Dalli::DeadlineExceededError.
|
|
484
|
+
#
|
|
485
|
+
# That error isn't a NetworkError. The call isn't retried or failed over,
|
|
486
|
+
# and the server isn't counted toward socket_max_failures, since running
|
|
487
|
+
# out of time says nothing about it. The connection the call was using is
|
|
488
|
+
# closed, as its reply may be half read.
|
|
489
|
+
#
|
|
490
|
+
# The deadline applies only to the thread or fiber that runs the block,
|
|
491
|
+
# as quiet does. Threads and fibers started in the block don't inherit
|
|
492
|
+
# it, and another thread sharing this client isn't affected. A nested
|
|
493
|
+
# block can only shorten the deadline. Float::INFINITY,
|
|
494
|
+
# which ActiveSupport::Deadline#remaining returns when there's no
|
|
495
|
+
# deadline, sets none. Yields the client and returns the block's value.
|
|
496
|
+
#
|
|
497
|
+
# client.with_deadline(0.5) { |c| c.get('key') }
|
|
498
|
+
def with_deadline(seconds)
|
|
499
|
+
return yield self if seconds == Float::INFINITY
|
|
500
|
+
|
|
501
|
+
deadline = Deadline.new(seconds)
|
|
502
|
+
outer = Thread.current[@deadline_key]
|
|
503
|
+
Thread.current[@deadline_key] = deadline if outer.nil? || deadline.expires_at < outer.expires_at
|
|
504
|
+
begin
|
|
505
|
+
yield self
|
|
506
|
+
ensure
|
|
507
|
+
Thread.current[@deadline_key] = outer
|
|
508
|
+
end
|
|
509
|
+
end
|
|
510
|
+
|
|
477
511
|
##
|
|
478
512
|
# Reads and discards the replies still pending from quiet requests, on the
|
|
479
513
|
# servers that have any. Only needed with the defer_drain option.
|
|
@@ -796,7 +830,7 @@ module Dalli
|
|
|
796
830
|
end
|
|
797
831
|
|
|
798
832
|
# Stub method so a bare Dalli client can pretend to be a connection pool.
|
|
799
|
-
def with
|
|
833
|
+
def with(...)
|
|
800
834
|
yield self
|
|
801
835
|
end
|
|
802
836
|
|
|
@@ -1120,7 +1154,7 @@ module Dalli
|
|
|
1120
1154
|
end
|
|
1121
1155
|
|
|
1122
1156
|
def ring
|
|
1123
|
-
@ring ||= Dalli::Ring.new(@normalized_servers, @options)
|
|
1157
|
+
@ring ||= Dalli::Ring.new(@normalized_servers, @options.merge(deadline_key: @deadline_key))
|
|
1124
1158
|
end
|
|
1125
1159
|
|
|
1126
1160
|
##
|
|
@@ -1187,7 +1221,7 @@ module Dalli
|
|
|
1187
1221
|
end
|
|
1188
1222
|
|
|
1189
1223
|
def pipelined_getter
|
|
1190
|
-
PipelinedGetter.new(ring, @key_manager)
|
|
1224
|
+
PipelinedGetter.new(ring, @key_manager, @deadline_key)
|
|
1191
1225
|
end
|
|
1192
1226
|
|
|
1193
1227
|
def pipelined_setter
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Dalli
|
|
4
|
+
##
|
|
5
|
+
# The time by which the calls in a Client#with_deadline block must finish.
|
|
6
|
+
# clamp takes the same arguments as ActiveSupport::Deadline#clamp.
|
|
7
|
+
##
|
|
8
|
+
class Deadline
|
|
9
|
+
attr_reader :expires_at
|
|
10
|
+
|
|
11
|
+
def initialize(seconds)
|
|
12
|
+
unless seconds.is_a?(Numeric) && seconds.to_f.finite?
|
|
13
|
+
raise ArgumentError, "deadline must be a finite number of seconds, got #{seconds.inspect}"
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
@expires_at = now + seconds
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def remaining
|
|
20
|
+
@expires_at - now
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# Returns maximum, or the time left if that's shorter. Raises error once
|
|
24
|
+
# no more than minimum is left.
|
|
25
|
+
def clamp(maximum, minimum: 0.0, error: Dalli::DeadlineExceededError)
|
|
26
|
+
left = @expires_at - now
|
|
27
|
+
raise error, 'Deadline exceeded' if left <= minimum
|
|
28
|
+
|
|
29
|
+
[maximum, left].min
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
private
|
|
33
|
+
|
|
34
|
+
def now
|
|
35
|
+
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -62,13 +62,14 @@ module Dalli
|
|
|
62
62
|
# combined `rescue DalliError, NetworkError` would silently swallow
|
|
63
63
|
# RetryableNetworkError too -- since NetworkError < DalliError -- dropping
|
|
64
64
|
# this server's keys on a transient hiccup instead of retrying. Only a
|
|
65
|
-
# non-network DalliError should be swallowed here
|
|
65
|
+
# non-network DalliError should be swallowed here; an exceeded deadline
|
|
66
|
+
# ends the call.
|
|
66
67
|
def make_delete_requests(groups, req_options = nil)
|
|
67
68
|
groups.each do |server, keys_for_server|
|
|
68
69
|
keys_for_server.select! do |key|
|
|
69
70
|
server.request(:pipelined_delete, key, req_options)
|
|
70
71
|
true
|
|
71
|
-
rescue Dalli::NetworkError
|
|
72
|
+
rescue Dalli::NetworkError, Dalli::DeadlineExceededError
|
|
72
73
|
raise
|
|
73
74
|
rescue DalliError => e
|
|
74
75
|
Dalli.logger.debug { e.inspect }
|
|
@@ -85,7 +86,7 @@ module Dalli
|
|
|
85
86
|
def finish_requests(groups)
|
|
86
87
|
groups.sum do |server, keys_for_server|
|
|
87
88
|
server.request(:finish_pipelined_delete, keys_for_server.size)
|
|
88
|
-
rescue Dalli::NetworkError
|
|
89
|
+
rescue Dalli::NetworkError, Dalli::DeadlineExceededError
|
|
89
90
|
raise
|
|
90
91
|
rescue DalliError => e
|
|
91
92
|
Dalli.logger.debug { e.inspect }
|
|
@@ -14,9 +14,10 @@ module Dalli
|
|
|
14
14
|
# Number of keys to send before draining responses during interleaved mode
|
|
15
15
|
CHUNK_SIZE = 10_000
|
|
16
16
|
|
|
17
|
-
def initialize(ring, key_manager)
|
|
17
|
+
def initialize(ring, key_manager, deadline_key = nil)
|
|
18
18
|
@ring = ring
|
|
19
19
|
@key_manager = key_manager
|
|
20
|
+
@deadline_key = deadline_key
|
|
20
21
|
end
|
|
21
22
|
|
|
22
23
|
##
|
|
@@ -52,7 +53,9 @@ module Dalli
|
|
|
52
53
|
# First yield any partial results collected during interleaved send
|
|
53
54
|
yield_partial_results(&block)
|
|
54
55
|
|
|
55
|
-
|
|
56
|
+
timeout = @ring.socket_timeout
|
|
57
|
+
deadline = @deadline_key && Thread.current[@deadline_key]
|
|
58
|
+
fetch_responses(servers, timeout, deadline, &block) until servers.empty?
|
|
56
59
|
raise @unmarshal_error if @unmarshal_error
|
|
57
60
|
ensure
|
|
58
61
|
abort_unfinished(servers)
|
|
@@ -93,7 +96,7 @@ module Dalli
|
|
|
93
96
|
caller_key = @key_manager.caller_key(caller_keys, key)
|
|
94
97
|
results[caller_key] = metadata if caller_key
|
|
95
98
|
end
|
|
96
|
-
rescue Dalli::NetworkError, Dalli::UnmarshalError
|
|
99
|
+
rescue Dalli::NetworkError, Dalli::UnmarshalError, Dalli::DeadlineExceededError
|
|
97
100
|
raise
|
|
98
101
|
rescue DalliError => e
|
|
99
102
|
Dalli.logger.debug { e.inspect }
|
|
@@ -128,7 +131,7 @@ module Dalli
|
|
|
128
131
|
|
|
129
132
|
started << server
|
|
130
133
|
finish_query_for_server(server)
|
|
131
|
-
rescue Dalli::NetworkError
|
|
134
|
+
rescue Dalli::NetworkError, Dalli::DeadlineExceededError
|
|
132
135
|
abort_unfinished(started)
|
|
133
136
|
raise
|
|
134
137
|
rescue Dalli::DalliError
|
|
@@ -159,8 +162,8 @@ module Dalli
|
|
|
159
162
|
# too, dropping this server's keys from the result on a transient hiccup
|
|
160
163
|
# instead of retrying, with nothing surfaced above debug-level logging.
|
|
161
164
|
# Only a non-network DalliError (this server genuinely can't serve these
|
|
162
|
-
# keys) should be swallowed here.
|
|
163
|
-
rescue Dalli::NetworkError
|
|
165
|
+
# keys) should be swallowed here. An exceeded deadline ends the call.
|
|
166
|
+
rescue Dalli::NetworkError, Dalli::DeadlineExceededError
|
|
164
167
|
raise
|
|
165
168
|
rescue DalliError => e
|
|
166
169
|
Dalli.logger.debug { e.inspect }
|
|
@@ -190,13 +193,22 @@ module Dalli
|
|
|
190
193
|
# Reads the replies available, removing finished servers from servers.
|
|
191
194
|
# The wait is for any server to send something, so it starts again after
|
|
192
195
|
# each read: a large batch that keeps arriving isn't cut off.
|
|
193
|
-
|
|
196
|
+
#
|
|
197
|
+
# With a deadline, each wait is cut to the time left. When a wait cut
|
|
198
|
+
# short finds nothing, the deadline has passed: the caller's ensure
|
|
199
|
+
# closes the connections, and no server is counted as failing.
|
|
200
|
+
def fetch_responses(servers, timeout, deadline, &block)
|
|
194
201
|
# Remove any servers which are not connected
|
|
195
202
|
servers.select!(&:connected?)
|
|
196
203
|
return if servers.empty?
|
|
197
204
|
|
|
198
|
-
|
|
199
|
-
|
|
205
|
+
wait = deadline ? deadline.clamp(timeout || Float::INFINITY) : timeout
|
|
206
|
+
readable_servers = servers_with_response(servers, wait)
|
|
207
|
+
if readable_servers.empty?
|
|
208
|
+
raise Dalli::DeadlineExceededError, 'Deadline exceeded waiting for get_multi' if deadline && wait != timeout
|
|
209
|
+
|
|
210
|
+
time_out(servers)
|
|
211
|
+
end
|
|
200
212
|
|
|
201
213
|
# Loop through the servers with responses, and
|
|
202
214
|
# delete any from our list that are finished
|
|
@@ -247,13 +259,14 @@ module Dalli
|
|
|
247
259
|
def servers_with_response(servers, timeout)
|
|
248
260
|
return [] if servers.empty?
|
|
249
261
|
|
|
250
|
-
# A bounded read can stop while OpenSSL still holds decrypted bytes
|
|
251
|
-
#
|
|
252
|
-
#
|
|
262
|
+
# A bounded read can stop while OpenSSL still holds decrypted bytes,
|
|
263
|
+
# and replies that arrived while the requests were sent are held by the
|
|
264
|
+
# connection. IO.select only sees the underlying socket, so process
|
|
265
|
+
# those bytes before waiting for another network event.
|
|
253
266
|
buffered = nil
|
|
254
267
|
servers.each do |server|
|
|
255
268
|
sock = server.sock
|
|
256
|
-
(buffered ||= []) << server if sock.respond_to?(:pending_data?) && sock.pending_data?
|
|
269
|
+
(buffered ||= []) << server if server.held_replies? || (sock.respond_to?(:pending_data?) && sock.pending_data?)
|
|
257
270
|
end
|
|
258
271
|
return buffered if buffered
|
|
259
272
|
|
|
@@ -64,12 +64,13 @@ module Dalli
|
|
|
64
64
|
# combined `rescue DalliError, NetworkError` would silently swallow
|
|
65
65
|
# RetryableNetworkError too -- since NetworkError < DalliError -- dropping
|
|
66
66
|
# this server's keys on a transient hiccup instead of retrying. Only a
|
|
67
|
-
# non-network DalliError should be swallowed here
|
|
67
|
+
# non-network DalliError should be swallowed here; an exceeded deadline
|
|
68
|
+
# ends the call.
|
|
68
69
|
def make_set_requests(groups, pairs, ttl, req_options)
|
|
69
70
|
groups.each do |server, keys_for_server|
|
|
70
71
|
keys_for_server.each do |key|
|
|
71
72
|
server.request(:pipelined_set, key, pairs[key], ttl, req_options)
|
|
72
|
-
rescue Dalli::NetworkError
|
|
73
|
+
rescue Dalli::NetworkError, Dalli::DeadlineExceededError
|
|
73
74
|
raise
|
|
74
75
|
rescue Dalli::MarshalError, Dalli::ValueOverMaxSize => e
|
|
75
76
|
# Nothing was written for this key, so the server's other keys are
|
|
@@ -88,7 +89,7 @@ module Dalli
|
|
|
88
89
|
def finish_requests(servers)
|
|
89
90
|
servers.each do |server|
|
|
90
91
|
server.request(:noop)
|
|
91
|
-
rescue Dalli::NetworkError
|
|
92
|
+
rescue Dalli::NetworkError, Dalli::DeadlineExceededError
|
|
92
93
|
raise
|
|
93
94
|
rescue DalliError => e
|
|
94
95
|
Dalli.logger.debug { e.inspect }
|
data/lib/dalli/protocol/base.rb
CHANGED
|
@@ -47,6 +47,7 @@ module Dalli
|
|
|
47
47
|
@verify_reply_keys = client_options[:verify_reply_keys] ? true : false
|
|
48
48
|
@value_marshaller = @raw_mode ? StringMarshaller.new(@options) : ValueMarshaller.new(@options)
|
|
49
49
|
@connection_manager = ConnectionManager.new(hostname, port, socket_type, @options)
|
|
50
|
+
@connection_manager.max_reply_value_bytes = @value_marshaller.max_response_bytes
|
|
50
51
|
end
|
|
51
52
|
|
|
52
53
|
# Returns true if client is in raw mode (no serialization/compression).
|
|
@@ -65,6 +66,7 @@ module Dalli
|
|
|
65
66
|
|
|
66
67
|
# Chokepoint method for error handling and ensuring liveness
|
|
67
68
|
def request(opkey, *args)
|
|
69
|
+
@connection_manager.check_deadline!
|
|
68
70
|
verify_state(opkey)
|
|
69
71
|
|
|
70
72
|
begin
|
|
@@ -254,6 +256,12 @@ module Dalli
|
|
|
254
256
|
!response_buffer.in_progress?
|
|
255
257
|
end
|
|
256
258
|
|
|
259
|
+
# Replies that arrived while the request was sent, which IO.select
|
|
260
|
+
# can't see (see ConnectionManager#send_while_reading)
|
|
261
|
+
def held_replies?
|
|
262
|
+
@connection_manager.held_replies?
|
|
263
|
+
end
|
|
264
|
+
|
|
257
265
|
def quiet?
|
|
258
266
|
Thread.current[::Dalli::QUIET]
|
|
259
267
|
end
|
|
@@ -433,8 +441,10 @@ module Dalli
|
|
|
433
441
|
end
|
|
434
442
|
|
|
435
443
|
def connect
|
|
436
|
-
@connection_manager.
|
|
437
|
-
|
|
444
|
+
@connection_manager.within_connect_deadline do
|
|
445
|
+
@connection_manager.establish_connection
|
|
446
|
+
@version = version
|
|
447
|
+
end
|
|
438
448
|
up!
|
|
439
449
|
end
|
|
440
450
|
|
|
@@ -487,8 +497,8 @@ module Dalli
|
|
|
487
497
|
def drain_pipeline_responses(results)
|
|
488
498
|
return unless connected?
|
|
489
499
|
|
|
490
|
-
#
|
|
491
|
-
return unless sock.wait_readable(0)
|
|
500
|
+
# Replies held while this chunk was sent first, then the socket
|
|
501
|
+
return unless @connection_manager.held_replies? || sock.wait_readable(0)
|
|
492
502
|
|
|
493
503
|
# Read available data without blocking
|
|
494
504
|
response_buffer.read
|
|
@@ -33,6 +33,13 @@ module Dalli
|
|
|
33
33
|
# cleared with the write buffer whenever the connection closes.
|
|
34
34
|
attr_writer :response_buffer
|
|
35
35
|
|
|
36
|
+
# The largest value a reply held during a send may declare: the
|
|
37
|
+
# protocol's max_response_bytes (see HeldReplies). Kept in options, as
|
|
38
|
+
# each instance variable here costs every request a little.
|
|
39
|
+
def max_reply_value_bytes=(bytes)
|
|
40
|
+
@options[:max_reply_value_bytes] = bytes
|
|
41
|
+
end
|
|
42
|
+
|
|
36
43
|
def initialize(hostname, port, socket_type, client_options)
|
|
37
44
|
@hostname = hostname
|
|
38
45
|
@port = port
|
|
@@ -46,8 +53,18 @@ module Dalli
|
|
|
46
53
|
@write_buffer = []
|
|
47
54
|
@write_buffer_bytes = 0
|
|
48
55
|
@response_buffer = nil
|
|
56
|
+
# Replies that arrived while a request was being sent, read before the
|
|
57
|
+
# socket while @held (see #send_while_reading)
|
|
58
|
+
@held_replies = nil
|
|
59
|
+
@held = false
|
|
60
|
+
@unanswered_bytes = 0
|
|
49
61
|
|
|
50
62
|
@fail_count = 0
|
|
63
|
+
# Where the client's with_deadline keeps its deadline (see
|
|
64
|
+
# check_deadline!), and this request's waits when it cuts them short
|
|
65
|
+
@deadline_key = @options[:deadline_key]
|
|
66
|
+
@deadline = nil
|
|
67
|
+
@request_timeout = nil
|
|
51
68
|
reset_down_info
|
|
52
69
|
extend ReadTimeout if @options[:enforce_read_timeout] && RUBY_ENGINE != 'ruby'
|
|
53
70
|
end
|
|
@@ -128,6 +145,9 @@ module Dalli
|
|
|
128
145
|
reconnect_on_fork if fork_detected?
|
|
129
146
|
return if @sock.nil? || @quiet_responses_pending
|
|
130
147
|
|
|
148
|
+
# Held replies with no request waiting for them were never asked for
|
|
149
|
+
return close if @held
|
|
150
|
+
|
|
131
151
|
now = Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond)
|
|
132
152
|
idle = now - @last_used_at
|
|
133
153
|
@last_used_at = now
|
|
@@ -180,6 +200,9 @@ module Dalli
|
|
|
180
200
|
@pid = nil
|
|
181
201
|
@quiet_responses_pending = false
|
|
182
202
|
discard_write_buffer
|
|
203
|
+
discard_held
|
|
204
|
+
@held_replies&.reset_accounting
|
|
205
|
+
@unanswered_bytes = 0
|
|
183
206
|
@response_buffer&.clear
|
|
184
207
|
abort_request!
|
|
185
208
|
end
|
|
@@ -220,6 +243,40 @@ module Dalli
|
|
|
220
243
|
|
|
221
244
|
@request_in_progress = true
|
|
222
245
|
@request_sent = false
|
|
246
|
+
limit_to_deadline if @deadline
|
|
247
|
+
end
|
|
248
|
+
|
|
249
|
+
# Called before each request, ahead of any reconnect. Looks up the
|
|
250
|
+
# deadline of the Client#with_deadline block it's made in, and raises
|
|
251
|
+
# Dalli::DeadlineExceededError if that has passed, so a request that
|
|
252
|
+
# can't run leaves the connection as it is.
|
|
253
|
+
def check_deadline!
|
|
254
|
+
return unless @deadline_key
|
|
255
|
+
|
|
256
|
+
@deadline = Thread.current[@deadline_key]
|
|
257
|
+
@deadline&.clamp(Float::INFINITY)
|
|
258
|
+
end
|
|
259
|
+
|
|
260
|
+
# Bounds a connect by the deadline, when it leaves less than the connect
|
|
261
|
+
# timeout: the socket, a TLS handshake and the version check that
|
|
262
|
+
# follows. Nothing has been sent on the connection yet, so a timeout
|
|
263
|
+
# here closes it and raises Dalli::DeadlineExceededError.
|
|
264
|
+
def within_connect_deadline(&)
|
|
265
|
+
deadline = @deadline_key && Thread.current[@deadline_key]
|
|
266
|
+
return yield unless deadline
|
|
267
|
+
|
|
268
|
+
timeout = Dalli::Socket.connect_timeout_for(options) || Float::INFINITY
|
|
269
|
+
limit = deadline.clamp(timeout)
|
|
270
|
+
return yield if limit == timeout
|
|
271
|
+
|
|
272
|
+
@request_timeout = limit
|
|
273
|
+
Timeout.timeout(limit, &)
|
|
274
|
+
rescue *TIMEOUT_ERRORS => e
|
|
275
|
+
raise unless @request_timeout
|
|
276
|
+
|
|
277
|
+
deadline_exceeded!(e)
|
|
278
|
+
ensure
|
|
279
|
+
@request_timeout = nil
|
|
223
280
|
end
|
|
224
281
|
|
|
225
282
|
def finish_request!
|
|
@@ -227,11 +284,15 @@ module Dalli
|
|
|
227
284
|
|
|
228
285
|
@request_in_progress = false
|
|
229
286
|
@request_sent = false
|
|
287
|
+
restore_timeout if @request_timeout
|
|
230
288
|
# A completed request is what proves the server healthy again, so the
|
|
231
289
|
# failure count resets here rather than on reconnect: a server that
|
|
232
290
|
# accepts connections but never answers would otherwise reset it on
|
|
233
291
|
# every retry and never reach socket_max_failures.
|
|
234
292
|
@fail_count = 0
|
|
293
|
+
# The server has read everything sent so far: it answered the last of it
|
|
294
|
+
@unanswered_bytes = 0
|
|
295
|
+
@held_replies.reset_accounting if @held_replies&.counting?
|
|
235
296
|
end
|
|
236
297
|
|
|
237
298
|
# Ends a request whose reply isn't read with it: a quiet write, or one of
|
|
@@ -242,11 +303,13 @@ module Dalli
|
|
|
242
303
|
|
|
243
304
|
@request_in_progress = false
|
|
244
305
|
@request_sent = false
|
|
306
|
+
restore_timeout if @request_timeout
|
|
245
307
|
end
|
|
246
308
|
|
|
247
309
|
def abort_request!
|
|
248
310
|
@request_in_progress = false
|
|
249
311
|
@request_sent = false
|
|
312
|
+
restore_timeout if @request_timeout
|
|
250
313
|
end
|
|
251
314
|
|
|
252
315
|
# True once any of the request in progress has been handed to the
|
|
@@ -265,13 +328,21 @@ module Dalli
|
|
|
265
328
|
# never sends CRLF can't grow the read buffer without bound.
|
|
266
329
|
MAX_LINE_BYTES = 8 * 1024
|
|
267
330
|
|
|
331
|
+
# TruffleRuby's gets with a limit doesn't find a two-byte separator whose
|
|
332
|
+
# bytes arrive in separate reads: it waits for more, or returns the line
|
|
333
|
+
# without them. Every reply line ends in CRLF, checked after the read,
|
|
334
|
+
# so reading to the LF finds the same lines.
|
|
335
|
+
LINE_SEPARATOR = RUBY_ENGINE == 'truffleruby' ? "\n" : "\r\n"
|
|
336
|
+
|
|
268
337
|
def read_line
|
|
338
|
+
return held_line if @held
|
|
339
|
+
|
|
269
340
|
flush_write_buffer
|
|
270
|
-
data = timed_tls_operation { @sock.gets(
|
|
341
|
+
data = timed_tls_operation { @sock.gets(LINE_SEPARATOR, MAX_LINE_BYTES) }
|
|
271
342
|
error_on_request!('EOF in read_line') if data.nil?
|
|
272
343
|
# Without its CRLF the line either ran past MAX_LINE_BYTES or was cut
|
|
273
344
|
# short by EOF; either way the connection is out of step
|
|
274
|
-
|
|
345
|
+
line_too_long! unless data.end_with?("\r\n")
|
|
275
346
|
data
|
|
276
347
|
rescue SystemCallError, *TIMEOUT_ERRORS, *SSL_ERRORS, EOFError => e
|
|
277
348
|
error_on_request!(e)
|
|
@@ -292,6 +363,8 @@ module Dalli
|
|
|
292
363
|
|
|
293
364
|
if RUBY_ENGINE == 'jruby'
|
|
294
365
|
def read(count)
|
|
366
|
+
return held_read(count) if @held
|
|
367
|
+
|
|
295
368
|
check_read_size!(count)
|
|
296
369
|
flush_write_buffer
|
|
297
370
|
@sock.readfull(count)
|
|
@@ -300,6 +373,8 @@ module Dalli
|
|
|
300
373
|
end
|
|
301
374
|
else
|
|
302
375
|
def read(count)
|
|
376
|
+
return held_read(count) if @held
|
|
377
|
+
|
|
303
378
|
check_read_size!(count)
|
|
304
379
|
flush_write_buffer
|
|
305
380
|
read_bytes(count)
|
|
@@ -321,6 +396,16 @@ module Dalli
|
|
|
321
396
|
# is sent once it grows past this size, or when a reply is read.
|
|
322
397
|
WRITE_BUFFER_FLUSH_BYTES = 64 * 1024
|
|
323
398
|
|
|
399
|
+
# Less than any socket send buffer: 8 KiB is the smallest default
|
|
400
|
+
# (Unix sockets on macOS), and Linux doubles a requested SO_SNDBUF from
|
|
401
|
+
# a minimum of 2,304 bytes
|
|
402
|
+
BLOCKING_WRITE_BYTES = 4 * 1024
|
|
403
|
+
|
|
404
|
+
# jruby-openssl won't read while it has encrypted bytes it couldn't send,
|
|
405
|
+
# and they can't go until the server reads, which it won't while its
|
|
406
|
+
# replies go unread. On JRuby a TLS request is sent as before.
|
|
407
|
+
HALF_DUPLEX_TLS = RUBY_ENGINE == 'jruby'
|
|
408
|
+
|
|
324
409
|
def write(bytes)
|
|
325
410
|
@write_buffer << bytes
|
|
326
411
|
@write_buffer_bytes += bytes.bytesize
|
|
@@ -345,17 +430,26 @@ module Dalli
|
|
|
345
430
|
end
|
|
346
431
|
|
|
347
432
|
# Non-blocking read. Here to support the operation
|
|
348
|
-
# of the get_multi operation
|
|
433
|
+
# of the get_multi operation. Held replies come first.
|
|
349
434
|
def read_available(...)
|
|
435
|
+
return take_held(@held_replies.bytesize) if @held
|
|
436
|
+
|
|
350
437
|
flush_write_buffer
|
|
351
438
|
@sock.read_available(...)
|
|
352
439
|
end
|
|
353
440
|
|
|
441
|
+
# Whether replies that arrived during a send are waiting to be read.
|
|
442
|
+
# IO.select can't see them, so a caller waiting for replies checks first.
|
|
443
|
+
def held_replies?
|
|
444
|
+
@held
|
|
445
|
+
end
|
|
446
|
+
|
|
354
447
|
def max_allowed_failures
|
|
355
448
|
@max_allowed_failures ||= @options[:socket_max_failures] || 2
|
|
356
449
|
end
|
|
357
450
|
|
|
358
451
|
def error_on_request!(err_or_string)
|
|
452
|
+
deadline_exceeded!(err_or_string) if @request_timeout && timeout_error?(err_or_string)
|
|
359
453
|
log_warn_message(err_or_string)
|
|
360
454
|
|
|
361
455
|
@fail_count += 1
|
|
@@ -441,21 +535,180 @@ module Dalli
|
|
|
441
535
|
def timed_tls_operation(&)
|
|
442
536
|
return yield unless @sock.is_a?(Dalli::Socket::SSLSocket)
|
|
443
537
|
|
|
444
|
-
Timeout.timeout(socket_timeout, &)
|
|
538
|
+
Timeout.timeout(@request_timeout || socket_timeout, &)
|
|
539
|
+
end
|
|
540
|
+
|
|
541
|
+
# Cuts this request's waits to the time its deadline leaves, when that's
|
|
542
|
+
# less than socket_timeout. CRuby enforces it through IO#timeout=; the
|
|
543
|
+
# TLS and enforce_read_timeout waits read @request_timeout.
|
|
544
|
+
def limit_to_deadline
|
|
545
|
+
timeout = socket_timeout || Float::INFINITY
|
|
546
|
+
limit = @deadline.clamp(timeout)
|
|
547
|
+
return if limit == timeout
|
|
548
|
+
|
|
549
|
+
@request_timeout = limit
|
|
550
|
+
io = @sock&.to_io
|
|
551
|
+
io.timeout = limit if io.respond_to?(:timeout=)
|
|
552
|
+
end
|
|
553
|
+
|
|
554
|
+
def restore_timeout
|
|
555
|
+
io = @sock&.to_io
|
|
556
|
+
io.timeout = socket_timeout if io.respond_to?(:timeout=)
|
|
557
|
+
@request_timeout = nil
|
|
558
|
+
end
|
|
559
|
+
|
|
560
|
+
def timeout_error?(err_or_string)
|
|
561
|
+
TIMEOUT_ERRORS.any? { |klass| err_or_string.is_a?(klass) }
|
|
562
|
+
end
|
|
563
|
+
|
|
564
|
+
# A wait cut short by a deadline ended the call. The connection is closed,
|
|
565
|
+
# as the reply may be half read, but the timeout says nothing about the
|
|
566
|
+
# server, so it isn't counted against it and the call isn't retried.
|
|
567
|
+
def deadline_exceeded!(err)
|
|
568
|
+
Dalli.logger.debug { "#{name}: deadline exceeded (#{err.class})" }
|
|
569
|
+
close
|
|
570
|
+
@request_timeout = nil
|
|
571
|
+
raise Dalli::DeadlineExceededError, "Deadline exceeded waiting for #{name}"
|
|
445
572
|
end
|
|
446
573
|
|
|
447
574
|
# One write per flush. A TLS socket sends each argument of
|
|
448
575
|
# write(*parts) as its own record and system call, so the parts are
|
|
449
576
|
# joined first.
|
|
577
|
+
#
|
|
578
|
+
# A write blocks while the socket's send buffer is full. memcached stops
|
|
579
|
+
# reading a connection while its replies go unread, so once the requests
|
|
580
|
+
# it hasn't answered are more than the buffers hold, a blocking write
|
|
581
|
+
# and the server wait on each other until socket_timeout. Up to
|
|
582
|
+
# BLOCKING_WRITE_BYTES unanswered, the write fits whatever the server
|
|
583
|
+
# is doing; past that, #send_while_reading takes the replies off the
|
|
584
|
+
# socket while it sends.
|
|
450
585
|
def flush_write_buffer
|
|
451
586
|
return if @write_buffer.empty?
|
|
452
587
|
|
|
453
588
|
# Set before writing: a write that fails may still have sent some bytes
|
|
454
589
|
@request_sent = true if @request_in_progress
|
|
455
|
-
|
|
590
|
+
data = @write_buffer.size == 1 ? @write_buffer.first : joined_write_buffer
|
|
591
|
+
@unanswered_bytes += data.bytesize
|
|
592
|
+
if @unanswered_bytes <= BLOCKING_WRITE_BYTES
|
|
593
|
+
write_blocking(data)
|
|
594
|
+
else
|
|
595
|
+
send_while_reading(data)
|
|
596
|
+
end
|
|
456
597
|
discard_write_buffer
|
|
457
598
|
end
|
|
458
599
|
|
|
600
|
+
# The write every request makes, kept free of a block outside TLS: a
|
|
601
|
+
# block here measurably slowed each request
|
|
602
|
+
def write_blocking(data)
|
|
603
|
+
return @sock.write(data) unless @sock.is_a?(Dalli::Socket::SSLSocket)
|
|
604
|
+
|
|
605
|
+
Timeout.timeout(@request_timeout || socket_timeout) { @sock.write(data) }
|
|
606
|
+
end
|
|
607
|
+
|
|
608
|
+
# Sends without blocking, holding the replies that arrive meanwhile (see
|
|
609
|
+
# HeldReplies). Fails after socket_timeout (or the time a with_deadline
|
|
610
|
+
# block leaves, when that is shorter) with nothing sent, as a
|
|
611
|
+
# blocking write does, or when a held reply isn't one the requests sent
|
|
612
|
+
# could get.
|
|
613
|
+
def send_while_reading(data)
|
|
614
|
+
return write_blocking(data) if HALF_DUPLEX_TLS && @sock.is_a?(OpenSSL::SSL::SSLSocket)
|
|
615
|
+
|
|
616
|
+
total = data.bytesize
|
|
617
|
+
sent = 0
|
|
618
|
+
deadline = nil
|
|
619
|
+
counted = false
|
|
620
|
+
while sent < total
|
|
621
|
+
rest = sent.zero? ? data : data.byteslice(sent, total - sent)
|
|
622
|
+
result = @sock.write_nonblock(rest, exception: false)
|
|
623
|
+
if result.is_a?(Integer)
|
|
624
|
+
sent += result
|
|
625
|
+
deadline = nil
|
|
626
|
+
else
|
|
627
|
+
# Replies can only be held from here, so the lines are counted now
|
|
628
|
+
counted ||= (@held_replies ||= HeldReplies.new(max_reply_value_bytes)).count_lines(data)
|
|
629
|
+
deadline ||= write_deadline
|
|
630
|
+
wait_to_send(result, deadline)
|
|
631
|
+
end
|
|
632
|
+
end
|
|
633
|
+
end
|
|
634
|
+
|
|
635
|
+
# Waits until the socket takes more, holding any replies that arrive.
|
|
636
|
+
# TLS can need the other direction first, and the socket is ready for
|
|
637
|
+
# the direction it doesn't need, so waiting on that too would return at
|
|
638
|
+
# once: a write can need to read (:wait_readable), and a read can need
|
|
639
|
+
# to write (:wait_writable).
|
|
640
|
+
def wait_to_send(wait_for, deadline)
|
|
641
|
+
readable, = wait_until(deadline, [@sock], wait_for == :wait_writable ? [@sock] : nil)
|
|
642
|
+
return if readable.empty?
|
|
643
|
+
|
|
644
|
+
wait_until(deadline, nil, [@sock]) if hold_arrived_replies == :wait_writable
|
|
645
|
+
end
|
|
646
|
+
|
|
647
|
+
def wait_until(deadline, readers, writers)
|
|
648
|
+
wait = deadline && (deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC))
|
|
649
|
+
raise Timeout::Error, "IO timeout writing to #{name}" if wait && wait <= 0
|
|
650
|
+
|
|
651
|
+
IO.select(readers, writers, nil, wait) or raise Timeout::Error, "IO timeout writing to #{name}"
|
|
652
|
+
end
|
|
653
|
+
|
|
654
|
+
def write_deadline
|
|
655
|
+
timeout = @request_timeout || socket_timeout
|
|
656
|
+
timeout && (Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout)
|
|
657
|
+
end
|
|
658
|
+
|
|
659
|
+
def max_reply_value_bytes
|
|
660
|
+
@options.fetch(:max_reply_value_bytes, MAX_READ_BYTES - 2)
|
|
661
|
+
end
|
|
662
|
+
|
|
663
|
+
# Returns what read_nonblock did when nothing was held
|
|
664
|
+
def hold_arrived_replies
|
|
665
|
+
result = @held_replies.read_from(@sock, @unanswered_bytes)
|
|
666
|
+
@held = true if result.nil?
|
|
667
|
+
result
|
|
668
|
+
rescue HeldReplies::Invalid => e
|
|
669
|
+
error_on_request!("#{name} sent #{e.message}")
|
|
670
|
+
end
|
|
671
|
+
|
|
672
|
+
# A held reply line. One that continues on the socket is finished from
|
|
673
|
+
# there. A CR that ends the held bytes may be the first half of its CRLF,
|
|
674
|
+
# which gets wouldn't find across the two.
|
|
675
|
+
def held_line
|
|
676
|
+
length, whole = @held_replies.next_line_length
|
|
677
|
+
line_too_long! if length > MAX_LINE_BYTES
|
|
678
|
+
return take_held(length) if whole
|
|
679
|
+
|
|
680
|
+
line = take_held(length)
|
|
681
|
+
line << read(1) while line.end_with?("\r") && line.bytesize <= MAX_LINE_BYTES
|
|
682
|
+
line << read_line unless line.end_with?("\r\n")
|
|
683
|
+
line_too_long! if line.bytesize > MAX_LINE_BYTES
|
|
684
|
+
line
|
|
685
|
+
end
|
|
686
|
+
|
|
687
|
+
# count bytes, the held ones first
|
|
688
|
+
def held_read(count)
|
|
689
|
+
check_read_size!(count)
|
|
690
|
+
held = @held_replies.bytesize
|
|
691
|
+
return take_held(count) if held >= count
|
|
692
|
+
|
|
693
|
+
value = take_held(held)
|
|
694
|
+
value << read(count - held)
|
|
695
|
+
end
|
|
696
|
+
|
|
697
|
+
def take_held(count)
|
|
698
|
+
part = @held_replies.take(count)
|
|
699
|
+
@held = false if @held_replies.empty?
|
|
700
|
+
part
|
|
701
|
+
end
|
|
702
|
+
|
|
703
|
+
def discard_held
|
|
704
|
+
@held = false
|
|
705
|
+
@held_replies&.clear
|
|
706
|
+
end
|
|
707
|
+
|
|
708
|
+
def line_too_long!
|
|
709
|
+
error_on_request!("Reply line without CRLF in the first #{MAX_LINE_BYTES} bytes")
|
|
710
|
+
end
|
|
711
|
+
|
|
459
712
|
# Joined as bytes: requests can mix UTF-8 and binary strings, which
|
|
460
713
|
# String#<< refuses to combine when both hold non-ASCII bytes
|
|
461
714
|
if String.method_defined?(:append_as_bytes)
|
|
@@ -509,7 +762,8 @@ module Dalli
|
|
|
509
762
|
flush_write_buffer
|
|
510
763
|
if @awaiting_reply
|
|
511
764
|
@awaiting_reply = false
|
|
512
|
-
|
|
765
|
+
# A reply held while the request was sent has already arrived
|
|
766
|
+
wait_readable! unless @held || @sock.is_a?(::OpenSSL::SSL::SSLSocket)
|
|
513
767
|
end
|
|
514
768
|
super
|
|
515
769
|
rescue SystemCallError, *TIMEOUT_ERRORS, *SSL_ERRORS, EOFError => e
|
|
@@ -524,7 +778,8 @@ module Dalli
|
|
|
524
778
|
end
|
|
525
779
|
|
|
526
780
|
def wait_readable!
|
|
527
|
-
@sock.wait_readable(socket_timeout) or
|
|
781
|
+
@sock.wait_readable(@request_timeout || socket_timeout) or
|
|
782
|
+
raise Timeout::Error, "IO timeout reading from #{name}"
|
|
528
783
|
end
|
|
529
784
|
end
|
|
530
785
|
end
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Dalli
|
|
4
|
+
module Protocol
|
|
5
|
+
##
|
|
6
|
+
# Replies that arrived while a request was being sent (see
|
|
7
|
+
# ConnectionManager#send_while_reading), held until the request's reader
|
|
8
|
+
# gets to them. Created only once a send has had to wait, so connections
|
|
9
|
+
# that never send a large request don't carry its state.
|
|
10
|
+
#
|
|
11
|
+
# Held bytes are checked as they arrive, so that what's held is never more
|
|
12
|
+
# than the requests sent could get back: whole reply lines of up to
|
|
13
|
+
# ConnectionManager::MAX_LINE_BYTES, no more replies than request lines
|
|
14
|
+
# sent, and values no larger than max_response_bytes. Anything else raises
|
|
15
|
+
# Invalid as soon as it's read.
|
|
16
|
+
##
|
|
17
|
+
class HeldReplies
|
|
18
|
+
class Invalid < StandardError; end
|
|
19
|
+
|
|
20
|
+
# Read per wakeup: enough to empty a Unix socket's send buffer (208 KiB
|
|
21
|
+
# by default on Linux)
|
|
22
|
+
READ_BYTES = 256 * 1024
|
|
23
|
+
|
|
24
|
+
def initialize(max_value_bytes)
|
|
25
|
+
@max_value_bytes = max_value_bytes
|
|
26
|
+
@buffer = ''.b
|
|
27
|
+
@offset = 0
|
|
28
|
+
@chunk = nil
|
|
29
|
+
@line = ''.b
|
|
30
|
+
reset_accounting
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# Whether lines were counted since the server last answered everything
|
|
34
|
+
def counting?
|
|
35
|
+
@counting
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Each request line can get one reply. Only the lines of a send that had
|
|
39
|
+
# to wait are counted: the socket took the others at once, as it does a
|
|
40
|
+
# blocking write's, and each of their bytes is allowed one.
|
|
41
|
+
def count_lines(data)
|
|
42
|
+
@counting = true
|
|
43
|
+
@counted_bytes += data.bytesize
|
|
44
|
+
@lines += data.count("\n")
|
|
45
|
+
true
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# The server has answered everything sent so far
|
|
49
|
+
def reset_accounting
|
|
50
|
+
@counting = false
|
|
51
|
+
@counted_bytes = 0
|
|
52
|
+
@lines = 0
|
|
53
|
+
@replies = 0
|
|
54
|
+
@value_left = 0
|
|
55
|
+
@line.clear
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# Moves the replies that have arrived on sock into the buffer, reading
|
|
59
|
+
# into it directly when it's empty, then checks them. Returns nil once
|
|
60
|
+
# bytes are held, or what read_nonblock returned when none were.
|
|
61
|
+
# unanswered_bytes is everything sent since the server last answered.
|
|
62
|
+
def read_from(sock, unanswered_bytes)
|
|
63
|
+
if empty?
|
|
64
|
+
@offset = 0
|
|
65
|
+
from = 0
|
|
66
|
+
result = sock.read_nonblock(READ_BYTES, @buffer, exception: false)
|
|
67
|
+
else
|
|
68
|
+
compact
|
|
69
|
+
from = @buffer.bytesize
|
|
70
|
+
result = sock.read_nonblock(READ_BYTES, @chunk ||= ''.b, exception: false)
|
|
71
|
+
@buffer << result if result.is_a?(String)
|
|
72
|
+
end
|
|
73
|
+
raise EOFError, 'EOF while sending' if result.nil?
|
|
74
|
+
# Nothing held: TLS read a handshake record, or nothing had arrived
|
|
75
|
+
return result unless result.is_a?(String)
|
|
76
|
+
|
|
77
|
+
check(from, unanswered_bytes)
|
|
78
|
+
nil
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def empty?
|
|
82
|
+
@offset == @buffer.bytesize
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def bytesize
|
|
86
|
+
@buffer.bytesize - @offset
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# The length of the next line, CRLF included, and whether its CRLF is
|
|
90
|
+
# held. Without it, the line runs to the end of what's held.
|
|
91
|
+
def next_line_length
|
|
92
|
+
eol = @buffer.byteindex("\r\n", @offset)
|
|
93
|
+
eol ? [eol + 2 - @offset, true] : [bytesize, false]
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# The next count bytes. Bytes that reach the end are copied: a byteslice
|
|
97
|
+
# there would share the buffer's memory and keep it all allocated.
|
|
98
|
+
def take(count)
|
|
99
|
+
if @offset + count < @buffer.bytesize
|
|
100
|
+
part = @buffer.byteslice(@offset, count)
|
|
101
|
+
@offset += count
|
|
102
|
+
else
|
|
103
|
+
part = String.new(capacity: count, encoding: Encoding::BINARY)
|
|
104
|
+
part.bytesplice(0, 0, @buffer, @offset, count)
|
|
105
|
+
clear
|
|
106
|
+
end
|
|
107
|
+
part
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def clear
|
|
111
|
+
@buffer.clear
|
|
112
|
+
@offset = 0
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
private
|
|
116
|
+
|
|
117
|
+
def compact
|
|
118
|
+
return unless @offset > COMPACT_THRESHOLD && @offset > (@buffer.bytesize / 2)
|
|
119
|
+
|
|
120
|
+
@buffer.bytesplice(0, @offset, '')
|
|
121
|
+
@offset = 0
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# Checks the held bytes from pos, keeping its place in a line or value
|
|
125
|
+
# that continues in the next read
|
|
126
|
+
def check(pos, unanswered_bytes)
|
|
127
|
+
size = @buffer.bytesize
|
|
128
|
+
while pos < size
|
|
129
|
+
if @value_left.positive?
|
|
130
|
+
step = [@value_left, size - pos].min
|
|
131
|
+
@value_left -= step
|
|
132
|
+
pos += step
|
|
133
|
+
next
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
eol = @buffer.byteindex("\n", pos)
|
|
137
|
+
@line << @buffer.byteslice(pos, (eol ? eol + 1 : size) - pos)
|
|
138
|
+
line_too_long! if @line.bytesize > ConnectionManager::MAX_LINE_BYTES
|
|
139
|
+
return unless eol
|
|
140
|
+
|
|
141
|
+
pos = eol + 1
|
|
142
|
+
check_reply(@line, unanswered_bytes)
|
|
143
|
+
@line.clear
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def check_reply(line, unanswered_bytes)
|
|
148
|
+
line_too_long! unless line.end_with?("\r\n")
|
|
149
|
+
|
|
150
|
+
@replies += 1
|
|
151
|
+
if @replies > unanswered_bytes - @counted_bytes + @lines
|
|
152
|
+
raise Invalid, 'more replies than requests while they were being sent'
|
|
153
|
+
end
|
|
154
|
+
return unless line.start_with?('VA ')
|
|
155
|
+
|
|
156
|
+
size = line[/\AVA (\d+)[ \r]/, 1]&.to_i
|
|
157
|
+
unless size && size <= @max_value_bytes
|
|
158
|
+
raise Invalid, 'a value larger than max_response_bytes while a request was being sent'
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
@value_left = size + 2
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def line_too_long!
|
|
165
|
+
raise Invalid, "a reply line without CRLF in the first #{ConnectionManager::MAX_LINE_BYTES} bytes"
|
|
166
|
+
end
|
|
167
|
+
end
|
|
168
|
+
end
|
|
169
|
+
end
|
data/lib/dalli/version.rb
CHANGED
data/lib/dalli.rb
CHANGED
|
@@ -47,6 +47,10 @@ module Dalli
|
|
|
47
47
|
# server's replies can't be trusted
|
|
48
48
|
class UnexpectedReplyKeyError < DalliError; end
|
|
49
49
|
|
|
50
|
+
# a Client#with_deadline block ran out of time. It isn't a NetworkError: the
|
|
51
|
+
# call isn't retried or failed over, and the server isn't counted as failing.
|
|
52
|
+
class DeadlineExceededError < DalliError; end
|
|
53
|
+
|
|
50
54
|
# Implements the NullObject pattern to store an application-defined value for 'Key not found' responses.
|
|
51
55
|
class NilObject; end # rubocop:disable Lint/EmptyClass
|
|
52
56
|
NOT_FOUND = NilObject.new
|
|
@@ -82,6 +86,7 @@ require_relative 'dalli/json_serializer'
|
|
|
82
86
|
require_relative 'dalli/compressor'
|
|
83
87
|
require_relative 'dalli/cache_result'
|
|
84
88
|
require_relative 'dalli/client'
|
|
89
|
+
require_relative 'dalli/deadline'
|
|
85
90
|
require_relative 'dalli/key_manager'
|
|
86
91
|
require_relative 'dalli/key_grouping'
|
|
87
92
|
require_relative 'dalli/pipelined_getter'
|
|
@@ -91,6 +96,7 @@ require_relative 'dalli/ring'
|
|
|
91
96
|
require_relative 'dalli/protocol'
|
|
92
97
|
require_relative 'dalli/protocol/base'
|
|
93
98
|
require_relative 'dalli/protocol/connection_manager'
|
|
99
|
+
require_relative 'dalli/protocol/held_replies'
|
|
94
100
|
require_relative 'dalli/protocol/meta'
|
|
95
101
|
require_relative 'dalli/protocol/response_buffer'
|
|
96
102
|
require_relative 'dalli/protocol/server_config_parser'
|
metadata
CHANGED
|
@@ -1,96 +1,102 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: dalli
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 5.
|
|
4
|
+
version: 5.4.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
|
-
- Peter M. Goldstein
|
|
8
|
-
- Mike Perham
|
|
7
|
+
- Peter M. Goldstein
|
|
8
|
+
- Mike Perham
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
11
|
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
|
-
- !ruby/object:Gem::Dependency
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: logger
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
-
|
|
18
|
+
- ">="
|
|
19
|
+
- !ruby/object:Gem::Version
|
|
20
|
+
version: "0"
|
|
21
|
+
type: :runtime
|
|
22
|
+
prerelease: false
|
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
24
|
+
requirements:
|
|
25
|
+
-
|
|
26
|
+
- ">="
|
|
27
|
+
- !ruby/object:Gem::Version
|
|
28
|
+
version: "0"
|
|
27
29
|
description: High performance memcached client for Ruby
|
|
28
30
|
email:
|
|
29
|
-
- peter.m.goldstein@gmail.com
|
|
30
|
-
- mperham@gmail.com
|
|
31
|
+
- peter.m.goldstein@gmail.com
|
|
32
|
+
- mperham@gmail.com
|
|
31
33
|
executables: []
|
|
32
34
|
extensions: []
|
|
33
35
|
extra_rdoc_files: []
|
|
34
36
|
files:
|
|
35
|
-
- CHANGELOG.md
|
|
36
|
-
- Gemfile
|
|
37
|
-
- LICENSE
|
|
38
|
-
- README.md
|
|
39
|
-
- lib/dalli.rb
|
|
40
|
-
- lib/dalli/cache_result.rb
|
|
41
|
-
- lib/dalli/cas/client.rb
|
|
42
|
-
- lib/dalli/client.rb
|
|
43
|
-
- lib/dalli/compressor.rb
|
|
44
|
-
- lib/dalli/
|
|
45
|
-
- lib/dalli/
|
|
46
|
-
- lib/dalli/
|
|
47
|
-
- lib/dalli/
|
|
48
|
-
- lib/dalli/
|
|
49
|
-
- lib/dalli/
|
|
50
|
-
- lib/dalli/
|
|
51
|
-
- lib/dalli/
|
|
52
|
-
- lib/dalli/
|
|
53
|
-
- lib/dalli/
|
|
54
|
-
- lib/dalli/protocol
|
|
55
|
-
- lib/dalli/protocol/
|
|
56
|
-
- lib/dalli/protocol/
|
|
57
|
-
- lib/dalli/protocol/
|
|
58
|
-
- lib/dalli/protocol/
|
|
59
|
-
- lib/dalli/protocol/
|
|
60
|
-
- lib/dalli/protocol/
|
|
61
|
-
- lib/dalli/protocol/
|
|
62
|
-
- lib/dalli/protocol/
|
|
63
|
-
- lib/dalli/protocol/
|
|
64
|
-
- lib/dalli/protocol/
|
|
65
|
-
- lib/dalli/protocol/
|
|
66
|
-
- lib/dalli/protocol/
|
|
67
|
-
- lib/dalli/
|
|
68
|
-
- lib/dalli/
|
|
69
|
-
- lib/dalli/
|
|
70
|
-
- lib/dalli/
|
|
71
|
-
- lib/
|
|
72
|
-
|
|
37
|
+
- CHANGELOG.md
|
|
38
|
+
- Gemfile
|
|
39
|
+
- LICENSE
|
|
40
|
+
- README.md
|
|
41
|
+
- lib/dalli.rb
|
|
42
|
+
- lib/dalli/cache_result.rb
|
|
43
|
+
- lib/dalli/cas/client.rb
|
|
44
|
+
- lib/dalli/client.rb
|
|
45
|
+
- lib/dalli/compressor.rb
|
|
46
|
+
- lib/dalli/deadline.rb
|
|
47
|
+
- lib/dalli/flags.rb
|
|
48
|
+
- lib/dalli/instrumentation.rb
|
|
49
|
+
- lib/dalli/json_serializer.rb
|
|
50
|
+
- lib/dalli/key_grouping.rb
|
|
51
|
+
- lib/dalli/key_manager.rb
|
|
52
|
+
- lib/dalli/options.rb
|
|
53
|
+
- lib/dalli/pipelined_deleter.rb
|
|
54
|
+
- lib/dalli/pipelined_getter.rb
|
|
55
|
+
- lib/dalli/pipelined_setter.rb
|
|
56
|
+
- lib/dalli/protocol.rb
|
|
57
|
+
- lib/dalli/protocol/base.rb
|
|
58
|
+
- lib/dalli/protocol/connection_manager.rb
|
|
59
|
+
- lib/dalli/protocol/held_replies.rb
|
|
60
|
+
- lib/dalli/protocol/key_regularizer.rb
|
|
61
|
+
- lib/dalli/protocol/meta.rb
|
|
62
|
+
- lib/dalli/protocol/request_formatter.rb
|
|
63
|
+
- lib/dalli/protocol/response_buffer.rb
|
|
64
|
+
- lib/dalli/protocol/response_processor.rb
|
|
65
|
+
- lib/dalli/protocol/server_config_parser.rb
|
|
66
|
+
- lib/dalli/protocol/string_marshaller.rb
|
|
67
|
+
- lib/dalli/protocol/ttl_sanitizer.rb
|
|
68
|
+
- lib/dalli/protocol/value_compressor.rb
|
|
69
|
+
- lib/dalli/protocol/value_marshaller.rb
|
|
70
|
+
- lib/dalli/protocol/value_serializer.rb
|
|
71
|
+
- lib/dalli/ring.rb
|
|
72
|
+
- lib/dalli/servers_arg_normalizer.rb
|
|
73
|
+
- lib/dalli/socket.rb
|
|
74
|
+
- lib/dalli/version.rb
|
|
75
|
+
- lib/rack/session/dalli.rb
|
|
76
|
+
homepage: "https://github.com/petergoldstein/dalli"
|
|
73
77
|
licenses:
|
|
74
|
-
- MIT
|
|
78
|
+
- MIT
|
|
75
79
|
metadata:
|
|
76
|
-
bug_tracker_uri: https://github.com/petergoldstein/dalli/issues
|
|
77
|
-
changelog_uri: https://github.com/petergoldstein/dalli/blob/main/CHANGELOG.md
|
|
78
|
-
rubygems_mfa_required:
|
|
80
|
+
bug_tracker_uri: "https://github.com/petergoldstein/dalli/issues"
|
|
81
|
+
changelog_uri: "https://github.com/petergoldstein/dalli/blob/main/CHANGELOG.md"
|
|
82
|
+
rubygems_mfa_required: "true"
|
|
79
83
|
rdoc_options: []
|
|
80
84
|
require_paths:
|
|
81
|
-
- lib
|
|
85
|
+
- lib
|
|
82
86
|
required_ruby_version: !ruby/object:Gem::Requirement
|
|
83
87
|
requirements:
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
88
|
+
-
|
|
89
|
+
- ">="
|
|
90
|
+
- !ruby/object:Gem::Version
|
|
91
|
+
version: "3.3"
|
|
87
92
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
88
93
|
requirements:
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
94
|
+
-
|
|
95
|
+
- ">="
|
|
96
|
+
- !ruby/object:Gem::Version
|
|
97
|
+
version: "0"
|
|
92
98
|
requirements: []
|
|
93
|
-
rubygems_version: 4.0
|
|
99
|
+
rubygems_version: 4.1.0
|
|
94
100
|
specification_version: 4
|
|
95
101
|
summary: High performance memcached client for Ruby
|
|
96
102
|
test_files: []
|