dalli 5.1.10 → 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.
data/lib/dalli/ring.rb CHANGED
@@ -36,9 +36,10 @@ module Dalli
36
36
 
37
37
  def continuum=(entries)
38
38
  @continuum = entries
39
- # Plain integers, so the binary search in server_for_hash_key needn't
40
- # call Entry#value at each step
39
+ # Plain integers, so the lookup in server_for_hash_key needn't call
40
+ # Entry#value at each step
41
41
  @continuum_values = entries&.map(&:value)
42
+ build_buckets
42
43
  end
43
44
 
44
45
  # alive_cache (optional) remembers each server's alive? result, so a
@@ -55,15 +56,17 @@ module Dalli
55
56
  raise Dalli::RingError, 'No server available'
56
57
  end
57
58
 
59
+ # Tries the key's own server first, then (with failover) up to 19
60
+ # rehashes of "<try><key>". The first try is outside the loop because it
61
+ # is almost always the one that's used.
58
62
  def server_from_continuum(key, alive_cache = nil)
59
- hkey = hash_for(key)
60
- 20.times do |try|
61
- server = server_for_hash_key(hkey)
63
+ server = server_for_hash_key(hash_for(key))
64
+ return server if server_alive?(server, alive_cache)
65
+ return nil unless @failover
62
66
 
67
+ 19.times do |try|
68
+ server = server_for_hash_key(hash_for("#{try}#{key}"))
63
69
  return server if server_alive?(server, alive_cache)
64
- break unless @failover
65
-
66
- hkey = hash_for("#{try}#{key}")
67
70
  end
68
71
  nil
69
72
  end
@@ -87,8 +90,13 @@ module Dalli
87
90
  end
88
91
  end
89
92
 
93
+ # Drains the replies left by quiet requests. Only servers that were sent a
94
+ # quiet request need it, so the others (and servers never connected) are
95
+ # skipped rather than each costing a noop round trip.
90
96
  def pipeline_consume_and_ignore_responses
91
97
  @servers.each do |s|
98
+ next unless s.quiet_responses_pending?
99
+
92
100
  s.request(:noop)
93
101
  rescue Dalli::NetworkError
94
102
  # Ignore this error, as it indicates the socket is unavailable
@@ -121,22 +129,41 @@ module Dalli
121
129
  def server_alive?(server, alive_cache)
122
130
  return server.alive? unless alive_cache
123
131
 
124
- alive_cache.fetch(server) { alive_cache[server] = server.alive? }
132
+ alive = alive_cache[server]
133
+ alive.nil? ? (alive_cache[server] = server.alive?) : alive
125
134
  end
126
135
 
127
136
  def entry_count_for(server, total_servers, total_weight)
128
137
  ((total_servers * POINTS_PER_SERVER * server.weight) / Float(total_weight)).floor
129
138
  end
130
139
 
131
- def server_for_hash_key(hash_key)
132
- # Find the closest index in the Ring with value <= the given value
133
- entryidx = @continuum_values.bsearch_index { |value| value > hash_key }
134
- if entryidx.nil?
135
- entryidx = @continuum.size - 1
136
- else
137
- entryidx -= 1
140
+ # Hash values are 32 bits. Splitting that range into about one bucket per
141
+ # continuum entry, and noting the first entry at or after each bucket's
142
+ # start, lets server_for_hash_key skip the binary search: it starts at the
143
+ # key's bucket and steps over the few entries before the key's hash.
144
+ def build_buckets
145
+ return unless @continuum_values
146
+
147
+ bits = [@continuum_values.size.bit_length, 1].max
148
+ @bucket_shift = 32 - bits
149
+ @bucket_first = Array.new(1 << bits)
150
+ idx = 0
151
+ size = @continuum_values.size
152
+ @bucket_first.each_index do |bucket|
153
+ floor = bucket << @bucket_shift
154
+ idx += 1 while idx < size && @continuum_values[idx] < floor
155
+ @bucket_first[bucket] = idx
138
156
  end
139
- @continuum[entryidx].server
157
+ end
158
+
159
+ def server_for_hash_key(hash_key)
160
+ # Find the last entry with value <= hash_key, wrapping around to the
161
+ # last entry when every value is greater (idx - 1 is then -1)
162
+ idx = @bucket_first[hash_key >> @bucket_shift]
163
+ values = @continuum_values
164
+ size = values.size
165
+ idx += 1 while idx < size && values[idx] <= hash_key
166
+ @continuum[idx - 1].server
140
167
  end
141
168
 
142
169
  def build_continuum(servers)
data/lib/dalli/socket.rb CHANGED
@@ -13,10 +13,6 @@ module Dalli
13
13
  ##
14
14
  module InstanceMethods
15
15
  READ_CHUNK_SIZE = 8196
16
- # Give the response parser control between bounded socket reads. In
17
- # particular, a peer continuously sending a header without CRLF must
18
- # not keep this method appending forever.
19
- MAX_AVAILABLE_BYTES = READ_CHUNK_SIZE * 8
20
16
 
21
17
  # Reads whatever is available without blocking. A read that returns less
22
18
  # than a full chunk means the socket was drained, so we stop there rather
@@ -24,7 +20,7 @@ module Dalli
24
20
  # Callers keep waiting on IO.select for anything that arrives later.
25
21
  def read_available(reusable_buffer = nil)
26
22
  if reusable_buffer
27
- value = read_nonblock([READ_CHUNK_SIZE, MAX_AVAILABLE_BYTES].min, reusable_buffer, exception: false)
23
+ value = read_nonblock(READ_CHUNK_SIZE, reusable_buffer, exception: false)
28
24
  case value
29
25
  when :wait_writable, :wait_readable
30
26
  return reusable_buffer.clear
@@ -32,15 +28,13 @@ module Dalli
32
28
  raise Errno::ECONNRESET, "Connection reset: #{logged_options.inspect}"
33
29
  end
34
30
  return value if drained_after?(value)
35
- return value if value.bytesize >= MAX_AVAILABLE_BYTES
36
31
  else
37
32
  value = ''.b
38
33
  end
39
34
 
40
35
  buffer = ''.b
41
36
  loop do
42
- result = read_nonblock([READ_CHUNK_SIZE, MAX_AVAILABLE_BYTES - value.bytesize].min,
43
- buffer, exception: false)
37
+ result = read_nonblock(READ_CHUNK_SIZE, buffer, exception: false)
44
38
  case result
45
39
  when :wait_writable, :wait_readable
46
40
  buffer.clear
@@ -50,15 +44,16 @@ module Dalli
50
44
  else
51
45
  value << result
52
46
  return value if drained_after?(result)
53
- return value if value.bytesize >= MAX_AVAILABLE_BYTES
54
47
  end
55
48
  end
56
49
  end
57
50
 
51
+ # True when a read came back short and nothing is left buffered in-process.
58
52
  def drained_after?(chunk)
59
53
  chunk.bytesize < READ_CHUNK_SIZE && !buffered_data?
60
54
  end
61
55
 
56
+ # Plain sockets keep nothing buffered in-process. See SSLSocket.
62
57
  def buffered_data?
63
58
  false
64
59
  end
@@ -109,22 +104,8 @@ module Dalli
109
104
  # OpenSSL can hold decrypted bytes that IO.select won't report as readable.
110
105
  # With OpenSSL's default (no read-ahead) a short read has already used up
111
106
  # the current record, so this is normally 0; it guards read-ahead setups.
112
- #
113
- # jruby-openssl can take the next record off the socket during a short
114
- # read and hold it where neither pending nor IO.select sees it, so on
115
- # JRuby a read continues until the socket would block.
116
- def pending_data?
117
- pending.positive? || (@rbuffer && !@rbuffer.empty?)
118
- end
119
-
120
- if RUBY_ENGINE == 'jruby'
121
- def buffered_data?
122
- true
123
- end
124
- else
125
- def buffered_data?
126
- pending_data?
127
- end
107
+ def buffered_data?
108
+ pending.positive?
128
109
  end
129
110
 
130
111
  unless method_defined?(:wait_readable)
@@ -160,12 +141,7 @@ module Dalli
160
141
  sock.options = { host: host, port: port }.merge(options)
161
142
  init_socket_options(sock, options)
162
143
 
163
- if options[:ssl_context]
164
- wrapping_ssl_socket(sock, host, options[:ssl_context],
165
- options[:socket_timeout])
166
- else
167
- sock
168
- end
144
+ options[:ssl_context] ? wrapping_ssl_socket(sock, host, options[:ssl_context]) : sock
169
145
  end
170
146
  end
171
147
 
@@ -252,43 +228,12 @@ module Dalli
252
228
  [seconds, microseconds].pack(timeval_pack_format(sock))
253
229
  end
254
230
 
255
- # Closes the TCP socket if the handshake fails or times out, so a failed
256
- # attempt doesn't hold a file descriptor until GC.
257
- def self.wrapping_ssl_socket(tcp_socket, host, ssl_context, timeout = nil)
258
- warn_if_unverified(ssl_context)
259
- connected = false
231
+ def self.wrapping_ssl_socket(tcp_socket, host, ssl_context)
260
232
  ssl_socket = Dalli::Socket::SSLSocket.new(tcp_socket, ssl_context)
261
233
  ssl_socket.hostname = host
262
234
  ssl_socket.sync_close = true
263
- # The underlying TCP socket's IO#timeout= does not reliably bound
264
- # OpenSSL's blocking handshake on Ruby 3.3.
265
- Timeout.timeout(timeout) { ssl_socket.connect }
266
- connected = true
235
+ ssl_socket.connect
267
236
  ssl_socket
268
- ensure
269
- tcp_socket.close unless connected
270
- end
271
-
272
- # Class variable to track whether the TLS verification warning has been logged
273
- @@ssl_verification_warning_logged = false # rubocop:disable Style/ClassVars
274
-
275
- # Verification comes entirely from the caller's ssl_context, and a bare
276
- # SSLContext.new verifies nothing, so warn (once per process) when the
277
- # certificate or the hostname won't be checked.
278
- def self.warn_if_unverified(ssl_context)
279
- return if @@ssl_verification_warning_logged
280
-
281
- problem = if !ssl_context.verify_mode.to_i.anybits?(::OpenSSL::SSL::VERIFY_PEER)
282
- 'verify_mode is VERIFY_NONE, so the server certificate is not checked'
283
- elsif !ssl_context.verify_hostname
284
- 'verify_hostname is false, so the certificate is not checked against the hostname'
285
- end
286
- return unless problem
287
-
288
- Dalli.logger.warn "SECURITY WARNING: Dalli's ssl_context #{problem}. " \
289
- 'Use ssl_context.set_params(verify_mode: OpenSSL::SSL::VERIFY_PEER, ' \
290
- 'verify_hostname: true) to verify the server.'
291
- @@ssl_verification_warning_logged = true # rubocop:disable Style/ClassVars
292
237
  end
293
238
  end
294
239
 
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.1.10'
4
+ VERSION = '5.2.0'
5
5
 
6
6
  MIN_SUPPORTED_MEMCACHED_VERSION = '1.6.27'
7
7
  end
data/lib/dalli.rb CHANGED
@@ -31,10 +31,6 @@ module Dalli
31
31
  # socket/server communication error that can be retried
32
32
  class RetryableNetworkError < NetworkError; end
33
33
 
34
- # a multi-get reply named a key that wasn't requested, so the rest of that
35
- # server's replies can't be trusted
36
- class UnexpectedReplyKeyError < DalliError; end
37
-
38
34
  # Implements the NullObject pattern to store an application-defined value for 'Key not found' responses.
39
35
  class NilObject; end # rubocop:disable Lint/EmptyClass
40
36
  NOT_FOUND = NilObject.new
@@ -66,8 +62,8 @@ require_relative 'dalli/version'
66
62
  require_relative 'dalli/instrumentation'
67
63
 
68
64
  require_relative 'dalli/flags'
69
- require_relative 'dalli/json_serializer'
70
65
  require_relative 'dalli/compressor'
66
+ require_relative 'dalli/cache_result'
71
67
  require_relative 'dalli/client'
72
68
  require_relative 'dalli/key_manager'
73
69
  require_relative 'dalli/pipelined_getter'
metadata CHANGED
@@ -1,15 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dalli
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.1.10
4
+ version: 5.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter M. Goldstein
8
8
  - Mike Perham
9
- autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2026-11-14 00:00:00.000000000 Z
11
+ date: 1980-01-02 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: logger
@@ -38,12 +37,12 @@ files:
38
37
  - LICENSE
39
38
  - README.md
40
39
  - lib/dalli.rb
40
+ - lib/dalli/cache_result.rb
41
41
  - lib/dalli/cas/client.rb
42
42
  - lib/dalli/client.rb
43
43
  - lib/dalli/compressor.rb
44
44
  - lib/dalli/flags.rb
45
45
  - lib/dalli/instrumentation.rb
46
- - lib/dalli/json_serializer.rb
47
46
  - lib/dalli/key_manager.rb
48
47
  - lib/dalli/options.rb
49
48
  - lib/dalli/pipelined_deleter.rb
@@ -75,7 +74,6 @@ metadata:
75
74
  bug_tracker_uri: https://github.com/petergoldstein/dalli/issues
76
75
  changelog_uri: https://github.com/petergoldstein/dalli/blob/main/CHANGELOG.md
77
76
  rubygems_mfa_required: 'true'
78
- post_install_message:
79
77
  rdoc_options: []
80
78
  require_paths:
81
79
  - lib
@@ -90,8 +88,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
90
88
  - !ruby/object:Gem::Version
91
89
  version: '0'
92
90
  requirements: []
93
- rubygems_version: 3.5.22
94
- signing_key:
91
+ rubygems_version: 4.0.21
95
92
  specification_version: 4
96
93
  summary: High performance memcached client for Ruby
97
94
  test_files: []
@@ -1,28 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'json'
4
-
5
- module Dalli
6
- ##
7
- # A serializer that stores values as JSON and never creates Ruby objects
8
- # from what it reads.
9
- #
10
- # Passing the JSON module itself as the serializer (serializer: JSON) reads
11
- # values back with JSON.load, which on json gem versions before 3.0 honors
12
- # "json_class" and instantiates other classes when the json gem's additions
13
- # are loaded. This serializer reads with JSON.parse, so a value written to
14
- # memcached by someone else can only come back as plain hashes, arrays,
15
- # strings, numbers, booleans and nil.
16
- #
17
- # Dalli::Client.new(servers, serializer: Dalli::JSONSerializer)
18
- ##
19
- module JSONSerializer
20
- def self.dump(value)
21
- JSON.generate(value)
22
- end
23
-
24
- def self.load(data)
25
- JSON.parse(data)
26
- end
27
- end
28
- end