dalli 5.1.8 → 5.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/dalli/socket.rb CHANGED
@@ -12,22 +12,29 @@ module Dalli
12
12
  # Common methods for all socket implementations.
13
13
  ##
14
14
  module InstanceMethods
15
+ READ_CHUNK_SIZE = 8196
16
+
17
+ # Reads whatever is available without blocking. A read that returns less
18
+ # than a full chunk means the socket was drained, so we stop there rather
19
+ # than spend another read_nonblock call just to get :wait_readable back.
20
+ # Callers keep waiting on IO.select for anything that arrives later.
15
21
  def read_available(reusable_buffer = nil)
16
22
  if reusable_buffer
17
- value = read_nonblock(8196, reusable_buffer, exception: false)
23
+ value = read_nonblock(READ_CHUNK_SIZE, reusable_buffer, exception: false)
18
24
  case value
19
25
  when :wait_writable, :wait_readable
20
26
  return reusable_buffer.clear
21
27
  when nil
22
28
  raise Errno::ECONNRESET, "Connection reset: #{logged_options.inspect}"
23
29
  end
30
+ return value if drained_after?(value)
24
31
  else
25
32
  value = ''.b
26
33
  end
27
34
 
28
35
  buffer = ''.b
29
36
  loop do
30
- result = read_nonblock(8196, buffer, exception: false)
37
+ result = read_nonblock(READ_CHUNK_SIZE, buffer, exception: false)
31
38
  case result
32
39
  when :wait_writable, :wait_readable
33
40
  buffer.clear
@@ -36,10 +43,21 @@ module Dalli
36
43
  raise Errno::ECONNRESET, "Connection reset: #{logged_options.inspect}"
37
44
  else
38
45
  value << result
46
+ return value if drained_after?(result)
39
47
  end
40
48
  end
41
49
  end
42
50
 
51
+ # True when a read came back short and nothing is left buffered in-process.
52
+ def drained_after?(chunk)
53
+ chunk.bytesize < READ_CHUNK_SIZE && !buffered_data?
54
+ end
55
+
56
+ # Plain sockets keep nothing buffered in-process. See SSLSocket.
57
+ def buffered_data?
58
+ false
59
+ end
60
+
43
61
  FILTERED_OUT_OPTIONS = %i[username password].freeze
44
62
  def logged_options
45
63
  options.except(*FILTERED_OUT_OPTIONS)
@@ -83,6 +101,13 @@ module Dalli
83
101
  io.options
84
102
  end
85
103
 
104
+ # OpenSSL can hold decrypted bytes that IO.select won't report as readable.
105
+ # With OpenSSL's default (no read-ahead) a short read has already used up
106
+ # the current record, so this is normally 0; it guards read-ahead setups.
107
+ def buffered_data?
108
+ pending.positive?
109
+ end
110
+
86
111
  unless method_defined?(:wait_readable)
87
112
  def wait_readable(timeout = nil)
88
113
  to_io.wait_readable(timeout)
@@ -203,41 +228,12 @@ module Dalli
203
228
  [seconds, microseconds].pack(timeval_pack_format(sock))
204
229
  end
205
230
 
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
231
  def self.wrapping_ssl_socket(tcp_socket, host, ssl_context)
209
- warn_if_unverified(ssl_context)
210
- connected = false
211
232
  ssl_socket = Dalli::Socket::SSLSocket.new(tcp_socket, ssl_context)
212
233
  ssl_socket.hostname = host
213
234
  ssl_socket.sync_close = true
214
235
  ssl_socket.connect
215
- connected = true
216
236
  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
237
  end
242
238
  end
243
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.8'
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,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dalli
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.1.8
4
+ version: 5.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter M. Goldstein
@@ -37,12 +37,12 @@ files:
37
37
  - LICENSE
38
38
  - README.md
39
39
  - lib/dalli.rb
40
+ - lib/dalli/cache_result.rb
40
41
  - lib/dalli/cas/client.rb
41
42
  - lib/dalli/client.rb
42
43
  - lib/dalli/compressor.rb
43
44
  - lib/dalli/flags.rb
44
45
  - lib/dalli/instrumentation.rb
45
- - lib/dalli/json_serializer.rb
46
46
  - lib/dalli/key_manager.rb
47
47
  - lib/dalli/options.rb
48
48
  - lib/dalli/pipelined_deleter.rb
@@ -88,7 +88,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
88
88
  - !ruby/object:Gem::Version
89
89
  version: '0'
90
90
  requirements: []
91
- rubygems_version: 4.0.23
91
+ rubygems_version: 4.0.21
92
92
  specification_version: 4
93
93
  summary: High performance memcached client for Ruby
94
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