Class: Adelnor::ThreadedServer

Inherits:
BaseServer show all
Defined in:
lib/adelnor/threaded_server.rb

Instance Method Summary collapse

Methods inherited from BaseServer

#handle, #rack_data, #read_request_message, run, #welcome_message

Constructor Details

#initialize(rack_app, port, options = {}) ⇒ ThreadedServer

Returns a new instance of ThreadedServer.



7
8
9
10
11
12
# File 'lib/adelnor/threaded_server.rb', line 7

def initialize(rack_app, port, options = {})
  super(rack_app, port, options)

  @thread_queue = Queue.new
  @pool_size = options[:thread_pool]
end

Instance Method Details

#handle_threadObject



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/adelnor/threaded_server.rb', line 26

def handle_thread
  tid = Thread.current.object_id

  puts "[#{tid}] Thread started"
  loop do
    client = @thread_queue.pop

    handle(client)
    client.close
  end
end

#runObject



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/adelnor/threaded_server.rb', line 14

def run
  @pool_size.times do
    Thread.new { handle_thread }
  end

  loop do
    client, = @socket.accept

    @thread_queue.push(client)
  end
end