Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Catch and handle socket sharing, fixes #208

commit ee5193e206c3934cb17596ae88099ea36da8d576 1 parent 371adde
@mperham authored
Showing with 11 additions and 33 deletions.
  1. +1 −0  History.md
  2. +4 −31 README.md
  3. +6 −2 lib/dalli/server.rb
View
1  History.md
@@ -4,6 +4,7 @@ Dalli Changelog
2.0.4
=======
+- Dalli no longer needs to be reset after Unicorn/Passenger fork [#208]
- Add option to re-raise errors rescued in the session and cache stores. [pitr, #200]
- DalliStore#fetch called the block if the cached value == false [#205]
- DalliStore should have accessible options [#195]
View
35 README.md
@@ -91,37 +91,6 @@ Both cache and session stores support `:raise_errors` parameter, which propagate
Dalli does not support Rails 2.x any longer.
-Usage with Passenger
-------------------------
-
-Put this at the bottom of `config/environment.rb`:
-
- if defined?(PhusionPassenger)
- PhusionPassenger.on_event(:starting_worker_process) do |forked|
- # Reset Rails's object cache
- # Only works with DalliStore
- Rails.cache.reset if forked
-
- # Reset Rails's session store
- # If you know a cleaner way to find the session store instance, please let me know
- ObjectSpace.each_object(ActionDispatch::Session::DalliStore) { |obj| obj.reset }
- end
- end
-
-Usage with Unicorn
------------------------
-
-Modify the `after_fork` block in your unicorn config file:
-
- after_fork do |server, worker|
- Rails.cache.reset if Rails.cache.respond_to?(:reset)
-
- # Reset Rails's session store
- # If you know a cleaner way to find the session store instance, please let me know
- ObjectSpace.each_object(ActionDispatch::Session::DalliStore) { |obj| obj.reset }
- end
-
-
Configuration
------------------------
Dalli::Client accepts the following options. All times are in seconds.
@@ -153,6 +122,10 @@ Features and Changes
By default, Dalli is thread-safe. Disable thread-safety at your own peril.
+Dalli does not need anything special in Unicorn/Passenger since 2.0.4.
+It will detect sockets shared with child processes and gracefully reopen the
+socket.
+
Note that Dalli does not require ActiveSupport or Rails. You can safely use it in your own Ruby projects.
View
8 lib/dalli/server.rb
@@ -37,11 +37,12 @@ def initialize(attribs, options = {})
@options = DEFAULTS.merge(options)
@sock = nil
@msg = nil
+ @pid = nil
end
# Chokepoint method for instrumentation
def request(op, *args)
- check_for_inprogress
+ verify_state
raise Dalli::NetworkError, "#{hostname}:#{port} is down: #{@error} #{@msg}" unless alive?
begin
send(op, *args)
@@ -81,6 +82,7 @@ def close
return unless @sock
@sock.close rescue nil
@sock = nil
+ @pid = nil
@inprogress = false
end
@@ -94,8 +96,9 @@ def unlock!
private
- def check_for_inprogress
+ def verify_state
failure! if @inprogress
+ failure! if @pid && @pid != Process.pid
end
def failure!
@@ -398,6 +401,7 @@ def connect
Dalli.logger.debug { "Dalli::Server#connect #{hostname}:#{port}" }
begin
+ @pid = Process.pid
@sock = KSocket.open(hostname, port, options)
@version = version # trigger actual connect
sasl_authentication if need_auth?
Please sign in to comment.
Something went wrong with that request. Please try again.