Class: ActiveRecord::Bogacs::DefaultPool::Queue

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/bogacs/default_pool.rb

Instance Method Summary collapse

Constructor Details

#initialize(lock) ⇒ Queue

ConnectionLeasingQueue



25
26
27
28
29
30
# File 'lib/active_record/bogacs/default_pool.rb', line 25

def initialize(lock)
  @lock = lock
  @cond = @lock.new_cond
  @num_waiting = 0
  @queue = []
end

Instance Method Details

#add(element) ⇒ Object

Add element to the queue. Never blocks.



48
49
50
51
52
53
# File 'lib/active_record/bogacs/default_pool.rb', line 48

def add(element)
  synchronize do
    @queue.push element
    @cond.signal
  end
end

#any_waiting?Boolean

Test if any threads are currently waiting on the queue.

Returns:

  • (Boolean)


33
34
35
36
37
# File 'lib/active_record/bogacs/default_pool.rb', line 33

def any_waiting?
  synchronize do
    @num_waiting > 0
  end
end

#clearObject

Remove all elements from the queue.



63
64
65
66
67
# File 'lib/active_record/bogacs/default_pool.rb', line 63

def clear
  synchronize do
    @queue.clear
  end
end

#delete(element) ⇒ Object

If element is in the queue, remove and return it, or nil.



56
57
58
59
60
# File 'lib/active_record/bogacs/default_pool.rb', line 56

def delete(element)
  synchronize do
    @queue.delete(element)
  end
end

#num_waitingObject

Returns the number of threads currently waiting on this queue.



41
42
43
44
45
# File 'lib/active_record/bogacs/default_pool.rb', line 41

def num_waiting
  synchronize do
    @num_waiting
  end
end

#poll(timeout = nil) ⇒ Object

Remove the head of the queue.

If timeout is not given, remove and return the head the queue if the number of available elements is strictly greater than the number of threads currently waiting (that is, don’t jump ahead in line). Otherwise, return nil.

If timeout is given, block if it there is no element available, waiting up to timeout seconds for an element to become available.

becomes available after timeout seconds

Raises:

  • (ActiveRecord::ConnectionTimeoutError)

    if timeout given and no element



82
83
84
# File 'lib/active_record/bogacs/default_pool.rb', line 82

def poll(timeout = nil)
  synchronize { internal_poll(timeout) }
end