Skip to content

Commit f819e0b

Browse files
committed
Add methods for handling state transitions.
1 parent 2fdcbc2 commit f819e0b

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

lib/protocol/http2/stream.rb

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,6 @@ def parent=(stream)
124124
@dependency.parent = stream.dependency
125125
end
126126

127-
# The stream is being closed because the connection is being closed.
128-
def close(error = nil)
129-
end
130-
131127
def maximum_frame_size
132128
@connection.available_frame_size
133129
end
@@ -144,6 +140,15 @@ def closed?
144140
@state == :closed
145141
end
146142

143+
# Transition directly to closed state. Do not pass go, do not collect $200.
144+
# This method should only be used by `Connection#close`.
145+
def close(error = nil)
146+
unless closed?
147+
@state = :closed
148+
self.closed(error)
149+
end
150+
end
151+
147152
def send_headers?
148153
@state == :idle or @state == :reserved_local or @state == :open or @state == :half_closed_remote
149154
end
@@ -228,16 +233,26 @@ def send_data(*arguments, **options)
228233
end
229234
end
230235

236+
# The stream has been opened.
237+
def opened(error = nil)
238+
end
239+
231240
def open!
232241
if @state == :idle
233242
@state = :open
234243
else
235244
raise ProtocolError, "Cannot open stream in state: #{@state}"
236245
end
237246

247+
self.opened
248+
238249
return self
239250
end
240251

252+
# The stream has been closed. If closed due to a stream reset, the error will be set.
253+
def closed(error = nil)
254+
end
255+
241256
# Transition the stream into the closed state.
242257
# @param error_code [Integer] the error code if the stream was closed due to a stream reset.
243258
def close!(error_code = nil)
@@ -248,7 +263,7 @@ def close!(error_code = nil)
248263
error = StreamError.new("Stream closed!", error_code)
249264
end
250265

251-
self.close(error)
266+
self.closed(error)
252267

253268
return self
254269
end

0 commit comments

Comments
 (0)