Module: Async::HTTP::Protocol::HTTP2::Connection
Overview
Provides shared connection behaviour for HTTP/2 client and server connections.
Instance Attribute Summary collapse
-
#count ⇒ Object
readonly
Returns the value of attribute count.
-
#promises ⇒ Object
readonly
Returns the value of attribute promises.
-
#stream ⇒ Object
readonly
Returns the value of attribute stream.
Instance Method Summary collapse
- #as_json ⇒ Object
-
#close(error = nil) ⇒ Object
Close the connection and stop the background reader.
- #concurrency ⇒ Object
- #http1? ⇒ Boolean
- #http2? ⇒ Boolean
-
#initialize ⇒ Object
Initialize the connection state.
- #peer ⇒ Object
-
#read_in_background(parent: Task.current) ⇒ Object
Start a transient background task that reads frames from the connection.
- #reusable? ⇒ Boolean
-
#start_connection ⇒ Object
Start the background reader task if it is not already running.
-
#synchronize(&block) ⇒ Object
Synchronize write access to the connection.
- #to_json ⇒ Object
- #to_s ⇒ Object
- #version ⇒ Object
-
#viable? ⇒ Boolean
Can we use this connection to make requests?.
-
#write_frame(frame) ⇒ Object
Write a single frame, deferring stop until the frame is written.
-
#write_frames ⇒ Object
Write multiple frames, deferring stop until the frames are written.
Instance Attribute Details
#count ⇒ Object (readonly)
Returns the value of attribute count.
136 137 138 |
# File 'lib/async/http/protocol/http2/connection.rb', line 136 def count @count end |
#promises ⇒ Object (readonly)
Returns the value of attribute promises.
129 130 131 |
# File 'lib/async/http/protocol/http2/connection.rb', line 129 def promises @promises end |
#stream ⇒ Object (readonly)
Returns the value of attribute stream.
72 73 74 |
# File 'lib/async/http/protocol/http2/connection.rb', line 72 def stream @stream end |
Instance Method Details
#as_json ⇒ Object
63 64 65 |
# File 'lib/async/http/protocol/http2/connection.rb', line 63 def as_json(...) to_s end |
#close(error = nil) ⇒ Object
Close the connection and stop the background reader.
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/async/http/protocol/http2/connection.rb', line 90 def close(error = nil) # Ensure the reader task is stopped. if @reader reader = @reader @reader = nil reader.stop end super end |
#concurrency ⇒ Object
139 140 141 |
# File 'lib/async/http/protocol/http2/connection.rb', line 139 def concurrency self.maximum_concurrent_streams end |
#http1? ⇒ Boolean
75 76 77 |
# File 'lib/async/http/protocol/http2/connection.rb', line 75 def http1? false end |
#http2? ⇒ Boolean
80 81 82 |
# File 'lib/async/http/protocol/http2/connection.rb', line 80 def http2? true end |
#initialize ⇒ Object
Initialize the connection state.
32 33 34 35 36 37 38 39 |
# File 'lib/async/http/protocol/http2/connection.rb', line 32 def initialize(...) super @reader = nil # Writing multiple frames at the same time can cause odd problems if frames are only partially written. So we use a semaphore to ensure frames are written in their entirety. @write_frame_guard = Async::Semaphore.new(1) end |
#peer ⇒ Object
132 133 134 |
# File 'lib/async/http/protocol/http2/connection.rb', line 132 def peer @peer ||= ::Protocol::HTTP::Peer.for(@stream.io) end |
#read_in_background(parent: Task.current) ⇒ Object
Start a transient background task that reads frames from the connection.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/async/http/protocol/http2/connection.rb', line 102 def read_in_background(parent: Task.current) raise RuntimeError, "Connection is closed!" if closed? parent.async(transient: true) do |task| @reader = task task.annotate("#{version} reading data for #{self.class}.") # We don't need to defer stop here as this is already a transient task (ignores stop): begin while !self.closed? self.consume_window self.read_frame end rescue => error # Close with error. ensure # Don't call #close twice. if @reader @reader = nil self.close(error) end end end end |
#reusable? ⇒ Boolean
149 150 151 |
# File 'lib/async/http/protocol/http2/connection.rb', line 149 def reusable? !self.closed? end |
#start_connection ⇒ Object
Start the background reader task if it is not already running.
85 86 87 |
# File 'lib/async/http/protocol/http2/connection.rb', line 85 def start_connection @reader || read_in_background end |
#synchronize(&block) ⇒ Object
Synchronize write access to the connection.
43 44 45 |
# File 'lib/async/http/protocol/http2/connection.rb', line 43 def synchronize(&block) @write_frame_guard.acquire(&block) end |
#to_json ⇒ Object
68 69 70 |
# File 'lib/async/http/protocol/http2/connection.rb', line 68 def to_json(...) as_json.to_json(...) end |
#to_s ⇒ Object
58 59 60 |
# File 'lib/async/http/protocol/http2/connection.rb', line 58 def to_s "\#<#{self.class} #{@streams.count} active streams>" end |
#version ⇒ Object
154 155 156 |
# File 'lib/async/http/protocol/http2/connection.rb', line 154 def version VERSION end |
#viable? ⇒ Boolean
Can we use this connection to make requests?
144 145 146 |
# File 'lib/async/http/protocol/http2/connection.rb', line 144 def viable? @stream&.readable? end |
#write_frame(frame) ⇒ Object
Write a single frame, deferring stop until the frame is written.
48 49 50 |
# File 'lib/async/http/protocol/http2/connection.rb', line 48 def write_frame(frame) Task.current.defer_stop{super} end |
#write_frames ⇒ Object
Write multiple frames, deferring stop until the frames are written.
53 54 55 |
# File 'lib/async/http/protocol/http2/connection.rb', line 53 def write_frames Task.current.defer_stop{super} end |