Sha256: 48d4122db4475ca028f8d50065024c810cd35b7321a0258b15ed4e7b44946a97

Contents?: true

Size: 854 Bytes

Versions: 7

Compression:

Stored size: 854 Bytes

Contents

# frozen_string_literal: true

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

def handle_client(socket)
  pending_requests = []
  parser = Http::Parser.new
  parser.on_message_complete = proc { pending_requests << parser }

  socket.recv_loop do |data|
    parser << data
    write_response(socket) while pending_requests.shift
  end
rescue IOError, SystemCallError => e
  # do nothing
ensure
  socket&.close
end

def write_response(socket)
  status_code = "200 OK"
  data = "Hello world!\n"
  headers = "Content-Type: text/plain\r\nContent-Length: #{data.bytesize}\r\n"
  socket.write "HTTP/1.1 #{status_code}\r\n#{headers}\r\n#{data}"
end

server = TCPServer.open('0.0.0.0', 1234)
puts "pid #{Process.pid} Polyphony (#{Thread.current.backend.kind}) listening on port 1234"

server.accept_loop do |c|
  spin { handle_client(c) }
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
polyphony-0.47.5.1 examples/performance/thread-vs-fiber/polyphony_server.rb
polyphony-0.47.5 examples/performance/thread-vs-fiber/polyphony_server.rb
polyphony-0.47.4 examples/performance/thread-vs-fiber/polyphony_server.rb
polyphony-0.47.3 examples/performance/thread-vs-fiber/polyphony_server.rb
polyphony-0.47.2 examples/performance/thread-vs-fiber/polyphony_server.rb
polyphony-0.47.1 examples/performance/thread-vs-fiber/polyphony_server.rb
polyphony-0.47.0 examples/performance/thread-vs-fiber/polyphony_server.rb