rainbows.git  about / heads / tags
Unicorn for sleepy apps and slow clients
blob ce888d875eb87f45e73b0e7eb050f14fe2efa871 653 bytes (raw)
$ git show v3.2.0:lib/rainbows/queue_pool.rb	# shows this blob on the CLI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 
# -*- encoding: binary -*-
# :enddoc:
require 'thread'

# Thread pool class based on pulling off a single Ruby Queue.
# This is NOT used for the ThreadPool class, since that class does not
# need a userspace Queue.
class Rainbows::QueuePool < Struct.new(:queue, :threads)
  def initialize(size = 20)
    q = Queue.new
    self.threads = (1..size).map do
      Thread.new do
        while job = q.shift
          yield job
        end
      end
    end
    self.queue = q
  end

  def quit!
    threads.each { |_| queue << nil }
    threads.delete_if do |t|
      Rainbows.tick
      t.alive? ? t.join(0.01) : true
    end until threads.empty?
  end
end

git clone https://yhbt.net/rainbows.git