dalli 5.0.14 → 5.1.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.
@@ -23,9 +23,6 @@ module Dalli
23
23
  VERSION = 'VERSION'
24
24
  SERVER_ERROR = 'SERVER_ERROR'
25
25
 
26
- # memcached can't store an item larger than 1 GiB (its -I maximum)
27
- MAX_VALUE_BYTES = 1024 * 1024 * 1024
28
-
29
26
  T_OK = [OK].freeze
30
27
  T_RESET = [RESET].freeze
31
28
  T_EN_HD = [EN, HD].freeze
@@ -41,25 +38,22 @@ module Dalli
41
38
  @value_marshaller = value_marshaller
42
39
  end
43
40
 
44
- # raw: true (a request made with raw: true) returns the value as stored,
45
- # ignoring any flags in the reply. The request didn't ask for flags, so
46
- # a reply carrying them anyway must not get the value deserialized.
47
- def meta_get_with_value(cache_nils: false, raw: false)
41
+ def meta_get_with_value(cache_nils: false)
48
42
  tokens = error_on_unexpected!(T_VA_EN_HD)
49
43
  return cache_nils ? ::Dalli::NOT_FOUND : nil if tokens.first == EN
50
44
  return true unless tokens.first == VA
51
45
 
52
- retrieve(read_data(tokens[1].to_i), raw ? 0 : bitflags_from_tokens(tokens), raw)
46
+ @value_marshaller.retrieve(read_data(tokens[1].to_i), bitflags_from_tokens(tokens))
53
47
  end
54
48
 
55
- def meta_get_with_value_and_cas(raw: false)
49
+ def meta_get_with_value_and_cas
56
50
  tokens = error_on_unexpected!(T_VA_EN_HD)
57
51
  return [nil, 0] if tokens.first == EN
58
52
 
59
53
  cas = cas_from_tokens(tokens)
60
54
  return [nil, cas] unless tokens.first == VA
61
55
 
62
- [retrieve(read_data(tokens[1].to_i), raw ? 0 : bitflags_from_tokens(tokens), raw), cas]
56
+ [@value_marshaller.retrieve(read_data(tokens[1].to_i), bitflags_from_tokens(tokens)), cas]
63
57
  end
64
58
 
65
59
  def meta_get_without_value
@@ -79,12 +73,13 @@ module Dalli
79
73
  # Used by meta_get for comprehensive metadata retrieval.
80
74
  # Supports thundering herd protection (N/R flags) and metadata flags (h/l/u).
81
75
  def meta_get_with_metadata(cache_nils: false, return_hit_status: false, return_last_access: false,
82
- raw: false)
76
+ return_ttl_remaining: false)
83
77
  tokens = error_on_unexpected!(T_VA_EN_HD)
84
78
  result = build_metadata_result(tokens)
85
79
  result[:hit_before] = hit_status_from_tokens(tokens) if return_hit_status
86
80
  result[:last_access] = last_access_from_tokens(tokens) if return_last_access
87
- result[:value] = parse_value_from_tokens(tokens, cache_nils, raw)
81
+ result[:ttl_remaining] = ttl_remaining_from_tokens(tokens) if return_ttl_remaining
82
+ result[:value] = parse_value_from_tokens(tokens, cache_nils)
88
83
  result
89
84
  end
90
85
 
@@ -92,20 +87,20 @@ module Dalli
92
87
  {
93
88
  value: nil, cas: cas_from_tokens(tokens),
94
89
  won_recache: tokens.include?('W'), stale: tokens.include?('X'),
95
- lost_recache: tokens.include?('Z')
90
+ lost_recache: tokens.include?('Z'),
91
+ # Explicit miss marker: EN means the key does not exist. A tombstoned
92
+ # item is NOT a miss -- it answers VA/HD with the X flag set -- and a
93
+ # stored nil under cache_nils is not one either, so neither can be
94
+ # inferred from value or cas alone.
95
+ miss: tokens.first == EN
96
96
  }
97
97
  end
98
98
 
99
- def parse_value_from_tokens(tokens, cache_nils, raw = false) # rubocop:disable Style/OptionalBooleanParameter
99
+ def parse_value_from_tokens(tokens, cache_nils)
100
100
  return cache_nils ? ::Dalli::NOT_FOUND : nil if tokens.first == EN
101
101
  return unless tokens.first == VA
102
102
 
103
- retrieve(read_data(tokens[1].to_i), raw ? 0 : bitflags_from_tokens(tokens), raw)
104
- end
105
-
106
- # Values from a raw request are returned as stored
107
- def retrieve(value, bitflags, raw)
108
- raw ? value : @value_marshaller.retrieve(value, bitflags)
103
+ @value_marshaller.retrieve(read_data(tokens[1].to_i), bitflags_from_tokens(tokens))
109
104
  end
110
105
 
111
106
  def meta_set_with_cas
@@ -187,10 +182,6 @@ module Dalli
187
182
  end
188
183
 
189
184
  def full_response_from_buffer(tokens, body, resp_size)
190
- # A hit with no key can't be matched to a request: skip it (false
191
- # status) rather than yield it, or take it for the end of the pipeline
192
- return [false, resp_size] unless tokens.any? { |t| t.start_with?('k') }
193
-
194
185
  value = @value_marshaller.retrieve(body, bitflags_from_tokens(tokens))
195
186
  [tokens.first == VA, cas_from_tokens(tokens), key_from_tokens(tokens), value, resp_size]
196
187
  end
@@ -214,21 +205,15 @@ module Dalli
214
205
  tokens = header.split
215
206
  header_len = header.bytesize + TERMINATOR.length
216
207
 
217
- # The body len is removed from the tokens array. memcached also gives
218
- # the size positionally ("VA <size> ..."); use it when there's no s
219
- # flag rather than reading the value as replies.
220
- body_len = tokens.any? { |t| t.start_with?('s') } ? body_len_from_tokens(tokens) : positional_size(tokens)
221
-
222
- # A complete response with no body. Only the terminating noop's MN
223
- # ends the pipeline. Any other bodyless reply (CLIENT_ERROR,
224
- # SERVER_ERROR, ...) answers one key's request: report it with a
225
- # false status so the caller skips it and keeps reading. Treating it
226
- # as the end would leave the remaining replies on the connection for
227
- # later commands to read as their own. A hit on an empty value (VA 0)
228
- # still has a body -- just its terminator -- so it's parsed below.
229
- return [tokens.first == MN, header_len] if body_len.zero? && tokens.first != VA
230
-
231
- check_value_size!(body_len)
208
+ # The body len is removed from the tokens array
209
+ body_len = body_len_from_tokens(tokens)
210
+
211
+ # We have a complete response that has no body.
212
+ # This is either the response to the terminating
213
+ # noop or, if the status is not MN, an intermediate
214
+ # error response that needs to be discarded.
215
+ return [true, header_len] if body_len.zero?
216
+
232
217
  resp_size = header_len + body_len + TERMINATOR.length
233
218
  # The header is in the buffer, but the body is not. As we don't have
234
219
  # a complete response, don't advance the buffer
@@ -258,6 +243,14 @@ module Dalli
258
243
  value_from_tokens(tokens, 'c').to_i
259
244
  end
260
245
 
246
+ # Detects the X presence flag, set when an item has been marked stale by a
247
+ # prior `md key I`. Uses strict equality (Array#any? with a String pattern
248
+ # compares with ==) so a future value-bearing flag beginning with X cannot
249
+ # be mistaken for it.
250
+ def stale_from_tokens(tokens)
251
+ tokens.any?('X')
252
+ end
253
+
261
254
  def key_from_tokens(tokens)
262
255
  encoded_key = value_from_tokens(tokens, 'k')
263
256
  if tokens.delete('b')
@@ -282,6 +275,12 @@ module Dalli
282
275
  value_from_tokens(tokens, 'l').to_i
283
276
  end
284
277
 
278
+ # Returns seconds of TTL remaining; -1 when the item has no expiry.
279
+ # The t flag returns t<seconds>.
280
+ def ttl_remaining_from_tokens(tokens)
281
+ value_from_tokens(tokens, 't').to_i
282
+ end
283
+
285
284
  def body_len_from_tokens(tokens)
286
285
  value_from_tokens(tokens, 's').to_i
287
286
  end
@@ -297,19 +296,6 @@ module Dalli
297
296
  end
298
297
  end
299
298
 
300
- # A pipelined reply claiming an impossible size would otherwise have the
301
- # buffer wait for (and accumulate) that many bytes
302
- # The size given right after "VA", or 0 when it isn't a number
303
- def positional_size(tokens)
304
- tokens.first == VA && tokens[1]&.match?(/\A\d+\z/) ? tokens[1].to_i : 0
305
- end
306
-
307
- def check_value_size!(size)
308
- return if size.between?(0, MAX_VALUE_BYTES)
309
-
310
- raise Dalli::DalliError, "Reply value size #{size} is out of range"
311
- end
312
-
313
299
  def read_line
314
300
  @io_source.read_line&.chomp!(TERMINATOR)
315
301
  end
@@ -320,9 +306,6 @@ module Dalli
320
306
  end
321
307
 
322
308
  def read_data(data_size)
323
- # Checked before the terminator is added: -1 or -2 would otherwise
324
- # read 1 or 0 bytes and leave the connection out of step
325
- check_value_size!(data_size)
326
309
  @io_source.read(data_size + TERMINATOR.bytesize)&.chomp!(TERMINATOR)
327
310
  end
328
311
  end
@@ -15,17 +15,11 @@ module Dalli
15
15
  compress: true,
16
16
  compressor: ::Dalli::Compressor,
17
17
  # min byte size to attempt compression
18
- compression_min_size: 4 * 1024, # 4K
19
- # max size a stored value may decompress to (nil: no limit). Guards
20
- # against a small compressed value expanding into gigabytes on read.
21
- decompressed_max_bytes: 128 * 1024 * 1024 # 128 MiB
18
+ compression_min_size: 4 * 1024 # 4K
22
19
  }.freeze
23
20
 
24
21
  OPTIONS = DEFAULTS.keys.freeze
25
22
 
26
- KEYWORD_PARAMETER_TYPES = %i[key keyreq].freeze
27
- private_constant :KEYWORD_PARAMETER_TYPES
28
-
29
23
  def initialize(client_options)
30
24
  @compression_options =
31
25
  DEFAULTS.merge(client_options.slice(*OPTIONS))
@@ -40,16 +34,8 @@ module Dalli
40
34
  end
41
35
 
42
36
  def retrieve(value, bitflags)
43
- return value unless bitflags.anybits?(Flags::COMPRESSED)
44
-
45
- max_bytes = @compression_options[:decompressed_max_bytes]
46
- # Custom compressors that only define decompress(data) keep working,
47
- # without the limit
48
- if max_bytes && compressor_accepts_max_bytes?
49
- compressor.decompress(value, max_bytes: max_bytes)
50
- else
51
- compressor.decompress(value)
52
- end
37
+ compressed = bitflags.anybits?(Flags::COMPRESSED)
38
+ compressed ? compressor.decompress(value) : value
53
39
 
54
40
  # TODO: We likely want to move this rescue into the Dalli::Compressor / Dalli::GzipCompressor
55
41
  # itself, since not all compressors necessarily use Zlib. For now keep it here, so the behavior
@@ -58,19 +44,6 @@ module Dalli
58
44
  raise UnmarshalError, "Unable to uncompress value: #{$ERROR_INFO.message}"
59
45
  end
60
46
 
61
- def compressor_accepts_max_bytes?
62
- return @compressor_accepts_max_bytes if defined?(@compressor_accepts_max_bytes)
63
-
64
- @compressor_accepts_max_bytes =
65
- begin
66
- compressor.method(:decompress).parameters.any? do |type, name|
67
- name == :max_bytes && KEYWORD_PARAMETER_TYPES.include?(type)
68
- end
69
- rescue NameError # an object without a reflectable decompress method
70
- false
71
- end
72
- end
73
-
74
47
  def compress_by_default?
75
48
  @compression_options[:compress]
76
49
  end
@@ -84,13 +57,11 @@ module Dalli
84
57
  end
85
58
 
86
59
  # Checks whether we should apply compression when serializing a value
87
- # based on the specified options. Returns false for a raw request
88
- # (raw reads ignore bitflags, so the value must be stored as given) or
89
- # unless the value is greater than the minimum compression size.
90
- # Otherwise returns based on a method-level option if specified,
91
- # falling back to the server default.
60
+ # based on the specified options. Returns false unless the value
61
+ # is greater than the minimum compression size. Otherwise returns
62
+ # based on a method-level option if specified, falling back to the
63
+ # server default.
92
64
  def compress_value?(value, req_options)
93
- return false if req_options&.dig(:raw)
94
65
  return false unless value.bytesize >= compression_min_size
95
66
  return compress_by_default? unless req_options && !req_options[:compress].nil?
96
67
 
@@ -100,7 +100,7 @@ module Dalli
100
100
  Dalli.logger.warn 'SECURITY WARNING: Dalli is using Marshal for serialization. ' \
101
101
  'Marshal can execute arbitrary code during deserialization. ' \
102
102
  'If your memcached server could be compromised, consider using ' \
103
- 'a safer serializer: Dalli::Client.new(servers, serializer: Dalli::JSONSerializer)'
103
+ 'a safer serializer like JSON: Dalli::Client.new(servers, serializer: JSON)'
104
104
  @@marshal_warning_logged = true # rubocop:disable Style/ClassVars
105
105
  end
106
106
  end
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'openssl'
4
3
  require 'timeout'
5
4
 
6
5
  module Dalli
@@ -17,11 +16,14 @@ module Dalli
17
16
  [Timeout::Error]
18
17
  end
19
18
 
20
- # SSL errors during the handshake or during read/write operations count as
21
- # a failed request: they trigger reconnection and, after
22
- # socket_max_failures, mark the server down.
23
- # openssl is required above: this file loads before dalli/socket, so a
24
- # defined? check here would find OpenSSL missing and leave this empty.
25
- SSL_ERRORS = [OpenSSL::SSL::SSLError].freeze
19
+ # SSL errors that occur during read/write operations (not during initial
20
+ # handshake) should trigger reconnection. These indicate transient network
21
+ # issues, not configuration problems.
22
+ SSL_ERRORS =
23
+ if defined?(OpenSSL::SSL::SSLError)
24
+ [OpenSSL::SSL::SSLError]
25
+ else
26
+ []
27
+ end
26
28
  end
27
29
  end
data/lib/dalli/socket.rb CHANGED
@@ -124,7 +124,7 @@ module Dalli
124
124
  # Returns true for an unmodified TCPSocket on Ruby 3.0+, or for resolv-replace >= 0.2.0
125
125
  # which forwards keyword arguments through its patch.
126
126
  # Returns false when monkey-patched by gems like socksify or resolv-replace < 0.2.0.
127
- # rubocop:disable-next ThreadSafety/ClassInstanceVariable
127
+ # rubocop:disable ThreadSafety/ClassInstanceVariable
128
128
  def self.supports_connect_timeout?
129
129
  return @supports_connect_timeout if defined?(@supports_connect_timeout)
130
130
 
@@ -135,6 +135,7 @@ module Dalli
135
135
  end
136
136
  end
137
137
  end
138
+ # rubocop:enable ThreadSafety/ClassInstanceVariable
138
139
 
139
140
  def self.create_socket_with_timeout(host, port, options)
140
141
  if supports_connect_timeout?
@@ -191,53 +192,25 @@ module Dalli
191
192
 
192
193
  # Detect and cache the correct pack format for struct timeval on this platform.
193
194
  # Different architectures have different sizes for time_t and suseconds_t.
194
- # rubocop:disable-next ThreadSafety/ClassInstanceVariable
195
+ # rubocop:disable ThreadSafety/ClassInstanceVariable
195
196
  def self.timeval_pack_format(sock)
196
197
  @timeval_pack_format ||= begin
197
198
  expected_size = sock.getsockopt(::Socket::SOL_SOCKET, ::Socket::SO_RCVTIMEO).data.bytesize
198
199
  TIMEVAL_PACK_FORMATS.find { |fmt| TIMEVAL_TEST_VALUES.pack(fmt).bytesize == expected_size } || 'll'
199
200
  end
200
201
  end
202
+ # rubocop:enable ThreadSafety/ClassInstanceVariable
201
203
 
202
204
  def self.pack_timeval(sock, seconds, microseconds)
203
205
  [seconds, microseconds].pack(timeval_pack_format(sock))
204
206
  end
205
207
 
206
- # Closes the TCP socket if the handshake fails or times out, so a failed
207
- # attempt doesn't hold a file descriptor until GC.
208
208
  def self.wrapping_ssl_socket(tcp_socket, host, ssl_context)
209
- warn_if_unverified(ssl_context)
210
- connected = false
211
209
  ssl_socket = Dalli::Socket::SSLSocket.new(tcp_socket, ssl_context)
212
210
  ssl_socket.hostname = host
213
211
  ssl_socket.sync_close = true
214
212
  ssl_socket.connect
215
- connected = true
216
213
  ssl_socket
217
- ensure
218
- tcp_socket.close unless connected
219
- end
220
-
221
- # Class variable to track whether the TLS verification warning has been logged
222
- @@ssl_verification_warning_logged = false # rubocop:disable Style/ClassVars
223
-
224
- # Verification comes entirely from the caller's ssl_context, and a bare
225
- # SSLContext.new verifies nothing, so warn (once per process) when the
226
- # certificate or the hostname won't be checked.
227
- def self.warn_if_unverified(ssl_context)
228
- return if @@ssl_verification_warning_logged
229
-
230
- problem = if !ssl_context.verify_mode.to_i.anybits?(::OpenSSL::SSL::VERIFY_PEER)
231
- 'verify_mode is VERIFY_NONE, so the server certificate is not checked'
232
- elsif !ssl_context.verify_hostname
233
- 'verify_hostname is false, so the certificate is not checked against the hostname'
234
- end
235
- return unless problem
236
-
237
- Dalli.logger.warn "SECURITY WARNING: Dalli's ssl_context #{problem}. " \
238
- 'Use ssl_context.set_params(verify_mode: OpenSSL::SSL::VERIFY_PEER, ' \
239
- 'verify_hostname: true) to verify the server.'
240
- @@ssl_verification_warning_logged = true # rubocop:disable Style/ClassVars
241
214
  end
242
215
  end
243
216
 
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.0.14'
4
+ VERSION = '5.1.0'
5
5
 
6
- MIN_SUPPORTED_MEMCACHED_VERSION = '1.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,7 +62,6 @@ 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'
71
66
  require_relative 'dalli/client'
72
67
  require_relative 'dalli/key_manager'
@@ -16,10 +16,11 @@ module Rack
16
16
  attr_reader :data
17
17
 
18
18
  # Don't freeze this until we fix the specs/implementation
19
- # rubocop:disable-next Style/MutableConstant
19
+ # rubocop:disable Style/MutableConstant
20
20
  DEFAULT_DALLI_OPTIONS = {
21
21
  namespace: 'rack:session'
22
22
  }
23
+ # rubocop:enable Style/MutableConstant
23
24
 
24
25
  # Brings in a new Rack::Session::Dalli middleware with the given
25
26
  # `:memcache_server`. The server is either a hostname, or a
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.0.14
4
+ version: 5.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter M. Goldstein
@@ -42,7 +42,6 @@ files:
42
42
  - lib/dalli/compressor.rb
43
43
  - lib/dalli/flags.rb
44
44
  - lib/dalli/instrumentation.rb
45
- - lib/dalli/json_serializer.rb
46
45
  - lib/dalli/key_manager.rb
47
46
  - lib/dalli/options.rb
48
47
  - lib/dalli/pipelined_deleter.rb
@@ -88,7 +87,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
88
87
  - !ruby/object:Gem::Version
89
88
  version: '0'
90
89
  requirements: []
91
- rubygems_version: 4.0.23
90
+ rubygems_version: 4.0.18
92
91
  specification_version: 4
93
92
  summary: High performance memcached client for Ruby
94
93
  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