dalli 4.3.12 → 5.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.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +23 -172
  3. data/Gemfile +1 -1
  4. data/README.md +4 -63
  5. data/lib/dalli/client.rb +39 -99
  6. data/lib/dalli/compressor.rb +5 -53
  7. data/lib/dalli/instrumentation.rb +1 -1
  8. data/lib/dalli/key_manager.rb +2 -28
  9. data/lib/dalli/options.rb +2 -2
  10. data/lib/dalli/pipelined_deleter.rb +3 -3
  11. data/lib/dalli/pipelined_getter.rb +31 -52
  12. data/lib/dalli/pipelined_setter.rb +11 -20
  13. data/lib/dalli/protocol/base.rb +32 -176
  14. data/lib/dalli/protocol/connection_manager.rb +20 -143
  15. data/lib/dalli/protocol/meta.rb +16 -55
  16. data/lib/dalli/protocol/{meta/request_formatter.rb → request_formatter.rb} +8 -21
  17. data/lib/dalli/protocol/response_buffer.rb +12 -27
  18. data/lib/dalli/protocol/{meta/response_processor.rb → response_processor.rb} +35 -100
  19. data/lib/dalli/protocol/server_config_parser.rb +1 -1
  20. data/lib/dalli/protocol/string_marshaller.rb +1 -3
  21. data/lib/dalli/protocol/value_compressor.rb +7 -36
  22. data/lib/dalli/protocol/value_marshaller.rb +0 -4
  23. data/lib/dalli/protocol/value_serializer.rb +1 -1
  24. data/lib/dalli/protocol.rb +9 -7
  25. data/lib/dalli/ring.rb +2 -2
  26. data/lib/dalli/servers_arg_normalizer.rb +1 -1
  27. data/lib/dalli/socket.rb +5 -54
  28. data/lib/dalli/version.rb +2 -2
  29. data/lib/dalli.rb +0 -3
  30. data/lib/rack/session/dalli.rb +2 -1
  31. metadata +8 -18
  32. data/lib/dalli/json_serializer.rb +0 -28
  33. data/lib/dalli/protocol/binary/request_formatter.rb +0 -116
  34. data/lib/dalli/protocol/binary/response_header.rb +0 -36
  35. data/lib/dalli/protocol/binary/response_processor.rb +0 -259
  36. data/lib/dalli/protocol/binary/sasl_authentication.rb +0 -60
  37. data/lib/dalli/protocol/binary.rb +0 -202
  38. data/lib/dalli/protocol_deprecations.rb +0 -45
  39. /data/lib/dalli/protocol/{meta/key_regularizer.rb → key_regularizer.rb} +0 -0
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'forwardable'
4
- require 'set'
5
4
  require 'socket'
6
5
  require 'timeout'
7
6
 
@@ -23,9 +22,9 @@ module Dalli
23
22
 
24
23
  def initialize(attribs, client_options = {})
25
24
  hostname, port, socket_type, @weight, user_creds = ServerConfigParser.parse(attribs)
25
+ warn_uri_credentials(user_creds)
26
26
  @options = client_options.merge(user_creds)
27
27
  @raw_mode = client_options[:raw]
28
- @verify_reply_keys = client_options[:verify_reply_keys] ? true : false
29
28
  @value_marshaller = @raw_mode ? StringMarshaller.new(@options) : ValueMarshaller.new(@options)
30
29
  @connection_manager = ConnectionManager.new(hostname, port, socket_type, @options)
31
30
  end
@@ -36,36 +35,20 @@ module Dalli
36
35
  @raw_mode
37
36
  end
38
37
 
39
- # True when values should come back as stored: the client is in raw mode,
40
- # or this request passed raw: true. A stored value is then never
41
- # deserialized or decompressed for a caller who asked for raw bytes.
42
- def raw_request?(options)
43
- raw_mode? || (options.is_a?(Hash) && options[:raw]) ? true : false
44
- end
45
-
46
38
  # Chokepoint method for error handling and ensuring liveness
47
39
  def request(opkey, *args)
48
40
  verify_state(opkey)
49
41
 
50
42
  begin
51
43
  @connection_manager.start_request!
52
- drain_pending_quiet_responses(opkey)
53
44
  response = send(opkey, *args)
54
- # Quiet requests can leave replies on the socket; remember that so a
55
- # later non-quiet request knows to read them first
56
- @connection_manager.mark_quiet_responses_pending! if quiet? && opkey != :noop
57
45
 
58
46
  # pipelined_get/pipelined_get_interleaved emit query but don't read the response(s)
59
47
  @connection_manager.finish_request! unless %i[pipelined_get pipelined_get_interleaved].include?(opkey)
60
48
 
61
49
  response
62
- rescue Dalli::MarshalError, Dalli::ValueOverMaxSize => e
63
- log_marshal_err(args.first, e) if e.is_a?(Dalli::MarshalError)
64
- # Raised only while the connection is clean: a value is marshalled
65
- # before any of its request is written. Leaving the request in
66
- # progress would close the connection on the next request, which
67
- # discards earlier quiet writes still waiting in the write buffer.
68
- @connection_manager.abort_request!
50
+ rescue Dalli::MarshalError => e
51
+ log_marshal_err(args.first, e)
69
52
  raise
70
53
  rescue Dalli::DalliError
71
54
  raise
@@ -100,7 +83,7 @@ module Dalli
100
83
  verify_pipelined_state(:getkq)
101
84
  write_noop
102
85
  # Use ensure_ready instead of reset to preserve any data already buffered
103
- # while this get_multi's interleaved requests were sent
86
+ # during interleaved pipelined get draining
104
87
  response_buffer.ensure_ready
105
88
  end
106
89
 
@@ -109,25 +92,12 @@ module Dalli
109
92
  # repeatedly whenever this server's socket is readable until
110
93
  # #pipeline_complete?.
111
94
  #
112
- # When a block is given, yields (key, value, cas) for each response,
113
- # avoiding intermediate Hash allocation. Returns nil.
114
- # Without a block, returns a Hash of { key => [value, cas] }.
115
- # rubocop:disable-next Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
116
- def pipeline_next_responses(&block)
117
- return abandon_pipeline if @pipeline_out_of_step
118
-
95
+ # Returns a Hash of kv pairs received.
96
+ def pipeline_next_responses
119
97
  reconnect_on_pipeline_complete!
120
- values = nil
98
+ values = {}
121
99
 
122
- # Only the read is socket I/O. The caller's block runs below, outside
123
- # this rescue, so an Errno or Timeout::Error it raises reaches the
124
- # caller instead of being mistaken for a network failure (which closed
125
- # the connection and retried the whole get_multi, yielding keys twice).
126
- begin
127
- response_buffer.read
128
- rescue SystemCallError, *TIMEOUT_ERRORS, *SSL_ERRORS, EOFError => e
129
- @connection_manager.error_on_request!(e)
130
- end
100
+ response_buffer.read
131
101
 
132
102
  status, cas, key, value = response_buffer.process_single_getk_response
133
103
  # status is not nil only if we have a full response to parse
@@ -137,57 +107,33 @@ module Dalli
137
107
  # to the noop at the end of the pipeline
138
108
  finish_pipeline && break if status && key.nil?
139
109
 
140
- check_pipeline_response_count!(false)
141
-
142
- # A key that wasn't requested means the replies are out of step
143
- # (see expect_reply_keys): keep what was read so far, drop the rest
144
- if unexpected_reply_key?(key)
145
- abandon_pipeline
146
- break
147
- end
148
-
149
110
  # If the status is ok and the key is not nil, then this is a
150
111
  # getkq response with a value that we want to set in the response hash
151
- unless key.nil?
152
- if block
153
- yield key, value, cas
154
- else
155
- values ||= {}
156
- values[key] = [value, cas]
157
- end
158
- end
159
-
160
- # A request to this server from inside the block closes the
161
- # connection, and with it the rest of this get_multi's replies
162
- raise_pipeline_closed unless response_buffer.in_progress?
112
+ values[key] = [value, cas] unless key.nil?
163
113
 
164
114
  # Get the next response from the buffer
165
115
  status, cas, key, value = response_buffer.process_single_getk_response
166
116
  end
167
117
 
168
- values || {}
118
+ values
119
+ rescue SystemCallError, *TIMEOUT_ERRORS, *SSL_ERRORS, EOFError => e
120
+ @connection_manager.error_on_request!(e)
169
121
  end
170
122
 
171
123
  # Abort current pipelined get. Generally used to signal an external
172
- # timeout during pipelined get, or that the get_multi stopped before
173
- # reading all its replies (timed_out: false). The underlying socket is
124
+ # timeout during pipelined get. The underlying socket is
174
125
  # disconnected, and the exception is swallowed.
175
126
  #
176
127
  # Returns nothing.
177
- def pipeline_abort(timed_out: true)
128
+ def pipeline_abort
178
129
  response_buffer.clear
179
- @pipeline_out_of_step = false
180
- @expected_reply_keys = nil
181
130
  @connection_manager.abort_request!
182
131
  return true unless connected?
183
132
 
184
- # Closes the connection, which ensures that our connection is in a
185
- # clean state for future requests. The get_multi ran out of time
186
- # overall, or stopped early, rather than hitting a socket error, so it
187
- # doesn't count toward socket_max_failures: two slow get_multis in a
188
- # row would otherwise mark a healthy server down.
189
- log_pipeline_abort(timed_out)
190
- @connection_manager.close
133
+ # Closes the connection, which ensures that our connection
134
+ # is in a clean state for future requests
135
+ @connection_manager.error_on_request!('External timeout')
136
+ rescue NetworkError
191
137
  true
192
138
  end
193
139
 
@@ -196,18 +142,6 @@ module Dalli
196
142
  !response_buffer.in_progress?
197
143
  end
198
144
 
199
- def username
200
- @options[:username] || ENV.fetch('MEMCACHE_USERNAME', nil)
201
- end
202
-
203
- def password
204
- @options[:password] || ENV.fetch('MEMCACHE_PASSWORD', nil)
205
- end
206
-
207
- def require_auth?
208
- !username.nil?
209
- end
210
-
211
145
  def quiet?
212
146
  Thread.current[::Dalli::QUIET]
213
147
  end
@@ -217,21 +151,19 @@ module Dalli
217
151
 
218
152
  private
219
153
 
220
- ALLOWED_QUIET_OPS = %i[add replace set delete incr decr append prepend flush noop].freeze
221
- private_constant :ALLOWED_QUIET_OPS
154
+ URI_CREDENTIAL_WARNING = 'Dalli 5.0 removed SASL authentication. ' \
155
+ 'Credentials in memcached:// URIs are ignored.'
156
+ private_constant :URI_CREDENTIAL_WARNING
222
157
 
223
- # Reads the replies still pending from quiet requests before any
224
- # non-quiet request, which reads a reply of its own. A quiet block reads
225
- # them at its end, but the connection is shared: another thread or fiber
226
- # (quiet? is fiber-local) can make a request while the block is still
227
- # open, and would otherwise read the block's replies as its own. The
228
- # noop also sends any quiet requests still buffered.
229
- def drain_pending_quiet_responses(opkey)
230
- return unless @connection_manager.quiet_responses_pending? && opkey != :noop && !quiet?
158
+ def warn_uri_credentials(user_creds)
159
+ return if user_creds[:username].nil? && user_creds[:password].nil?
231
160
 
232
- noop
161
+ Dalli.logger.warn(URI_CREDENTIAL_WARNING)
233
162
  end
234
163
 
164
+ ALLOWED_QUIET_OPS = %i[add replace set delete incr decr append prepend flush noop].freeze
165
+ private_constant :ALLOWED_QUIET_OPS
166
+
235
167
  def verify_allowed_quiet!(opkey)
236
168
  return if ALLOWED_QUIET_OPS.include?(opkey)
237
169
 
@@ -283,8 +215,7 @@ module Dalli
283
215
 
284
216
  def connect
285
217
  @connection_manager.establish_connection
286
- authenticate_connection if require_auth?
287
- @version = version # Connect socket if not authed
218
+ @version = version
288
219
  up!
289
220
  end
290
221
 
@@ -293,7 +224,6 @@ module Dalli
293
224
  # Use clear (not reset) to keep pipeline_complete? = true, which is
294
225
  # the expected state before pipeline_response_setup is called.
295
226
  response_buffer.clear
296
- expect_reply_keys(keys)
297
227
 
298
228
  req = +''
299
229
  keys.each do |key|
@@ -307,10 +237,8 @@ module Dalli
307
237
  # This prevents socket buffer deadlock when sending many keys.
308
238
  # Populates the provided results hash with any responses drained during send.
309
239
  def pipelined_get_interleaved(keys, chunk_size, results)
310
- # Start with an empty buffer for draining during the send phase. Bytes
311
- # left by an earlier get_multi that stopped early belong to its replies.
312
- response_buffer.reset
313
- expect_reply_keys(keys)
240
+ # Initialize the response buffer for draining during send phase
241
+ response_buffer.ensure_ready
314
242
 
315
243
  keys.each_slice(chunk_size) do |chunk|
316
244
  # Build and write this chunk of requests
@@ -329,7 +257,7 @@ module Dalli
329
257
  # Non-blocking read and processing of any available pipeline responses.
330
258
  # Used during interleaved pipelined gets to prevent buffer deadlock.
331
259
  # Populates the provided results hash directly to avoid allocation overhead.
332
- def drain_pipeline_responses(results) # rubocop:disable Metrics/CyclomaticComplexity
260
+ def drain_pipeline_responses(results)
333
261
  return unless connected?
334
262
 
335
263
  # Non-blocking check if socket has data available
@@ -343,14 +271,6 @@ module Dalli
343
271
  status, cas, key, value = response_buffer.process_single_getk_response
344
272
  break if status.nil? # No complete response available
345
273
 
346
- check_pipeline_response_count!(status && key.nil?)
347
-
348
- # Handled by the next pipeline_next_responses, once sending is done
349
- if unexpected_reply_key?(key)
350
- @pipeline_out_of_step = true
351
- break
352
- end
353
-
354
274
  results[key] = [value, cas] unless key.nil?
355
275
  end
356
276
  rescue SystemCallError, Dalli::NetworkError
@@ -358,59 +278,8 @@ module Dalli
358
278
  nil
359
279
  end
360
280
 
361
- # Registered with the connection manager, which clears it whenever the
362
- # connection closes, so a new connection never parses an old one's bytes
363
281
  def response_buffer
364
- @response_buffer ||= ResponseBuffer.new(@connection_manager, response_processor).tap do |buffer|
365
- @connection_manager.response_buffer = buffer
366
- end
367
- end
368
-
369
- # A multi-get only accepts replies for the keys it asked for. memcached
370
- # names an item created by auto-create (ma or mg with N) for a
371
- # base64-encoded key by its raw bytes, without the b flag; such a key
372
- # can contain spaces or CR/LF, which split the reply header and make the
373
- # replies after it parse as different ones. A key that wasn't requested
374
- # is how that shows up.
375
- # Only with the verify_reply_keys option, since building the set costs a
376
- # multi-get about 15%. Dalli itself no longer creates such items (see
377
- # Meta#decr_incr); the check guards against ones that older versions or
378
- # other clients created, on memcached versions with the behavior.
379
- def expect_reply_keys(keys)
380
- @pipeline_out_of_step = false
381
- @expected_response_count = keys.size
382
- @pipeline_response_count = 0
383
- @expected_reply_keys = (keys.to_set { |k| reply_key_form(k) } if @verify_reply_keys)
384
- end
385
-
386
- def check_pipeline_response_count!(terminating)
387
- return if terminating
388
-
389
- @pipeline_response_count += 1
390
- return if @pipeline_response_count <= @expected_response_count
391
-
392
- raise Dalli::DalliError, 'Too many get_multi replies'
393
- end
394
-
395
- def unexpected_reply_key?(key)
396
- !key.nil? && @expected_reply_keys && !@expected_reply_keys.include?(reply_key_form(key))
397
- end
398
-
399
- # Keys compared as bytes; ASCII keys compare equal in any encoding as is
400
- def reply_key_form(key)
401
- key.ascii_only? ? key : key.b
402
- end
403
-
404
- # Keeps what was read, drops the rest of this server's replies, and
405
- # closes the connection so nothing unread is left on it. Not a socket
406
- # failure, so it doesn't count toward socket_max_failures; the keys not
407
- # read are simply missing from the result.
408
- def abandon_pipeline
409
- Dalli.logger.warn { "#{name} replied to a get_multi with a key that wasn't requested; closing the connection" }
410
- @pipeline_out_of_step = false
411
- response_buffer.clear
412
- @connection_manager.close
413
- {}
282
+ @response_buffer ||= ResponseBuffer.new(@connection_manager, response_processor)
414
283
  end
415
284
 
416
285
  # Called after the noop response is received at the end of a set
@@ -426,19 +295,6 @@ module Dalli
426
295
  @connection_manager.reconnect! 'pipelined get has completed' if pipeline_complete?
427
296
  end
428
297
 
429
- def raise_pipeline_closed
430
- raise Dalli::DalliError, "The connection to #{name} closed while a get_multi was reading its replies " \
431
- '(a request made from inside the get_multi block?)'
432
- end
433
-
434
- def log_pipeline_abort(timed_out)
435
- if timed_out
436
- Dalli.logger.warn { "#{name} didn't finish a get_multi within socket_timeout; closing the connection" }
437
- else
438
- Dalli.logger.debug { "#{name} get_multi stopped before reading all replies; closing the connection" }
439
- end
440
- end
441
-
442
298
  def log_marshal_err(key, err)
443
299
  Dalli.logger.error "Marshalling error for key '#{key}': #{err.message}"
444
300
  Dalli.logger.error 'You are trying to cache a Ruby object which cannot be serialized to memcached.'
@@ -28,9 +28,6 @@ module Dalli
28
28
 
29
29
  attr_accessor :hostname, :port, :socket_type, :options
30
30
  attr_reader :sock
31
- # The buffer replies are read into (see Base#response_buffer). It's
32
- # cleared with the write buffer whenever the connection closes.
33
- attr_writer :response_buffer
34
31
 
35
32
  def initialize(hostname, port, socket_type, client_options)
36
33
  @hostname = hostname
@@ -38,14 +35,9 @@ module Dalli
38
35
  @socket_type = socket_type
39
36
  @options = DEFAULTS.merge(client_options)
40
37
  @request_in_progress = false
41
- @quiet_responses_pending = false
42
38
  @sock = nil
43
39
  @pid = nil
44
- @write_buffer = []
45
- @write_buffer_bytes = 0
46
- @response_buffer = nil
47
40
 
48
- @fail_count = 0
49
41
  reset_down_info
50
42
  end
51
43
 
@@ -61,13 +53,11 @@ module Dalli
61
53
  Dalli.logger.debug { "Dalli::Server#connect #{name}" }
62
54
 
63
55
  @sock = memcached_socket
64
- # Writes are buffered in @write_buffer instead; see WRITE_BUFFER_FLUSH_BYTES
65
- @sock.sync = true
66
- @pid = Process.pid
56
+ @sock.sync = false # Enable buffered I/O for better performance
57
+ @pid = PIDCache.pid
67
58
  @request_in_progress = false
68
- @quiet_responses_pending = false
69
- rescue SystemCallError, *TIMEOUT_ERRORS, *SSL_ERRORS, EOFError, SocketError => e
70
- # SocketError = DNS resolution failure; SSL_ERRORS = failed TLS handshake
59
+ rescue SystemCallError, *TIMEOUT_ERRORS, EOFError, SocketError => e
60
+ # SocketError = DNS resolution failure
71
61
  error_on_request!(e)
72
62
  end
73
63
 
@@ -95,8 +85,6 @@ module Dalli
95
85
  def down!
96
86
  close
97
87
  log_down_detected
98
- # Once down_retry_delay passes, the server gets a full set of attempts
99
- @fail_count = 0
100
88
 
101
89
  @error = $ERROR_INFO&.class&.name
102
90
  @msg ||= $ERROR_INFO&.message
@@ -126,44 +114,19 @@ module Dalli
126
114
  return unless @sock
127
115
 
128
116
  begin
129
- close_socket
117
+ @sock.close
130
118
  rescue StandardError
131
119
  nil
132
120
  end
133
121
  @sock = nil
134
122
  @pid = nil
135
- @quiet_responses_pending = false
136
- discard_write_buffer
137
- @response_buffer&.clear
138
123
  abort_request!
139
124
  end
140
125
 
141
- # A forked child shares the parent's connection, so it closes only its
142
- # own file descriptor. Closing the TLS socket itself would send
143
- # close_notify and end the parent's TLS session.
144
- def close_socket
145
- fork_detected? ? @sock.to_io.close : @sock.close
146
- end
147
-
148
126
  def connected?
149
127
  !@sock.nil?
150
128
  end
151
129
 
152
- # True when a quiet request was written on this connection and its
153
- # replies (memcached only suppresses some of them) haven't been
154
- # consumed by a noop yet.
155
- def quiet_responses_pending?
156
- @quiet_responses_pending
157
- end
158
-
159
- def mark_quiet_responses_pending!
160
- @quiet_responses_pending = true
161
- end
162
-
163
- def clear_quiet_responses_pending!
164
- @quiet_responses_pending = false
165
- end
166
-
167
130
  def request_in_progress?
168
131
  @request_in_progress
169
132
  end
@@ -178,88 +141,47 @@ module Dalli
178
141
  raise '[Dalli] No request in progress. This may be a bug in Dalli.' unless @request_in_progress
179
142
 
180
143
  @request_in_progress = false
181
- # A completed request is what proves the server healthy again, so the
182
- # failure count resets here rather than on reconnect: a server that
183
- # accepts connections but never answers would otherwise reset it on
184
- # every retry and never reach socket_max_failures.
185
- @fail_count = 0
186
144
  end
187
145
 
188
146
  def abort_request!
189
147
  @request_in_progress = false
190
148
  end
191
149
 
192
- # The longest reply line Dalli reads, in bytes, CRLF included. A meta
193
- # reply header is at most about 500 bytes: "VA <size>" plus its flags,
194
- # the largest being a 250-byte key base64-encoded to 336 bytes, a
195
- # 20-digit CAS, a 32-byte opaque and a few numeric fields. The longest
196
- # stats line (stats settings) carries a file path such as ext_path, up
197
- # to PATH_MAX (4096). 8 KiB leaves room for both, while a server that
198
- # never sends CRLF can't grow the read buffer without bound.
199
- MAX_LINE_BYTES = 8 * 1024
200
-
201
150
  def read_line
202
- flush_write_buffer
203
- data = with_tls_timeout { @sock.gets("\r\n", MAX_LINE_BYTES) }
151
+ data = @sock.gets("\r\n")
204
152
  error_on_request!('EOF in read_line') if data.nil?
205
- # Without its CRLF the line either ran past MAX_LINE_BYTES or was cut
206
- # short by EOF; either way the connection is out of step
207
- error_on_request!("Reply line without CRLF in the first #{MAX_LINE_BYTES} bytes") unless data.end_with?("\r\n")
208
153
  data
209
154
  rescue SystemCallError, *TIMEOUT_ERRORS, *SSL_ERRORS, EOFError => e
210
155
  error_on_request!(e)
211
156
  end
212
157
 
213
- # memcached can't store an item larger than 1 GiB (its -I maximum), so a
214
- # reply claiming more (or a negative size) is malformed or hostile.
215
- # Reading allocates the full count up front, so check before reading.
216
- MAX_READ_BYTES = (1024 * 1024 * 1024) + 2 # plus the value's trailing "\r\n"
217
-
218
- def check_read_size!(count)
219
- return if count.between?(0, MAX_READ_BYTES)
220
-
221
- raise Dalli::DalliError, "Reply size #{count} from #{name} is out of range"
222
- end
223
-
224
158
  def read(count)
225
- check_read_size!(count)
226
- flush_write_buffer
227
- with_tls_timeout { @sock.readfull(count) }
159
+ # JRuby doesn't support IO#timeout=, so use custom readfull implementation
160
+ # CRuby 3.3+ has IO#timeout= which makes IO#read work with timeouts
161
+ if RUBY_ENGINE == 'jruby'
162
+ @sock.readfull(count)
163
+ else
164
+ @sock.read(count)
165
+ end
228
166
  rescue SystemCallError, *TIMEOUT_ERRORS, *SSL_ERRORS, EOFError => e
229
167
  error_on_request!(e)
230
168
  end
231
169
 
232
- # Requests are buffered here rather than in the socket's own IO buffer
233
- # (the socket is sync). Ruby flushes an IO's write buffer when any process
234
- # holding it closes or finalizes the IO, so after a fork a child could
235
- # send bytes the parent had buffered but not sent yet: a quiet write would
236
- # run twice, or the parent's replies would shift onto the wrong requests.
237
- # A forked child discards this buffer instead. Like IO's own buffer, it
238
- # is sent once it grows past this size, or when a reply is read.
239
- WRITE_BUFFER_FLUSH_BYTES = 64 * 1024
240
-
241
170
  def write(bytes)
242
- # A copy: the caller may change its string before the buffer is sent
243
- # (the meta protocol writes a value string as its own part), as Ruby's
244
- # IO buffer copied on write
245
- @write_buffer << (bytes.frozen? ? bytes : bytes.dup)
246
- @write_buffer_bytes += bytes.bytesize
247
- flush_write_buffer if @write_buffer_bytes >= WRITE_BUFFER_FLUSH_BYTES
248
- bytes.bytesize
249
- rescue SystemCallError, *TIMEOUT_ERRORS, *SSL_ERRORS, IOError => e
171
+ @sock.write(bytes)
172
+ rescue SystemCallError, *TIMEOUT_ERRORS, *SSL_ERRORS => e
250
173
  error_on_request!(e)
251
174
  end
252
175
 
253
176
  def flush
254
- flush_write_buffer
255
- rescue SystemCallError, *TIMEOUT_ERRORS, *SSL_ERRORS, IOError => e
177
+ @sock.flush
178
+ rescue SystemCallError, *TIMEOUT_ERRORS, *SSL_ERRORS => e
256
179
  error_on_request!(e)
257
180
  end
258
181
 
259
182
  # Non-blocking read. Here to support the operation
260
183
  # of the get_multi operation
261
184
  def read_nonblock
262
- flush_write_buffer
263
185
  @sock.read_available
264
186
  end
265
187
 
@@ -286,9 +208,8 @@ module Dalli
286
208
  raise Dalli::RetryableNetworkError, message
287
209
  end
288
210
 
289
- # Called on connect. Deliberately leaves @fail_count alone; see
290
- # finish_request!.
291
211
  def reset_down_info
212
+ @fail_count = 0
292
213
  @down_at = nil
293
214
  @last_down_at = nil
294
215
  @msg = nil
@@ -313,18 +234,13 @@ module Dalli
313
234
  def reconnect_on_fork
314
235
  message = 'Fork detected, re-connecting child process...'
315
236
  Dalli.logger.info { message }
316
- # Drops anything the parent had buffered, closes the inherited socket
317
- # without touching the parent's connection, and reconnects immediately
237
+ # Close socket on a fork and reconnect immediately
318
238
  close
319
239
  establish_connection
320
240
  end
321
241
 
322
242
  def fork_detected?
323
- # Process.pid rather than PIDCache: PIDCache is refreshed by a
324
- # Process._fork hook, and another library's fork hook (such as
325
- # connection_pool closing its connections) can run in the child before
326
- # it, which would close the parent's TLS session as if it were ours.
327
- @pid && @pid != Process.pid
243
+ @pid && @pid != PIDCache.pid
328
244
  end
329
245
 
330
246
  def log_down_detected
@@ -345,45 +261,6 @@ module Dalli
345
261
  time = Time.now - @down_at
346
262
  Dalli.logger.warn { format('%<name>s is back (downtime was %<time>.3f seconds)', name: name, time: time) }
347
263
  end
348
-
349
- private
350
-
351
- # One write per flush. A TLS socket sends each argument of
352
- # write(*parts) as its own record and system call, so the parts are
353
- # joined first.
354
- def flush_write_buffer
355
- return if @write_buffer.empty?
356
-
357
- with_tls_timeout { @sock.write(@write_buffer.size == 1 ? @write_buffer.first : joined_write_buffer) }
358
- discard_write_buffer
359
- end
360
-
361
- def with_tls_timeout
362
- return yield unless @sock.is_a?(Dalli::Socket::SSLSocket)
363
-
364
- Timeout.timeout(socket_timeout) { yield } # rubocop:disable Style/ExplicitBlockArgument
365
- end
366
-
367
- # Joined as bytes: requests can mix UTF-8 and binary strings, which
368
- # String#<< refuses to combine when both hold non-ASCII bytes
369
- if String.method_defined?(:append_as_bytes)
370
- def joined_write_buffer
371
- out = String.new(capacity: @write_buffer_bytes, encoding: Encoding::BINARY)
372
- @write_buffer.each { |part| out.append_as_bytes(part) }
373
- out
374
- end
375
- else
376
- def joined_write_buffer
377
- out = String.new(capacity: @write_buffer_bytes, encoding: Encoding::BINARY)
378
- @write_buffer.each { |part| out << part.b }
379
- out
380
- end
381
- end
382
-
383
- def discard_write_buffer
384
- @write_buffer.clear
385
- @write_buffer_bytes = 0
386
- end
387
264
  end
388
265
  end
389
266
  end