Skip to content

Commit

Permalink
detect forks
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshMcKin committed May 16, 2016
1 parent 070f98f commit 2178202
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 59 deletions.
3 changes: 2 additions & 1 deletion HISTORY.md
Expand Up @@ -5,11 +5,12 @@ Head
=======
- None yet.

1.0.1
1.1.0
=======
- Close orphan clients outside of synchonize
- Freeze alarm message
- Detect dead resources and reap
- Detect fork

1.0.0
=======
Expand Down
29 changes: 1 addition & 28 deletions README.md
Expand Up @@ -140,35 +140,8 @@ a lambda that accepts the client as an argument or symbol representing a method

## Forking

HotTub's `#reset!` methods close all idle connections, prevents connections in use from returning
to the pool and attempts to close orphaned connections as they attempt to return.
HotTub::Pool automatically detects forks and drains the pool, so no additional "after fork" code is required.

# Puma
on_worker_boot do

# If you let HotTub manage all your connections
HotTub.reset!

# If you have your own HotTub::Sessions
MY_SESSIONS.reset!

# If you have any one-off pools
MY_POOL.reset!

end

# Unicorn
before_fork do |server, worker|

# If you let HotTub manage all your connections
HotTub.reset!

# If you have your own HotTub::Sessions
MY_SESSIONS.reset!

# If you have any one-off pools
MY_POOL.reset!
end


## Contributing to HotTub
Expand Down
41 changes: 11 additions & 30 deletions lib/hot_tub/pool.rb
Expand Up @@ -116,12 +116,15 @@ def initialize(opts={},&client_block)

@never_block = (@max_size == 0)

@pid = Process.pid

at_exit {shutdown!} unless @sessions_key
end

# Preform an operations with a client/connection.
# Requires a block that receives the client.
def run
drain! if forked?
clnt = pop
yield clnt
ensure
Expand Down Expand Up @@ -158,48 +161,22 @@ def drain!
ensure
@_out.clear
@_pool.clear
@pid = Process.pid
@cond.broadcast
end
end
nil
end
alias :close! :drain!

# Reset the pool.
# or if shutdown allow threads to quickly finish their work
# Clients from the previous pool will not return to pool.
def reset!
HotTub.logger.info "[HotTub] Resetting pool #{@name}!" if HotTub.logger
@mutex.synchronize do
begin
while clnt = @_pool.pop
close_client(clnt)
end
ensure
@_out.clear
@_pool.clear
@cond.broadcast
end
end
nil
end
alias :reset! :drain!

# Kills the reaper and drains the pool.
def shutdown!
HotTub.logger.info "[HotTub] Shutting down pool #{@name}!" if HotTub.logger
@shutdown = true
kill_reaper if @reaper
@mutex.synchronize do
begin
while clnt = @_pool.pop
close_client(clnt)
end
ensure
@_out.clear
@_pool.clear
@cond.broadcast
end
end
drain!
@shutdown = false
nil
end

Expand Down Expand Up @@ -250,6 +227,10 @@ def max_size=max_size
@max_size = max_size
end

def forked?
@pid != Process.pid
end

private

ALARM_MESSAGE = "Could not fetch a free client in time. Consider increasing your pool size.".freeze
Expand Down

0 comments on commit 2178202

Please sign in to comment.