Sha256: 5eaebd31e7124699b46931fa3360ff118426c35b836d32248bcb757046a4b1b8

Contents?: true

Size: 1.39 KB

Versions: 90

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

require 'bundler/setup'
require 'polyphony'
require 'http/parser'

class Http::Parser
  def setup_async
    self.on_message_complete = proc { @request_complete = true }
  end

  def parse(data)
    self << data
    return nil unless @request_complete

    @request_complete = nil
    self
  end
end

def handle_client(socket)
  parser = Http::Parser.new
  reqs = []
  parser.on_message_complete = proc do |env|
    reqs << Object.new # parser
  end
  socket.read_loop do |data|
    parser << data
    while (req = reqs.shift)
      handle_request(socket, req)
      req = nil
      # snooze
    end
  end
rescue IOError, SystemCallError => e
  # do nothing
ensure
  socket&.close
  parser.reset!
end

def handle_request(client, parser)
  status_code = 200
  data = "Hello world!\n"
  headers = "Content-Length: #{data.bytesize}\r\n"
  client.write "HTTP/1.1 #{status_code}\r\n#{headers}\r\n#{data}"
end

$incoming = Polyphony::Queue.new

$threads = (1..4).map {
  Thread.new {
    Thread.current.setup_fiber_scheduling
    loop {
      conn = $incoming.pop
      puts "#{Thread.current.inspect} pop #{conn.inspect}"
      spin { handle_client(conn) }
    }
  }
}

spin do
  server = TCPServer.open('0.0.0.0', 1234)
  puts "listening on port 1234"

  loop do
    client = server.accept
    $incoming << client
    # spin { handle_client(client) }
  end
end

puts "pid #{Process.pid}"
suspend

Version data entries

90 entries across 90 versions & 1 rubygems

Version Path
polyphony-1.6 examples/performance/thread-vs-fiber/polyphony_mt_server.rb
polyphony-1.5 examples/performance/thread-vs-fiber/polyphony_mt_server.rb
polyphony-1.4 examples/performance/thread-vs-fiber/polyphony_mt_server.rb
polyphony-1.3 examples/performance/thread-vs-fiber/polyphony_mt_server.rb
polyphony-1.2.1 examples/performance/thread-vs-fiber/polyphony_mt_server.rb
polyphony-1.2 examples/performance/thread-vs-fiber/polyphony_mt_server.rb
polyphony-1.1.1 examples/performance/thread-vs-fiber/polyphony_mt_server.rb
polyphony-1.1 examples/performance/thread-vs-fiber/polyphony_mt_server.rb
polyphony-1.0.2 examples/performance/thread-vs-fiber/polyphony_mt_server.rb
polyphony-1.0.1 examples/performance/thread-vs-fiber/polyphony_mt_server.rb
polyphony-1.0 examples/performance/thread-vs-fiber/polyphony_mt_server.rb
polyphony-0.99.6 examples/performance/thread-vs-fiber/polyphony_mt_server.rb
polyphony-0.99.5 examples/performance/thread-vs-fiber/polyphony_mt_server.rb
polyphony-0.99.4 examples/performance/thread-vs-fiber/polyphony_mt_server.rb
polyphony-0.99.3 examples/performance/thread-vs-fiber/polyphony_mt_server.rb
polyphony-0.99.2 examples/performance/thread-vs-fiber/polyphony_mt_server.rb
polyphony-0.99.1 examples/performance/thread-vs-fiber/polyphony_mt_server.rb
polyphony-0.99 examples/performance/thread-vs-fiber/polyphony_mt_server.rb
polyphony-0.98 examples/performance/thread-vs-fiber/polyphony_mt_server.rb
polyphony-0.97 examples/performance/thread-vs-fiber/polyphony_mt_server.rb