Class: Rack::Handler::Libevent

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/handler/libevent.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options) ⇒ Libevent



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rack/handler/libevent.rb', line 19

def initialize(app, options)
  @app = app
  
  options[:Host] or raise ArgumentError, "Host option required"
  options[:Port] or raise ArgumentError, "Port option required"

  @host = options[:Host]
  @port = options[:Port].to_i

  @base = ::Libevent::Base.new
  @http = ::Libevent::Http.new(@base)

  @http.set_timeout(options[:timeout].to_i) if options[:timeout]
end

Class Method Details

.run(app, options) ⇒ Object



8
9
10
11
# File 'lib/rack/handler/libevent.rb', line 8

def self.run(app, options)
  server = new(app, options)
  server.start
end

.valid_optionsObject



13
14
15
16
17
# File 'lib/rack/handler/libevent.rb', line 13

def self.valid_options
  {
    "timeout=TIMEOUT" => "Set the timeout for an HTTP request"
  }
end

Instance Method Details

#startObject



34
35
36
37
38
39
40
41
42
# File 'lib/rack/handler/libevent.rb', line 34

def start
  @http.bind_socket(@host, @port) or raise RuntimeError, "Can't bind to #{@host}:#{@port}"
  @http.set_request_handler(self.method(:process))

  @base.trap_signal("INT")  { self.stop }
  @base.trap_signal("TERM") { self.stop }

  @base.dispatch
end

#stopObject



44
45
46
# File 'lib/rack/handler/libevent.rb', line 44

def stop
  @base.exit_loop
end