class RubyWolf::Worker

Attributes

app[R]
connections[R]
pid[R]
server[R]
socket[R]

Public Class Methods

new(server) click to toggle source
# File lib/ruby_wolf/worker.rb, line 5
def initialize(server)
  @server = server
  @app = server.app
  @socket = server.socket
  @connections = []
end

Public Instance Methods

start() click to toggle source
# File lib/ruby_wolf/worker.rb, line 12
def start
  @pid = fork do
    RubyWolf.logger.info('Worker is ready')
    handle_loop
  end
end

Private Instance Methods

accept_connection() click to toggle source
# File lib/ruby_wolf/worker.rb, line 61
def accept_connection
  @connections << RubyWolf::Connection.new(socket.accept_nonblock)
rescue IO::WaitReadable, Errno::EINTR
end
close_connection(connection) click to toggle source
# File lib/ruby_wolf/worker.rb, line 66
def close_connection(connection)
  connection.close
  @connections.delete(connection)
end
handle_loop() click to toggle source
# File lib/ruby_wolf/worker.rb, line 21
def handle_loop
  loop do
    need_to_read = connections.select(&:need_to_read?)
    need_to_write = connections.select(&:need_to_write?)

    ready_to_read, ready_to_write, = IO.select(
      need_to_read + [socket],
      need_to_write
    )

    handle_read(ready_to_read)
    handle_write(ready_to_write)
  end
end
handle_read(ready_to_read) click to toggle source
# File lib/ruby_wolf/worker.rb, line 36
def handle_read(ready_to_read)
  ready_to_read.each do |connection|
    if connection == socket
      accept_connection
    else
      connection.read
      handle_request(connection) unless connection.need_to_read?
    end
  end
end
handle_request(connection) click to toggle source
# File lib/ruby_wolf/worker.rb, line 54
def handle_request(connection)
  handler = RubyWolf::Handler.new(app, connection) do |response|
    connection.enqueue_write(response)
  end
  handler.process
end
handle_write(ready_to_write) click to toggle source
# File lib/ruby_wolf/worker.rb, line 47
def handle_write(ready_to_write)
  ready_to_write.each do |connection|
    connection.write
    close_connection(connection) unless connection.need_to_write?
  end
end