Class: Falcon::Rackup::Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/falcon/rackup/handler.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) ⇒ Handler

Returns a new instance of Handler.



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/falcon/rackup/handler.rb', line 54

def initialize(server, task)
  @server = server
  @task = task
  
  @notification = Thread::Queue.new
  
  @waiter = @task.async(transient: true) do
    @notification.pop
    
    @task&.stop
    @task = nil
  end
end

Class Method Details

.endpoint_for(**options) ⇒ Object

Generate an endpoint for the given ‘rackup` options.



25
26
27
28
29
30
# File 'lib/falcon/rackup/handler.rb', line 25

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

.run(app, **options) ⇒ Object

Run the specified app using the given options:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/falcon/rackup/handler.rb', line 34

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 = server.run
    
    wrapper = self.new(server, task)
    
    yield wrapper if block_given?
    
    server_task.wait
  ensure
    server_task.stop
    wrapper.close
  end
end

.to_sObject

The name of the handler.



19
20
21
# File 'lib/falcon/rackup/handler.rb', line 19

def self.to_s
  "Falcon v#{Falcon::VERSION}"
end

Instance Method Details

#closeObject



72
73
74
75
76
77
78
# File 'lib/falcon/rackup/handler.rb', line 72

def close
  @notification&.close
  @notification = nil
  
  @waiter&.stop
  @waiter = nil
end

#stopObject



68
69
70
# File 'lib/falcon/rackup/handler.rb', line 68

def stop
  @notification&.push(true)
end