Class: Rack::Handler::Falcon

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

Overview

The falcon adaptor for the ‘rackup` executable.

Constant Summary collapse

SCHEME =

The default scheme.

"http"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server, task) ⇒ Falcon

Returns a new instance of Falcon.



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rack/handler/falcon.rb', line 51

def initialize(server, task)
  @server = server
  @task = task

  @notification = Async::IO::Notification.new

  @waiter = @task.async(transient: true) do
    @notification.wait

    @task&.stop
    @task = nil
  end
end

Class Method Details

.endpoint_for(**options) ⇒ Object

Generate an endpoint for the given ‘rackup` options.



20
21
22
23
24
25
# File 'lib/rack/handler/falcon.rb', line 20

def self.endpoint_for(**options)
  host = options[:Host] || 'localhost'
  port = Integer(options[:Port] || 9292)
  
  return Async::IO::Endpoint.tcp(host, port)
end

.run(app, **options) ⇒ Object

Run the specified app using the given options:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rack/handler/falcon.rb', line 29

def self.run(app, **options)
  app = ::Protocol::Rack::Adapter.new(app)
      
  Sync do |task|
    endpoint = endpoint_for(**options)
    server = ::Falcon::Server.new(app, endpoint, protocol: Async::HTTP::Protocol::HTTP1, scheme: SCHEME)

    server_task = task.async do
      server.run.each(&:wait)
    end

    wrapper = self.new(server, task)
    
    yield wrapper if block_given?

    server_task.wait
  ensure
    server_task.stop
    wrapper.close
  end
end

Instance Method Details

#closeObject



69
70
71
72
73
74
75
# File 'lib/rack/handler/falcon.rb', line 69

def close
  @notification&.close
  @notification = nil

  @waiter&.stop
  @waiter = nil
end

#stopObject



65
66
67
# File 'lib/rack/handler/falcon.rb', line 65

def stop
  @notification&.signal
end