dalli 3.2.8 → 4.3.3

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
@@ -52,7 +52,7 @@ module Dalli
52
52
 
53
53
  FILTERED_OUT_OPTIONS = %i[username password].freeze
54
54
  def logged_options
55
- options.reject { |k, _| FILTERED_OUT_OPTIONS.include? k }
55
+ options.except(*FILTERED_OUT_OPTIONS)
56
56
  end
57
57
  end
58
58
 
@@ -63,6 +63,7 @@ module Dalli
63
63
  ##
64
64
  class SSLSocket < ::OpenSSL::SSL::SSLSocket
65
65
  include Dalli::Socket::InstanceMethods
66
+
66
67
  def options
67
68
  io.options
68
69
  end
@@ -85,9 +86,16 @@ module Dalli
85
86
  ##
86
87
  class TCP < TCPSocket
87
88
  include Dalli::Socket::InstanceMethods
89
+
88
90
  # options - supports enhanced logging in the case of a timeout
89
91
  attr_accessor :options
90
92
 
93
+ # Expected parameter signature for unmodified TCPSocket#initialize.
94
+ # Used to detect when gems like socksify or resolv-replace have monkey-patched
95
+ # TCPSocket, which breaks the connect_timeout: keyword argument.
96
+ TCPSOCKET_NATIVE_PARAMETERS = [[:rest]].freeze
97
+ private_constant :TCPSOCKET_NATIVE_PARAMETERS
98
+
91
99
  def self.open(host, port, options = {})
92
100
  create_socket_with_timeout(host, port, options) do |sock|
93
101
  sock.options = { host: host, port: port }.merge(options)
@@ -97,15 +105,18 @@ module Dalli
97
105
  end
98
106
  end
99
107
 
108
+ # Detect and cache whether TCPSocket supports the connect_timeout: keyword argument.
109
+ # Returns false if TCPSocket#initialize has been monkey-patched by gems like
110
+ # socksify or resolv-replace, which don't support keyword arguments.
111
+ def self.supports_connect_timeout?
112
+ return @supports_connect_timeout if defined?(@supports_connect_timeout)
113
+
114
+ @supports_connect_timeout = RUBY_VERSION >= '3.0' &&
115
+ ::TCPSocket.instance_method(:initialize).parameters == TCPSOCKET_NATIVE_PARAMETERS
116
+ end
117
+
100
118
  def self.create_socket_with_timeout(host, port, options)
101
- # Check that TCPSocket#initialize was not overwritten by resolv-replace gem
102
- # (part of ruby standard library since 3.0.0, should be removed in 3.4.0),
103
- # as it does not handle keyword arguments correctly.
104
- # To check this we are using the fact that resolv-replace
105
- # aliases TCPSocket#initialize method to #original_resolv_initialize.
106
- # https://github.com/ruby/resolv-replace/blob/v0.1.1/lib/resolv-replace.rb#L21
107
- if RUBY_VERSION >= '3.0' &&
108
- !::TCPSocket.private_instance_methods.include?(:original_resolv_initialize)
119
+ if supports_connect_timeout?
109
120
  sock = new(host, port, connect_timeout: options[:socket_timeout])
110
121
  yield(sock)
111
122
  else
@@ -117,19 +128,57 @@ module Dalli
117
128
  end
118
129
 
119
130
  def self.init_socket_options(sock, options)
131
+ configure_tcp_options(sock, options)
132
+ configure_socket_buffers(sock, options)
133
+ configure_timeout(sock, options)
134
+ end
135
+
136
+ def self.configure_tcp_options(sock, options)
120
137
  sock.setsockopt(::Socket::IPPROTO_TCP, ::Socket::TCP_NODELAY, true)
121
138
  sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_KEEPALIVE, true) if options[:keepalive]
139
+ end
140
+
141
+ def self.configure_socket_buffers(sock, options)
122
142
  sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_RCVBUF, options[:rcvbuf]) if options[:rcvbuf]
123
143
  sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_SNDBUF, options[:sndbuf]) if options[:sndbuf]
144
+ end
124
145
 
146
+ def self.configure_timeout(sock, options)
125
147
  return unless options[:socket_timeout]
126
148
 
127
- seconds, fractional = options[:socket_timeout].divmod(1)
128
- microseconds = fractional * 1_000_000
129
- timeval = [seconds, microseconds].pack('l_2')
149
+ if sock.respond_to?(:timeout=)
150
+ # Ruby 3.2+ has IO#timeout for reliable cross-platform timeout handling
151
+ sock.timeout = options[:socket_timeout]
152
+ else
153
+ # Ruby 3.1 fallback using socket options
154
+ # struct timeval has architecture-dependent sizes (time_t, suseconds_t)
155
+ seconds, fractional = options[:socket_timeout].divmod(1)
156
+ microseconds = (fractional * 1_000_000).to_i
157
+ timeval = pack_timeval(sock, seconds, microseconds)
158
+
159
+ sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_RCVTIMEO, timeval)
160
+ sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_SNDTIMEO, timeval)
161
+ end
162
+ end
130
163
 
131
- sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_RCVTIMEO, timeval)
132
- sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_SNDTIMEO, timeval)
164
+ # Pack formats for struct timeval across architectures.
165
+ # Uses fixed-size formats for JRuby compatibility (JRuby doesn't support _ modifier on q).
166
+ # - ll: 8 bytes (32-bit time_t, 32-bit suseconds_t)
167
+ # - qq: 16 bytes (64-bit time_t, 64-bit suseconds_t or padded 32-bit)
168
+ TIMEVAL_PACK_FORMATS = %w[ll qq].freeze
169
+ TIMEVAL_TEST_VALUES = [0, 0].freeze
170
+
171
+ # Detect and cache the correct pack format for struct timeval on this platform.
172
+ # Different architectures have different sizes for time_t and suseconds_t.
173
+ def self.timeval_pack_format(sock)
174
+ @timeval_pack_format ||= begin
175
+ expected_size = sock.getsockopt(::Socket::SOL_SOCKET, ::Socket::SO_RCVTIMEO).data.bytesize
176
+ TIMEVAL_PACK_FORMATS.find { |fmt| TIMEVAL_TEST_VALUES.pack(fmt).bytesize == expected_size } || 'll'
177
+ end
178
+ end
179
+
180
+ def self.pack_timeval(sock, seconds, microseconds)
181
+ [seconds, microseconds].pack(timeval_pack_format(sock))
133
182
  end
134
183
 
135
184
  def self.wrapping_ssl_socket(tcp_socket, host, ssl_context)
@@ -168,9 +217,16 @@ module Dalli
168
217
  Timeout.timeout(options[:socket_timeout]) do
169
218
  sock = new(path)
170
219
  sock.options = { path: path }.merge(options)
220
+ init_socket_options(sock, options)
171
221
  sock
172
222
  end
173
223
  end
224
+
225
+ def self.init_socket_options(sock, options)
226
+ # https://man7.org/linux/man-pages/man7/unix.7.html
227
+ sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_SNDBUF, options[:sndbuf]) if options[:sndbuf]
228
+ sock.timeout = options[:socket_timeout] if options[:socket_timeout] && sock.respond_to?(:timeout=)
229
+ end
174
230
  end
175
231
  end
176
232
  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.8'
4
+ VERSION = '4.3.3'
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,12 @@ 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
+
31
+ # socket/server communication error that can be retried
32
+ class RetryableNetworkError < NetworkError; end
33
+
30
34
  # Implements the NullObject pattern to store an application-defined value for 'Key not found' responses.
31
35
  class NilObject; end # rubocop:disable Lint/EmptyClass
32
36
  NOT_FOUND = NilObject.new
@@ -55,11 +59,15 @@ module Dalli
55
59
  end
56
60
 
57
61
  require_relative 'dalli/version'
62
+ require_relative 'dalli/instrumentation'
58
63
 
59
64
  require_relative 'dalli/compressor'
65
+ require_relative 'dalli/protocol_deprecations'
60
66
  require_relative 'dalli/client'
61
67
  require_relative 'dalli/key_manager'
62
68
  require_relative 'dalli/pipelined_getter'
69
+ require_relative 'dalli/pipelined_setter'
70
+ require_relative 'dalli/pipelined_deleter'
63
71
  require_relative 'dalli/ring'
64
72
  require_relative 'dalli/protocol'
65
73
  require_relative 'dalli/protocol/base'
@@ -71,6 +79,7 @@ require_relative 'dalli/protocol/server_config_parser'
71
79
  require_relative 'dalli/protocol/ttl_sanitizer'
72
80
  require_relative 'dalli/protocol/value_compressor'
73
81
  require_relative 'dalli/protocol/value_marshaller'
82
+ require_relative 'dalli/protocol/string_marshaller'
74
83
  require_relative 'dalli/protocol/value_serializer'
75
84
  require_relative 'dalli/servers_arg_normalizer'
76
85
  require_relative 'dalli/socket'
@@ -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
@@ -139,12 +157,21 @@ module Rack
139
157
  ::Dalli::Client.new(server_configurations, client_options)
140
158
  else
141
159
  ensure_connection_pool_added!
142
- ConnectionPool.new(pool_options) do
160
+ ConnectionPool.new(**pool_options) do
143
161
  ::Dalli::Client.new(server_configurations, client_options.merge(threadsafe: false))
144
162
  end
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,16 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dalli
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.2.8
4
+ version: 4.3.3
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: 2024-02-12 00:00:00.000000000 Z
13
- dependencies: []
11
+ date: 1980-01-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: logger
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
14
27
  description: High performance memcached client for Ruby
15
28
  email:
16
29
  - peter.m.goldstein@gmail.com
@@ -27,10 +40,13 @@ files:
27
40
  - lib/dalli/cas/client.rb
28
41
  - lib/dalli/client.rb
29
42
  - lib/dalli/compressor.rb
43
+ - lib/dalli/instrumentation.rb
30
44
  - lib/dalli/key_manager.rb
31
45
  - lib/dalli/options.rb
32
46
  - lib/dalli/pid_cache.rb
47
+ - lib/dalli/pipelined_deleter.rb
33
48
  - lib/dalli/pipelined_getter.rb
49
+ - lib/dalli/pipelined_setter.rb
34
50
  - lib/dalli/protocol.rb
35
51
  - lib/dalli/protocol/base.rb
36
52
  - lib/dalli/protocol/binary.rb
@@ -45,12 +61,13 @@ files:
45
61
  - lib/dalli/protocol/meta/response_processor.rb
46
62
  - lib/dalli/protocol/response_buffer.rb
47
63
  - lib/dalli/protocol/server_config_parser.rb
64
+ - lib/dalli/protocol/string_marshaller.rb
48
65
  - lib/dalli/protocol/ttl_sanitizer.rb
49
66
  - lib/dalli/protocol/value_compressor.rb
50
67
  - lib/dalli/protocol/value_marshaller.rb
51
68
  - lib/dalli/protocol/value_serializer.rb
69
+ - lib/dalli/protocol_deprecations.rb
52
70
  - lib/dalli/ring.rb
53
- - lib/dalli/server.rb
54
71
  - lib/dalli/servers_arg_normalizer.rb
55
72
  - lib/dalli/socket.rb
56
73
  - lib/dalli/version.rb
@@ -60,9 +77,8 @@ licenses:
60
77
  - MIT
61
78
  metadata:
62
79
  bug_tracker_uri: https://github.com/petergoldstein/dalli/issues
63
- changelog_uri: https://github.com/petergoldstein/dalli/blob/main/CHANGELOG.md
80
+ changelog_uri: https://github.com/petergoldstein/dalli/blob/v4.3/CHANGELOG.md
64
81
  rubygems_mfa_required: 'true'
65
- post_install_message:
66
82
  rdoc_options: []
67
83
  require_paths:
68
84
  - lib
@@ -70,15 +86,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
70
86
  requirements:
71
87
  - - ">="
72
88
  - !ruby/object:Gem::Version
73
- version: '2.6'
89
+ version: '3.1'
74
90
  required_rubygems_version: !ruby/object:Gem::Requirement
75
91
  requirements:
76
92
  - - ">="
77
93
  - !ruby/object:Gem::Version
78
94
  version: '0'
79
95
  requirements: []
80
- rubygems_version: 3.5.6
81
- signing_key:
96
+ rubygems_version: 4.0.6
82
97
  specification_version: 4
83
98
  summary: High performance memcached client for Ruby
84
99
  test_files: []
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