Class: Tobox::FiberPool

Inherits:
Pool
  • Object
show all
Defined in:
lib/tobox/pool/fiber_pool.rb

Instance Method Summary collapse

Methods inherited from Pool

#do_work

Constructor Details

#initialize(_) ⇒ FiberPool

Returns a new instance of FiberPool.



8
9
10
11
12
13
14
15
16
# File 'lib/tobox/pool/fiber_pool.rb', line 8

def initialize(_)
  Sequel.extension(:fiber_concurrency)
  super
  @fibers = []

  @fiber_mtx = Mutex.new
  @fiber_cond = ConditionVariable.new
  @fiber_thread = nil
end

Instance Method Details

#startObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/tobox/pool/fiber_pool.rb', line 18

def start
  @fiber_thread = Thread.start do
    Thread.current.name = "tobox-fibers-thread"

    begin
      FiberScheduler do
        @fiber_mtx.synchronize do
          @workers.each do |worker|
            @fibers << start_fiber_worker(worker)
          end
          @fiber_cond.signal
        end
      end
    rescue KillError
      @fibers.each { |f| f.raise(KillError) }
    end
  end
  @fiber_mtx.synchronize do
    @fiber_cond.wait(@fiber_mtx)
  end
end

#stopObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/tobox/pool/fiber_pool.rb', line 40

def stop
  shutdown_timeout = @configuration[:shutdown_timeout]
  grace_shutdown_timeout = @configuration[:grace_shutdown_timeout]

  super

  @fiber_thread.join(shutdown_timeout)

  return unless @fiber_thread.alive?

  @fiber_thread.raise(KillError)
  @fiber_thread.join(grace_shutdown_timeout)
  @fiber_thread.kill
  @fiber_thread.join(1)
end