Class: Async::HTTP::Protocol::HTTP2::Server

Inherits:
Protocol::HTTP2::Server
  • Object
show all
Includes:
Connection
Defined in:
lib/async/http/protocol/http2/server.rb

Instance Attribute Summary collapse

Attributes included from Connection

#count, #promises, #stream

Instance Method Summary collapse

Methods included from Connection

#as_json, #concurrency, #http1?, #http2?, #peer, #read_in_background, #reusable?, #start_connection, #synchronize, #to_json, #to_s, #version, #viable?

Constructor Details

#initialize(stream) ⇒ Server

Returns a new instance of Server.



18
19
20
21
22
23
24
25
26
27
# File 'lib/async/http/protocol/http2/server.rb', line 18

def initialize(stream)
	# Used by some generic methods in Connetion:
	@stream = stream
	
	framer = ::Protocol::HTTP2::Framer.new(stream)
	
	super(framer)
	
	@requests = Async::Queue.new
end

Instance Attribute Details

#requestsObject (readonly)

Returns the value of attribute requests.



29
30
31
# File 'lib/async/http/protocol/http2/server.rb', line 29

def requests
  @requests
end

Instance Method Details

#accept_stream(stream_id) ⇒ Object



31
32
33
34
35
# File 'lib/async/http/protocol/http2/server.rb', line 31

def accept_stream(stream_id)
	super do
		Request::Stream.create(self, stream_id)
	end
end

#close(error = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/async/http/protocol/http2/server.rb', line 37

def close(error = nil)
	if @requests
		# Stop the request loop:
		@requests.enqueue(nil)
		@requests = nil
	end
	
	super
end

#each(task: Task.current) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/async/http/protocol/http2/server.rb', line 47

def each(task: Task.current)
	task.annotate("Reading #{version} requests for #{self.class}.")
	
	# It's possible the connection has died before we get here...
	@requests&.async do |task, request|
		task.annotate("Incoming request: #{request.method} #{request.path.inspect}.")
		
		@count += 1
		
		task.defer_stop do
			response = yield(request)
		rescue
			# We need to close the stream if the user code blows up while generating a response:
			request.stream.send_reset_stream(::Protocol::HTTP2::INTERNAL_ERROR)
			
			raise
		else
			request.send_response(response)
		end
	end
	
	# Maybe we should add some synchronisation here - i.e. only exit once all requests are finished.
end