dalli 3.2.17 → 4.0.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
@@ -2,7 +2,6 @@
2
2
 
3
3
  require 'openssl'
4
4
  require 'rbconfig'
5
- require 'timeout'
6
5
 
7
6
  module Dalli
8
7
  ##
@@ -13,8 +12,6 @@ module Dalli
13
12
  # Common methods for all socket implementations.
14
13
  ##
15
14
  module InstanceMethods
16
- MAX_AVAILABLE_BYTES = 8196 * 8
17
-
18
15
  def readfull(count)
19
16
  value = String.new(capacity: count + 1)
20
17
  loop do
@@ -27,21 +24,16 @@ module Dalli
27
24
 
28
25
  def read_available
29
26
  value = +''
30
- while value.bytesize < MAX_AVAILABLE_BYTES
31
- result = read_nonblock([8196, MAX_AVAILABLE_BYTES - value.bytesize].min, exception: false)
27
+ loop do
28
+ result = read_nonblock(8196, exception: false)
32
29
  break if WAIT_RCS.include?(result)
33
30
  raise Errno::ECONNRESET, "Connection reset: #{logged_options.inspect}" unless result
34
31
 
35
32
  value << result
36
- break if result.bytesize < 8196 && !buffered_data?
37
33
  end
38
34
  value
39
35
  end
40
36
 
41
- def buffered_data?
42
- false
43
- end
44
-
45
37
  WAIT_RCS = %i[wait_writable wait_readable].freeze
46
38
 
47
39
  def append_to_buffer?(result)
@@ -60,7 +52,7 @@ module Dalli
60
52
 
61
53
  FILTERED_OUT_OPTIONS = %i[username password].freeze
62
54
  def logged_options
63
- options.reject { |k, _| FILTERED_OUT_OPTIONS.include? k }
55
+ options.except(*FILTERED_OUT_OPTIONS)
64
56
  end
65
57
  end
66
58
 
@@ -71,13 +63,6 @@ module Dalli
71
63
  ##
72
64
  class SSLSocket < ::OpenSSL::SSL::SSLSocket
73
65
  include Dalli::Socket::InstanceMethods
74
- def pending_data?
75
- pending.to_i.positive? || (@rbuffer && !@rbuffer.empty?)
76
- end
77
-
78
- def buffered_data?
79
- RUBY_ENGINE == 'jruby' || pending_data?
80
- end
81
66
 
82
67
  def options
83
68
  io.options
@@ -101,6 +86,7 @@ module Dalli
101
86
  ##
102
87
  class TCP < TCPSocket
103
88
  include Dalli::Socket::InstanceMethods
89
+
104
90
  # options - supports enhanced logging in the case of a timeout
105
91
  attr_accessor :options
106
92
 
@@ -109,11 +95,7 @@ module Dalli
109
95
  sock.options = { host: host, port: port }.merge(options)
110
96
  init_socket_options(sock, options)
111
97
 
112
- if options[:ssl_context]
113
- wrapping_ssl_socket(sock, host, options[:ssl_context], options[:socket_timeout])
114
- else
115
- sock
116
- end
98
+ options[:ssl_context] ? wrapping_ssl_socket(sock, host, options[:ssl_context]) : sock
117
99
  end
118
100
  end
119
101
 
@@ -137,57 +119,41 @@ module Dalli
137
119
  end
138
120
 
139
121
  def self.init_socket_options(sock, options)
122
+ configure_tcp_options(sock, options)
123
+ configure_socket_buffers(sock, options)
124
+ configure_timeout(sock, options)
125
+ end
126
+
127
+ def self.configure_tcp_options(sock, options)
140
128
  sock.setsockopt(::Socket::IPPROTO_TCP, ::Socket::TCP_NODELAY, true)
141
129
  sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_KEEPALIVE, true) if options[:keepalive]
130
+ end
131
+
132
+ def self.configure_socket_buffers(sock, options)
142
133
  sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_RCVBUF, options[:rcvbuf]) if options[:rcvbuf]
143
134
  sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_SNDBUF, options[:sndbuf]) if options[:sndbuf]
135
+ end
144
136
 
137
+ def self.configure_timeout(sock, options)
145
138
  return unless options[:socket_timeout]
146
139
 
147
- seconds, fractional = options[:socket_timeout].divmod(1)
148
- microseconds = fractional * 1_000_000
149
- timeval = [seconds, microseconds].pack('l_2')
140
+ if sock.respond_to?(:timeout=)
141
+ sock.timeout = options[:socket_timeout]
142
+ else
143
+ seconds, fractional = options[:socket_timeout].divmod(1)
144
+ timeval = [seconds, fractional * 1_000_000].pack('l_2')
150
145
 
151
- sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_RCVTIMEO, timeval)
152
- sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_SNDTIMEO, timeval)
146
+ sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_RCVTIMEO, timeval)
147
+ sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_SNDTIMEO, timeval)
148
+ end
153
149
  end
154
150
 
155
- # Closes the TCP socket if the handshake fails or times out, so a failed
156
- # attempt doesn't hold a file descriptor until GC.
157
- def self.wrapping_ssl_socket(tcp_socket, host, ssl_context, socket_timeout = nil)
158
- warn_if_unverified(ssl_context)
159
- connected = false
151
+ def self.wrapping_ssl_socket(tcp_socket, host, ssl_context)
160
152
  ssl_socket = Dalli::Socket::SSLSocket.new(tcp_socket, ssl_context)
161
153
  ssl_socket.hostname = host
162
154
  ssl_socket.sync_close = true
163
- Timeout.timeout(socket_timeout) { ssl_socket.connect }
164
- connected = true
155
+ ssl_socket.connect
165
156
  ssl_socket
166
- ensure
167
- tcp_socket.close unless connected
168
- end
169
-
170
- # Verification comes entirely from the caller's ssl_context, and a bare
171
- # SSLContext.new verifies nothing, so warn (once per process) when the
172
- # certificate or the hostname won't be checked.
173
- def self.warn_if_unverified(ssl_context)
174
- return if @ssl_verification_warning_logged
175
-
176
- problem = unverified_problem(ssl_context)
177
- return unless problem
178
-
179
- Dalli.logger.warn "SECURITY WARNING: Dalli's ssl_context #{problem}. " \
180
- 'Use ssl_context.set_params(verify_mode: OpenSSL::SSL::VERIFY_PEER, ' \
181
- 'verify_hostname: true) to verify the server.'
182
- @ssl_verification_warning_logged = true
183
- end
184
-
185
- def self.unverified_problem(ssl_context)
186
- if !ssl_context.verify_mode.to_i.anybits?(::OpenSSL::SSL::VERIFY_PEER)
187
- 'verify_mode is VERIFY_NONE, so the server certificate is not checked'
188
- elsif !ssl_context.verify_hostname
189
- 'verify_hostname is false, so the certificate is not checked against the hostname'
190
- end
191
157
  end
192
158
  end
193
159
 
@@ -218,9 +184,16 @@ module Dalli
218
184
  Timeout.timeout(options[:socket_timeout]) do
219
185
  sock = new(path)
220
186
  sock.options = { path: path }.merge(options)
187
+ init_socket_options(sock, options)
221
188
  sock
222
189
  end
223
190
  end
191
+
192
+ def self.init_socket_options(sock, options)
193
+ # https://man7.org/linux/man-pages/man7/unix.7.html
194
+ sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_SNDBUF, options[:sndbuf]) if options[:sndbuf]
195
+ sock.timeout = options[:socket_timeout] if options[:socket_timeout] && sock.respond_to?(:timeout=)
196
+ end
224
197
  end
225
198
  end
226
199
  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 = '3.2.17'
4
+ VERSION = '4.0.0'
5
5
 
6
6
  MIN_SUPPORTED_MEMCACHED_VERSION = '1.4'
7
7
  end
data/lib/dalli.rb CHANGED
@@ -4,8 +4,6 @@
4
4
  # Namespace for all Dalli code.
5
5
  ##
6
6
  module Dalli
7
- autoload :Server, 'dalli/server'
8
-
9
7
  # generic error
10
8
  class DalliError < RuntimeError; end
11
9
 
@@ -27,6 +25,9 @@ module Dalli
27
25
  # operation is not permitted in a multi block
28
26
  class NotPermittedMultiOpError < DalliError; end
29
27
 
28
+ # raised when Memcached response with a SERVER_ERROR
29
+ class ServerError < DalliError; end
30
+
30
31
  # Implements the NullObject pattern to store an application-defined value for 'Key not found' responses.
31
32
  class NilObject; end # rubocop:disable Lint/EmptyClass
32
33
  NOT_FOUND = NilObject.new
@@ -57,7 +58,6 @@ end
57
58
  require_relative 'dalli/version'
58
59
 
59
60
  require_relative 'dalli/compressor'
60
- require_relative 'dalli/json_serializer'
61
61
  require_relative 'dalli/client'
62
62
  require_relative 'dalli/key_manager'
63
63
  require_relative 'dalli/pipelined_getter'
@@ -9,6 +9,10 @@ module Rack
9
9
  module Session
10
10
  # Rack::Session::Dalli provides memcached based session management.
11
11
  class Dalli < Abstract::PersistedSecure
12
+ class MissingSessionError < StandardError; end
13
+
14
+ RACK_SESSION_PERSISTED = 'rack.session.persisted'
15
+
12
16
  attr_reader :data
13
17
 
14
18
  # Don't freeze this until we fix the specs/implementation
@@ -70,23 +74,37 @@ module Rack
70
74
  @data = build_data_source(options)
71
75
  end
72
76
 
73
- def find_session(_req, sid)
77
+ def call(*_args)
78
+ super
79
+ rescue MissingSessionError
80
+ [401, {}, ['Wrong session ID']]
81
+ end
82
+
83
+ def find_session(req, sid)
74
84
  with_dalli_client([nil, {}]) do |dc|
75
85
  existing_session = existing_session_for_sid(dc, sid)
76
- return [sid, existing_session] unless existing_session.nil?
86
+ if existing_session.nil?
87
+ sid = create_sid_with_empty_session(dc)
88
+ existing_session = {}
89
+ end
77
90
 
78
- [create_sid_with_empty_session(dc), {}]
91
+ update_session_persisted_data(req, { id: sid })
92
+ return [sid, existing_session]
79
93
  end
80
94
  end
81
95
 
82
- def write_session(_req, sid, session, options)
96
+ def write_session(req, sid, session, options)
83
97
  return false unless sid
84
98
 
85
99
  key = memcached_key_from_sid(sid)
86
100
  return false unless key
87
101
 
88
102
  with_dalli_client(false) do |dc|
89
- dc.set(memcached_key_from_sid(sid), session, ttl(options[:expire_after]))
103
+ write_session_safely!(
104
+ dc, sid, session_persisted_data(req),
105
+ write_args: [memcached_key_from_sid(sid), session, ttl(options[:expire_after])]
106
+ )
107
+
90
108
  sid
91
109
  end
92
110
  end
@@ -145,6 +163,15 @@ module Rack
145
163
  end
146
164
  end
147
165
 
166
+ def write_session_safely!(dalli_client, sid, persisted_data, write_args:)
167
+ if persisted_data && persisted_data[:id] == sid # That means that we update the existing session
168
+ # Override the session only if it still exists in the store!
169
+ raise MissingSessionError unless dalli_client.replace(*write_args)
170
+ else
171
+ dalli_client.set(*write_args)
172
+ end
173
+ end
174
+
148
175
  def extract_dalli_options(options)
149
176
  raise 'Rack::Session::Dalli no longer supports the :cache option.' if options[:cache]
150
177
 
@@ -175,8 +202,8 @@ module Rack
175
202
  raise e
176
203
  end
177
204
 
178
- def with_dalli_client(result_on_error = nil, &block)
179
- @data.with(&block)
205
+ def with_dalli_client(result_on_error = nil, &)
206
+ @data.with(&)
180
207
  rescue ::Dalli::DalliError, Errno::ECONNREFUSED
181
208
  raise if $ERROR_INFO.message.include?('undefined class')
182
209
 
@@ -190,6 +217,14 @@ module Rack
190
217
  def ttl(expire_after)
191
218
  expire_after.nil? ? 0 : expire_after + 1
192
219
  end
220
+
221
+ def session_persisted_data(req)
222
+ req.get_header RACK_SESSION_PERSISTED
223
+ end
224
+
225
+ def update_session_persisted_data(req, data)
226
+ req.set_header RACK_SESSION_PERSISTED, data
227
+ end
193
228
  end
194
229
  end
195
230
  end
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: 3.2.17
4
+ version: 4.0.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
@@ -41,7 +40,6 @@ files:
41
40
  - lib/dalli/cas/client.rb
42
41
  - lib/dalli/client.rb
43
42
  - lib/dalli/compressor.rb
44
- - lib/dalli/json_serializer.rb
45
43
  - lib/dalli/key_manager.rb
46
44
  - lib/dalli/options.rb
47
45
  - lib/dalli/pid_cache.rb
@@ -65,7 +63,6 @@ files:
65
63
  - lib/dalli/protocol/value_marshaller.rb
66
64
  - lib/dalli/protocol/value_serializer.rb
67
65
  - lib/dalli/ring.rb
68
- - lib/dalli/server.rb
69
66
  - lib/dalli/servers_arg_normalizer.rb
70
67
  - lib/dalli/socket.rb
71
68
  - lib/dalli/version.rb
@@ -77,7 +74,6 @@ metadata:
77
74
  bug_tracker_uri: https://github.com/petergoldstein/dalli/issues
78
75
  changelog_uri: https://github.com/petergoldstein/dalli/blob/main/CHANGELOG.md
79
76
  rubygems_mfa_required: 'true'
80
- post_install_message:
81
77
  rdoc_options: []
82
78
  require_paths:
83
79
  - lib
@@ -85,15 +81,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
85
81
  requirements:
86
82
  - - ">="
87
83
  - !ruby/object:Gem::Version
88
- version: '2.6'
84
+ version: '3.1'
89
85
  required_rubygems_version: !ruby/object:Gem::Requirement
90
86
  requirements:
91
87
  - - ">="
92
88
  - !ruby/object:Gem::Version
93
89
  version: '0'
94
90
  requirements: []
95
- rubygems_version: 3.5.22
96
- signing_key:
91
+ rubygems_version: 4.0.3
97
92
  specification_version: 4
98
93
  summary: High performance memcached client for Ruby
99
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
data/lib/dalli/server.rb DELETED
@@ -1,6 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Dalli # rubocop:disable Style/Documentation
4
- warn 'Dalli::Server is deprecated, use Dalli::Protocol::Binary instead'
5
- Server = Protocol::Binary
6
- end