Class: Zapp::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/zapp/server.rb

Overview

The Zap HTTP Server, listens on a TCP connection and processes incoming requests

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeServer

Returns a new instance of Server.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/zapp/server.rb', line 8

def initialize
  # Ensure config.ru file is loaded just once by main ractor
  Zapp.config.app

  @socket_pipe = Zapp::Pipe.for(TCPSocket)
  @context_pipe = Zapp::Pipe.for(Zapp::HTTPContext::Context, Symbol)

  @socket_pipe_receiver = Zapp::SocketPipe::Receiver.new(pipe: @socket_pipe)

  @worker_pool = Zapp::WorkerPool.new(socket_pipe: @socket_pipe, context_pipe: @context_pipe)
end

Instance Attribute Details

#socket_pipe_receiverObject (readonly)

Returns the value of attribute socket_pipe_receiver.



6
7
8
# File 'lib/zapp/server.rb', line 6

def socket_pipe_receiver
  @socket_pipe_receiver
end

#worker_poolObject (readonly)

Returns the value of attribute worker_pool.



6
7
8
# File 'lib/zapp/server.rb', line 6

def worker_pool
  @worker_pool
end

Instance Method Details

#runObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/zapp/server.rb', line 20

def run
  log_start

  loop do
    socket = socket_pipe_receiver.take

    next if socket.eof?

    parsing_thread_pool.post do
      ctx = Zapp::HTTPContext::Context.new(socket: socket)

      worker_pool.process(context: ctx) unless ctx.client_closed? # Parsing failed
    end
  rescue Errno::ECONNRESET
    next
  end
rescue SignalException, IRB::Abort => e
  shutdown(e)
end

#shutdown(err = nil) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/zapp/server.rb', line 40

def shutdown(err = nil)
  Zapp::Logger.info("Received signal #{err.class.name}") unless err.nil?
  Zapp::Logger.info("Gracefully shutting down workers, allowing request processing to finish")

  worker_pool.drain

  Zapp::Logger.info("Done. See you next time!")
  Zapp::Logger.flush
end