dalli 5.3.4 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bcdbff6c85bb9b36e985afe7760f3a30b347e7e9cac5ab91220d1df40fbe9321
4
- data.tar.gz: cd31ee942d757acd20c3dd6686bfb16ce8f5ace3246df87d3bb8d0152f9bd140
3
+ metadata.gz: a9ff0cea6b9397fdb4bda61cb43998982e5972b859931780354a6540bf975187
4
+ data.tar.gz: 345a0407d7aaf23515b4711ba5469b19d43512781bb69413b813ac183186f3bd
5
5
  SHA512:
6
- metadata.gz: e617006601c20a575781cb74c2393b79fe4b06d4179f44bf2cc87a150396843fbf57181e91040d8060f0e572a7bd27d7de82bf748da125eaadcdfb2c5ae25a38
7
- data.tar.gz: 1981431f621ca9f4ad053d0df4bd712bdddd0c4e5847545cfe7bee03446a70b3c3ecd472eda799a1c33e3c61331b6114ee497b699d85431c0152a969536693ee
6
+ metadata.gz: 4912d5a7481d10b7ca10257d21408496db84f54ac92c08102934ba29575d661e5b3c03ffb669736d72ea6f631ff71815351e37d7b43df3287ca6c83393f9ee40
7
+ data.tar.gz: 93309cfff89840ba1b4c4c1b9d7824f4496044877f1b509b008a3175db73d40c076d58b3acfadc566fb5c79fc84756ab8a7170f9f8f36da64344d010c23c4077
data/CHANGELOG.md CHANGED
@@ -4,6 +4,13 @@ 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
+
7
14
  5.3.4
8
15
  ==========
9
16
 
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.
@@ -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
- fetch_responses(servers, @ring.socket_timeout, &block) until servers.empty?
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
- def fetch_responses(servers, timeout, &block)
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
- readable_servers = servers_with_response(servers, timeout)
199
- time_out(servers) if readable_servers.empty?
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
@@ -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 }
@@ -66,6 +66,7 @@ module Dalli
66
66
 
67
67
  # Chokepoint method for error handling and ensuring liveness
68
68
  def request(opkey, *args)
69
+ @connection_manager.check_deadline!
69
70
  verify_state(opkey)
70
71
 
71
72
  begin
@@ -440,8 +441,10 @@ module Dalli
440
441
  end
441
442
 
442
443
  def connect
443
- @connection_manager.establish_connection
444
- @version = version
444
+ @connection_manager.within_connect_deadline do
445
+ @connection_manager.establish_connection
446
+ @version = version
447
+ end
445
448
  up!
446
449
  end
447
450
 
@@ -60,6 +60,11 @@ module Dalli
60
60
  @unanswered_bytes = 0
61
61
 
62
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
63
68
  reset_down_info
64
69
  extend ReadTimeout if @options[:enforce_read_timeout] && RUBY_ENGINE != 'ruby'
65
70
  end
@@ -238,6 +243,40 @@ module Dalli
238
243
 
239
244
  @request_in_progress = true
240
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
241
280
  end
242
281
 
243
282
  def finish_request!
@@ -245,6 +284,7 @@ module Dalli
245
284
 
246
285
  @request_in_progress = false
247
286
  @request_sent = false
287
+ restore_timeout if @request_timeout
248
288
  # A completed request is what proves the server healthy again, so the
249
289
  # failure count resets here rather than on reconnect: a server that
250
290
  # accepts connections but never answers would otherwise reset it on
@@ -263,11 +303,13 @@ module Dalli
263
303
 
264
304
  @request_in_progress = false
265
305
  @request_sent = false
306
+ restore_timeout if @request_timeout
266
307
  end
267
308
 
268
309
  def abort_request!
269
310
  @request_in_progress = false
270
311
  @request_sent = false
312
+ restore_timeout if @request_timeout
271
313
  end
272
314
 
273
315
  # True once any of the request in progress has been handed to the
@@ -407,6 +449,7 @@ module Dalli
407
449
  end
408
450
 
409
451
  def error_on_request!(err_or_string)
452
+ deadline_exceeded!(err_or_string) if @request_timeout && timeout_error?(err_or_string)
410
453
  log_warn_message(err_or_string)
411
454
 
412
455
  @fail_count += 1
@@ -492,7 +535,40 @@ module Dalli
492
535
  def timed_tls_operation(&)
493
536
  return yield unless @sock.is_a?(Dalli::Socket::SSLSocket)
494
537
 
495
- 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}"
496
572
  end
497
573
 
498
574
  # One write per flush. A TLS socket sends each argument of
@@ -526,11 +602,12 @@ module Dalli
526
602
  def write_blocking(data)
527
603
  return @sock.write(data) unless @sock.is_a?(Dalli::Socket::SSLSocket)
528
604
 
529
- Timeout.timeout(socket_timeout) { @sock.write(data) }
605
+ Timeout.timeout(@request_timeout || socket_timeout) { @sock.write(data) }
530
606
  end
531
607
 
532
608
  # Sends without blocking, holding the replies that arrive meanwhile (see
533
- # HeldReplies). Fails after socket_timeout with nothing sent, as a
609
+ # HeldReplies). Fails after socket_timeout (or the time a with_deadline
610
+ # block leaves, when that is shorter) with nothing sent, as a
534
611
  # blocking write does, or when a held reply isn't one the requests sent
535
612
  # could get.
536
613
  def send_while_reading(data)
@@ -575,7 +652,8 @@ module Dalli
575
652
  end
576
653
 
577
654
  def write_deadline
578
- socket_timeout && (Process.clock_gettime(Process::CLOCK_MONOTONIC) + socket_timeout)
655
+ timeout = @request_timeout || socket_timeout
656
+ timeout && (Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout)
579
657
  end
580
658
 
581
659
  def max_reply_value_bytes
@@ -700,7 +778,8 @@ module Dalli
700
778
  end
701
779
 
702
780
  def wait_readable!
703
- @sock.wait_readable(socket_timeout) or raise Timeout::Error, "IO timeout reading from #{name}"
781
+ @sock.wait_readable(@request_timeout || socket_timeout) or
782
+ raise Timeout::Error, "IO timeout reading from #{name}"
704
783
  end
705
784
  end
706
785
  end
data/lib/dalli/version.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Dalli
4
- VERSION = '5.3.4'
4
+ VERSION = '5.4.0'
5
5
 
6
6
  MIN_SUPPORTED_MEMCACHED_VERSION = '1.6.27'
7
7
  end
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'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dalli
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.3.4
4
+ version: 5.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter M. Goldstein
@@ -43,6 +43,7 @@ files:
43
43
  - lib/dalli/cas/client.rb
44
44
  - lib/dalli/client.rb
45
45
  - lib/dalli/compressor.rb
46
+ - lib/dalli/deadline.rb
46
47
  - lib/dalli/flags.rb
47
48
  - lib/dalli/instrumentation.rb
48
49
  - lib/dalli/json_serializer.rb