Sha256: f4125f3a15a87e9cc7ee6175feca5aa42eea71513ac4b71a2df676afdb1ab7b5

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 KB

Contents

# frozen_string_literal: true

require "mooro"
require "mooro/server"

module Mooro
  module Plugin
    module InterruptableServer
      def make_worker(supervisor, logger, ractor_name: "interruptable_worker")
        block = Ractor.make_shareable(method(:serve).to_proc)
        Ractor.new(supervisor, logger, block, name: ractor_name) do |supervisor, logger, serve|
          clients = Thread::Queue.new
          runner = Thread.new do
            while (current_client = clients.pop)
              begin
                serve.call(current_client)
              rescue TerminateServer
                break
              rescue => err
                logger.send(err.to_s)
              ensure
                current_client&.close
              end
            end
          end
          begin
            until (client = supervisor.take) == :terminate
              clients.push(client)
            end
          rescue Ractor::ClosedError => closed_err
            logger.send("#{closed_err}: Supervisor's outgoing port is closed")
          ensure
            runner.raise(TerminateServer)
            runner.join
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mooro-0.1.0 lib/mooro/plugin/fault_tolerance.rb