Sha256: de5e884bbe3cb3d8f674b630ee476061829fea4d00a4d8de6d14583b74309897

Contents?: true

Size: 1.11 KB

Versions: 7

Compression:

Stored size: 1.11 KB

Contents

# frozen_string_literal: true

require "timeout"
require "fiber_scheduler"

module Tobox
  class FiberPool < Pool
    class KillError < Interrupt; end

    def initialize(_configuration)
      Sequel.extension(:fiber_concurrency)
      super
      @error_handlers = Array(@configuration.lifecycle_events[:error])
    end

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

        FiberScheduler do
          @workers.each_with_index do |wk, _idx|
            Fiber.schedule do
              wk.work
            rescue KillError
              # noop
            rescue Exception => e # rubocop:disable Lint/RescueException
              @error_handlers.each { |hd| hd.call(:tobox_error, e) }
              raise e
            end
          end
        end
      end
    end

    def stop
      shutdown_timeout = @configuration[:shutdown_timeout]

      super

      begin
        Timeout.timeout(shutdown_timeout) { @fiber_thread.value }
      rescue Timeout::Error
        # hard exit
        @fiber_thread.raise(KillError)
        @fiber_thread.value
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
tobox-0.1.6 lib/tobox/pool/fiber_pool.rb
tobox-0.1.5 lib/tobox/pool/fiber_pool.rb
tobox-0.1.4 lib/tobox/pool/fiber_pool.rb
tobox-0.1.3 lib/tobox/pool/fiber_pool.rb
tobox-0.1.2 lib/tobox/pool/fiber_pool.rb
tobox-0.1.1 lib/tobox/pool/fiber_pool.rb
tobox-0.1.0 lib/tobox/pool/fiber_pool.rb