Class: Riser::TimeoutSizedQueue
- Inherits:
-
Object
- Object
- Riser::TimeoutSizedQueue
- Defined in:
- lib/riser/server.rb
Instance Method Summary collapse
- #at_end_of_queue? ⇒ Boolean
- #close ⇒ Object
- #closed? ⇒ Boolean
- #empty? ⇒ Boolean
-
#initialize(size, name: nil) ⇒ TimeoutSizedQueue
constructor
A new instance of TimeoutSizedQueue.
- #pop ⇒ Object
- #push(value, timeout_seconds) ⇒ Object
- #size ⇒ Object (also: #length)
- #stat_get(reset: true) ⇒ Object
- #stat_start ⇒ Object
- #stat_stop ⇒ Object
Constructor Details
#initialize(size, name: nil) ⇒ TimeoutSizedQueue
Returns a new instance of TimeoutSizedQueue.
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/riser/server.rb', line 8 def initialize(size, name: nil) @size = size @queue = [] @closed = false @mutex = Thread::Mutex.new @push_cond = Thread::ConditionVariable.new @pop_cond = Thread::ConditionVariable.new @name = name && name.dup.freeze @stat_enable = false end |
Instance Method Details
#at_end_of_queue? ⇒ Boolean
41 42 43 |
# File 'lib/riser/server.rb', line 41 def at_end_of_queue? @mutex.synchronize{ @closed && @queue.empty? } end |
#close ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/riser/server.rb', line 33 def close @mutex.synchronize{ @closed = true @pop_cond.broadcast } nil end |
#closed? ⇒ Boolean
29 30 31 |
# File 'lib/riser/server.rb', line 29 def closed? @mutex.synchronize{ @closed } end |
#empty? ⇒ Boolean
25 26 27 |
# File 'lib/riser/server.rb', line 25 def empty? @mutex.synchronize{ @queue.empty? } end |
#pop ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/riser/server.rb', line 66 def pop @mutex.synchronize{ if (@stat_enable) then @stat_pop_count += 1 @stat_pop_average_queue_size = (@stat_pop_average_queue_size * (@stat_pop_count - 1) + @queue.size) / @stat_pop_count end while (@queue.empty?) @closed and return @stat_pop_wait_count += 1 if @stat_enable @pop_cond.wait(@mutex) end @push_cond.signal @queue.shift } end |
#push(value, timeout_seconds) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/riser/server.rb', line 45 def push(value, timeout_seconds) @mutex.synchronize{ @closed and raise 'closed' if (@stat_enable) then @stat_push_count += 1 @stat_push_average_queue_size = (@stat_push_average_queue_size * (@stat_push_count - 1) + @queue.size) / @stat_push_count end unless (@queue.size < @size) then @stat_push_wait_count += 1 if @stat_enable @push_cond.wait(@mutex, timeout_seconds) unless (@queue.size < @size) then @stat_push_timeout_count += 1 if @stat_enable return end end @pop_cond.signal @queue.push(value) self } end |
#size ⇒ Object Also known as: length
19 20 21 |
# File 'lib/riser/server.rb', line 19 def size @mutex.synchronize{ @queue.size } end |
#stat_get(reset: true) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/riser/server.rb', line 111 def stat_get(reset: true) if (@stat_enable) then info = nil @mutex.synchronize{ info = { queue_name: @name, queue_size: @size, closed: @closed, start_time: @stat_start_time, push_average_queue_size: @stat_push_average_queue_size, push_count: @stat_push_count, push_wait_count: @stat_push_wait_count, push_timeout_count: @stat_push_timeout_count, pop_average_queue_size: @stat_pop_average_queue_size, pop_count: @stat_pop_count, pop_wait_count: @stat_pop_wait_count } if (reset) then stat_reset_no_lock end } info[:get_time] = Time.now info[:elapsed_seconds] = info[:get_time] - info[:start_time] info[:push_wait_ratio] = info[:push_wait_count].to_f / info[:push_count] info[:push_timeout_ratio] = info[:push_timeout_count].to_f / info[:push_count] info[:pop_wait_ratio] = info[:pop_wait_count].to_f / info[:pop_count] # sort [ :queue_name, :queue_size, :closed, :start_time, :get_time, :elapsed_seconds, :push_average_queue_size, :push_count, :push_wait_count, :push_wait_ratio, :push_timeout_count, :push_timeout_ratio, :pop_average_queue_size, :pop_count, :pop_wait_count, :pop_wait_ratio ].each do |name| info[name] = info.delete(name) end info end end |
#stat_start ⇒ Object
94 95 96 97 98 99 100 101 102 |
# File 'lib/riser/server.rb', line 94 def stat_start @mutex.synchronize{ unless (@stat_enable) then stat_reset_no_lock @stat_enable = true end } nil end |
#stat_stop ⇒ Object
104 105 106 107 108 109 |
# File 'lib/riser/server.rb', line 104 def stat_stop @mutex.synchronize{ @stat_enable = false } nil end |