Class: PgHelper::ConnectionPool::Queue

Inherits:
Object
  • Object
show all
Defined in:
lib/pg_helper/connection_pool.rb

Overview

nearly standard queue, but with timeout on wait FIXME: custom class inherit from www.ruby-doc.org/stdlib-2.0/libdoc/thread/rdoc/Queue.html

Instance Method Summary collapse

Constructor Details

#initialize(lock = Monitor.new) ⇒ Queue

Returns a new instance of Queue.



34
35
36
37
38
39
# File 'lib/pg_helper/connection_pool.rb', line 34

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

Instance Method Details

#add(element) ⇒ Object

Add element to the queue. Never blocks.



58
59
60
61
62
63
# File 'lib/pg_helper/connection_pool.rb', line 58

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)


42
43
44
45
46
# File 'lib/pg_helper/connection_pool.rb', line 42

def any_waiting?
  synchronize do
    @num_waiting > 0
  end
end

#clearObject

Remove all elements from the queue.



73
74
75
76
77
# File 'lib/pg_helper/connection_pool.rb', line 73

def clear
  synchronize do
    @queue.clear
  end
end

#delete(element) ⇒ Object

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



66
67
68
69
70
# File 'lib/pg_helper/connection_pool.rb', line 66

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

#num_waitingObject

Return the number of threads currently waiting on this queue.



50
51
52
53
54
55
# File 'lib/pg_helper/connection_pool.rb', line 50

def num_waiting
  synchronize do
    $DEBUG && warn(@num_waiting.to_s)
    @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.

Raises:

  • ConnectionTimeoutError if timeout is given and no element

becomes available after timeout seconds,



93
94
95
96
97
98
99
100
101
# File 'lib/pg_helper/connection_pool.rb', line 93

def poll(timeout = nil)
  synchronize do
    if timeout
      no_wait_poll || wait_poll(timeout)
    else
      no_wait_poll
    end
  end
end