Sha256: 14f38be346fe641b132924f3e4b4e73de30049ced271fc2dc1e025a639d77ce9

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 KB

Contents

# frozen_string_literal: true

require "socket"
require "logger"
require "webrick"

require_relative "processor"
require_relative "listener"
require_relative "env"
require_relative "ractor_helper"

module RightSpeed
  CONFIG_HOOK_KEY = 'right_speed_config_hooks'

  class Server
    DEFAULT_HOST = "127.0.0.1"
    DEFAULT_PORT = 8080
    DEFAULT_WORKER_TYPE = :roundrobin
    DEFAULT_WORKERS = Env.processors

    AVAILABLE_WORKER_TYPES = [:roundrobin, :fair, :accept]

    attr_reader :config_hooks

    def initialize(
          app:,
          host: DEFAULT_HOST,
          port: DEFAULT_PORT,
          workers: DEFAULT_WORKERS,
          worker_type: DEFAULT_WORKER_TYPE,
          backlog: nil
        )
      @host = host
      @port = port
      @app = app
      @workers = workers
      @worker_type = worker_type
      @backlog = backlog
      @config_hooks = []
      @logger = nil
    end

    def run
      logger = RightSpeed.logger
      logger.info { "Start running with #{@workers} workers" }

      hooks = @config_hooks + (Ractor.current[RightSpeed::CONFIG_HOOK_KEY] || [])
      hooks.each do |hook|
        if hook.respond_to?(:call)
          hook.call
        end
      end

      RactorHelper.uri_hook
      RactorHelper.rack_hook

      begin
        processor = Processor.setup(app: @app, worker_type: @worker_type, workers: @workers)
        listener = Listener.setup(worker_type: @worker_type, host: @host, port: @port, backlog: nil)
        processor.configure(listener: listener)
        processor.run
        listener.wait
        processor.wait
      ensure
        listener.stop rescue nil
        processor.stop rescue nil
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
right_speed-0.2.0 lib/right_speed/server.rb