class Sorta::Http::Server

Constants

CPU_COUNT

Public Class Methods

new(app, **options) click to toggle source
# File lib/sorta/http/server.rb, line 14
def initialize(app, **options)
  @app = app
  @options = options
  @cpu_count = options[:workers] || CPU_COUNT
  @port = options[:port] || 8080
  @host = options[:host] || 'localhost'
  @logger = options[:logger] || Logger.new

  init_pipe
  init_workers
  # TODO: init_supervisor
end

Public Instance Methods

run() click to toggle source
# File lib/sorta/http/server.rb, line 27
def run
  tcp_server = TCPServer.new(@host, @port)
  loop { @pipe.send(tcp_server.accept, move: true) }
end

Private Instance Methods

init_pipe() click to toggle source
# File lib/sorta/http/server.rb, line 34
def init_pipe
  @pipe = Ractor.new do
    loop { Ractor.yield(Ractor.receive, move: true) }
  end
end
init_workers() click to toggle source
# File lib/sorta/http/server.rb, line 40
def init_workers
  @workers ||= @cpu_count.times.map do
    Ractor.new(@app, @pipe, @logger) do |app, pipe, logger|
      app = app.compile! if app.respond_to? :compile!
      Worker.new(pipe, app, logger: logger).run
    end
  end
end