Module: Async::HTTP::Protocol::HTTP2::Connection

Included in:
Client, Server
Defined in:
lib/async/http/protocol/http2/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



125
126
127
# File 'lib/async/http/protocol/http2/connection.rb', line 125

def count
  @count
end

#promisesObject (readonly)

Returns the value of attribute promises.



119
120
121
# File 'lib/async/http/protocol/http2/connection.rb', line 119

def promises
  @promises
end

#streamObject (readonly)

Returns the value of attribute stream.



58
59
60
# File 'lib/async/http/protocol/http2/connection.rb', line 58

def stream
  @stream
end

Instance Method Details

#close(error = nil) ⇒ Object



72
73
74
75
76
# File 'lib/async/http/protocol/http2/connection.rb', line 72

def close(error = nil)
  @reader = nil
  
  super
end

#concurrencyObject



127
128
129
# File 'lib/async/http/protocol/http2/connection.rb', line 127

def concurrency
  self.maximum_concurrent_streams
end

#http1?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/async/http/protocol/http2/connection.rb', line 60

def http1?
  false
end

#http2?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/async/http/protocol/http2/connection.rb', line 64

def http2?
  true
end

#initializeObject



44
45
46
47
48
49
50
51
52
# File 'lib/async/http/protocol/http2/connection.rb', line 44

def initialize(*)
  super
  
  @count = 0
  @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

#peerObject



121
122
123
# File 'lib/async/http/protocol/http2/connection.rb', line 121

def peer
  @stream.io
end

#read_in_background(parent: Task.current) ⇒ Object

Raises:

  • (RuntimeError)


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/async/http/protocol/http2/connection.rb', line 95

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}.")
    
    begin
      while !self.closed?
        self.consume_window
        self.read_frame
      end
    rescue SocketError, IOError, EOFError, Errno::ECONNRESET, Errno::EPIPE, Async::Wrapper::Cancelled
      # Ignore.
    ensure
      # Don't call #close twice.
      if @reader
        self.close($!)
      end
    end
  end
end

#reusable?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/async/http/protocol/http2/connection.rb', line 136

def reusable?
  !self.closed?
end

#start_connectionObject



68
69
70
# File 'lib/async/http/protocol/http2/connection.rb', line 68

def start_connection
  @reader || read_in_background
end

#to_sObject



54
55
56
# File 'lib/async/http/protocol/http2/connection.rb', line 54

def to_s
  "\#<#{self.class} #{@streams.count} active streams>"
end

#versionObject



140
141
142
# File 'lib/async/http/protocol/http2/connection.rb', line 140

def version
  VERSION
end

#viable?Boolean

Can we use this connection to make requests?

Returns:

  • (Boolean)


132
133
134
# File 'lib/async/http/protocol/http2/connection.rb', line 132

def viable?
  @stream.connected?
end

#write_frame(frame) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/async/http/protocol/http2/connection.rb', line 78

def write_frame(frame)
  # We don't want to write multiple frames at the same time.
  @write_frame_guard.acquire do
    super
  end
  
  @stream.flush
end

#write_frames(&block) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/async/http/protocol/http2/connection.rb', line 87

def write_frames(&block)
  @write_frame_guard.acquire do
    super
  end
  
  @stream.flush
end