Class: ConcurrentWorker::RequestCounter

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

Instance Method Summary collapse

Constructor Details

#initializeRequestCounter

Returns a new instance of RequestCounter.



9
10
11
12
# File 'lib/concurrent_worker.rb', line 9

def initialize
  @count = Queue.new
  @com = Queue.new
end

Instance Method Details

#closeObject



33
34
35
# File 'lib/concurrent_worker.rb', line 33

def close
  @count.close
end

#closed?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/concurrent_worker.rb', line 37

def closed?
  @count.closed?
end

#popObject



16
17
18
19
20
21
# File 'lib/concurrent_worker.rb', line 16

def pop
  Thread.handle_interrupt(Object => :never) do
    @count.pop
    @com.push(true)
  end
end

#push(args) ⇒ Object



13
14
15
# File 'lib/concurrent_worker.rb', line 13

def push(args)
  @count.push(args)
end

#restObject



41
42
43
44
45
46
47
48
49
# File 'lib/concurrent_worker.rb', line 41

def rest
  result = []
  until @count.empty?
    req = @count.pop
    next if req == []
    result.push(req)
  end
  result
end

#sizeObject



29
30
31
# File 'lib/concurrent_worker.rb', line 29

def size
  @count.size
end

#wait_until_less_than(n) ⇒ Object



23
24
25
26
27
28
# File 'lib/concurrent_worker.rb', line 23

def wait_until_less_than(n)
  return if @count.size < n
  while @com.pop
    break if @count.size < n
  end
end