Class: Riser::TimeoutSizedQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/riser/server.rb

Instance Method Summary collapse

Constructor Details

#initialize(max_size, name: nil) ⇒ TimeoutSizedQueue

Returns a new instance of TimeoutSizedQueue.



9
10
11
12
13
14
15
16
17
18
# File 'lib/riser/server.rb', line 9

def initialize(max_size, name: nil)
  @max_size = max_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

Returns:

  • (Boolean)


42
43
44
# File 'lib/riser/server.rb', line 42

def at_end_of_queue?
  @mutex.synchronize{ @closed && @queue.empty? }
end

#closeObject



34
35
36
37
38
39
40
# File 'lib/riser/server.rb', line 34

def close
  @mutex.synchronize{
    @closed = true
    @pop_cond.broadcast
  }
  nil
end

#closed?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/riser/server.rb', line 30

def closed?
  @mutex.synchronize{ @closed }
end

#empty?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/riser/server.rb', line 26

def empty?
  @mutex.synchronize{ @queue.empty? }
end

#popObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/riser/server.rb', line 67

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 if (@queue.size == @max_size)
    @queue.shift
  }
end

#push(value, timeout_seconds) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/riser/server.rb', line 46

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 < @max_size) then
      @stat_push_wait_count += 1 if @stat_enable
      @push_cond.wait(@mutex, timeout_seconds)
      unless (@queue.size < @max_size) then
        @stat_push_timeout_count += 1 if @stat_enable
        return
      end
    end
    @pop_cond.signal if @queue.empty?
    @queue.push(value)
    self
  }
end

#sizeObject Also known as: length



20
21
22
# File 'lib/riser/server.rb', line 20

def size
  @mutex.synchronize{ @queue.size }
end

#stat_get(reset: true) ⇒ Object



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
164
# File 'lib/riser/server.rb', line 112

def stat_get(reset: true)
  if (@stat_enable) then
    info = nil
    @mutex.synchronize{
      info = {
        queue_name:              @name,
        max_size:                @max_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,
      :max_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_startObject



95
96
97
98
99
100
101
102
103
# File 'lib/riser/server.rb', line 95

def stat_start
  @mutex.synchronize{
    unless (@stat_enable) then
      stat_reset_no_lock
      @stat_enable = true
    end
  }
  nil
end

#stat_stopObject



105
106
107
108
109
110
# File 'lib/riser/server.rb', line 105

def stat_stop
  @mutex.synchronize{
    @stat_enable = false
  }
  nil
end