Skip to content
This repository
Browse code

Catch and handle socket sharing, fixes #208

  • Loading branch information...
commit ee5193e206c3934cb17596ae88099ea36da8d576 1 parent 371adde
Mike Perham authored
1  History.md
Source Rendered
@@ -4,6 +4,7 @@ Dalli Changelog
4 4
 2.0.4
5 5
 =======
6 6
 
  7
+- Dalli no longer needs to be reset after Unicorn/Passenger fork [#208]
7 8
 - Add option to re-raise errors rescued in the session and cache stores. [pitr, #200]
8 9
 - DalliStore#fetch called the block if the cached value == false [#205]
9 10
 - DalliStore should have accessible options [#195]
35  README.md
Source Rendered
@@ -91,37 +91,6 @@ Both cache and session stores support `:raise_errors` parameter, which propagate
91 91
 Dalli does not support Rails 2.x any longer.
92 92
 
93 93
 
94  
-Usage with Passenger
95  
-------------------------
96  
-
97  
-Put this at the bottom of `config/environment.rb`:
98  
-
99  
-    if defined?(PhusionPassenger)
100  
-      PhusionPassenger.on_event(:starting_worker_process) do |forked|
101  
-        # Reset Rails's object cache
102  
-        # Only works with DalliStore
103  
-        Rails.cache.reset if forked
104  
-
105  
-        # Reset Rails's session store
106  
-        # If you know a cleaner way to find the session store instance, please let me know
107  
-        ObjectSpace.each_object(ActionDispatch::Session::DalliStore) { |obj| obj.reset }
108  
-      end
109  
-    end
110  
-
111  
-Usage with Unicorn
112  
------------------------
113  
-
114  
-Modify the `after_fork` block in your unicorn config file:
115  
-
116  
-    after_fork do |server, worker|
117  
-      Rails.cache.reset if Rails.cache.respond_to?(:reset)
118  
-
119  
-      # Reset Rails's session store
120  
-      # If you know a cleaner way to find the session store instance, please let me know
121  
-      ObjectSpace.each_object(ActionDispatch::Session::DalliStore) { |obj| obj.reset }
122  
-    end
123  
-
124  
-
125 94
 Configuration
126 95
 ------------------------
127 96
 Dalli::Client accepts the following options. All times are in seconds.
@@ -153,6 +122,10 @@ Features and Changes
153 122
 
154 123
 By default, Dalli is thread-safe.  Disable thread-safety at your own peril.
155 124
 
  125
+Dalli does not need anything special in Unicorn/Passenger since 2.0.4.
  126
+It will detect sockets shared with child processes and gracefully reopen the
  127
+socket.
  128
+
156 129
 Note that Dalli does not require ActiveSupport or Rails.  You can safely use it in your own Ruby projects.
157 130
 
158 131
 
8  lib/dalli/server.rb
@@ -37,11 +37,12 @@ def initialize(attribs, options = {})
37 37
       @options = DEFAULTS.merge(options)
38 38
       @sock = nil
39 39
       @msg = nil
  40
+      @pid = nil
40 41
     end
41 42
 
42 43
     # Chokepoint method for instrumentation
43 44
     def request(op, *args)
44  
-      check_for_inprogress
  45
+      verify_state
45 46
       raise Dalli::NetworkError, "#{hostname}:#{port} is down: #{@error} #{@msg}" unless alive?
46 47
       begin
47 48
         send(op, *args)
@@ -81,6 +82,7 @@ def close
81 82
       return unless @sock
82 83
       @sock.close rescue nil
83 84
       @sock = nil
  85
+      @pid = nil
84 86
       @inprogress = false
85 87
     end
86 88
 
@@ -94,8 +96,9 @@ def unlock!
94 96
 
95 97
     private
96 98
 
97  
-    def check_for_inprogress
  99
+    def verify_state
98 100
       failure! if @inprogress
  101
+      failure! if @pid && @pid != Process.pid
99 102
     end
100 103
 
101 104
     def failure!
@@ -398,6 +401,7 @@ def connect
398 401
       Dalli.logger.debug { "Dalli::Server#connect #{hostname}:#{port}" }
399 402
 
400 403
       begin
  404
+        @pid = Process.pid
401 405
         @sock = KSocket.open(hostname, port, options)
402 406
         @version = version # trigger actual connect
403 407
         sasl_authentication if need_auth?

0 notes on commit ee5193e

Please sign in to comment.
Something went wrong with that request. Please try again.