Skip to content

Commit

Permalink
Remove @busy variable, more precise timeout waits
Browse files Browse the repository at this point in the history
  • Loading branch information
mperham committed Sep 18, 2011
1 parent 6f39ecd commit ecea6ea
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 7 deletions.
6 changes: 1 addition & 5 deletions lib/connection_pool.rb
Expand Up @@ -34,7 +34,6 @@ def initialize(options={})
@options[:size].times do
@available << yield
end
@busy = []
end

def with(&block)
Expand All @@ -54,17 +53,14 @@ def method_missing(name, *args)

def checkout
Thread.current[:"current-#{self.object_id}"] ||= begin
conn = @available.timed_pop(@options[:timeout])
@busy << conn
conn
@available.timed_pop(@options[:timeout])
end
end

def checkin
conn = Thread.current[:"current-#{self.object_id}"]
Thread.current[:"current-#{self.object_id}"] = nil
return unless conn
@busy.delete(conn)
@available << conn
nil
end
Expand Down
5 changes: 3 additions & 2 deletions lib/connection_pool/timed_queue.rb
Expand Up @@ -21,8 +21,9 @@ def timed_pop(timeout=0.5)
@mutex.synchronize do
loop do
return @que.shift unless @que.empty?
raise Timeout::Error if Time.now > deadline
@resource.wait(@mutex, timeout)
to_wait = deadline - Time.now
raise Timeout::Error if to_wait <= 0
@resource.wait(@mutex, to_wait)
end
end
end
Expand Down

0 comments on commit ecea6ea

Please sign in to comment.