Skip to content

Commit b6f8e19

Browse files
committed
Separate out state machine and closed callback.
1 parent 08621a0 commit b6f8e19

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

lib/protocol/http1/connection.rb

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ def hijack!
182182
@stream = nil
183183
stream.flush
184184

185-
self.closed!
185+
@state = :hijacked
186+
self.closed
186187

187188
return stream
188189
end
@@ -203,13 +204,20 @@ def close(error = nil)
203204
stream.close
204205
end
205206

206-
self.closed!(error)
207+
unless closed?
208+
@state = :closed
209+
self.closed(error)
210+
end
207211
end
208212

209213
def open!
210-
raise ProtocolError, "Cannot write request in #{@state}!" unless @state == :idle
214+
if @state == :idle
215+
@state = :open
216+
else
217+
raise ProtocolError, "Cannot open connection in state: #{@state}!"
218+
end
211219

212-
@state = :open
220+
return self
213221
end
214222

215223
def write_request(authority, method, path, version, headers)
@@ -371,7 +379,7 @@ def send_end_stream!
371379
if @state == :open
372380
@state = :half_closed_local
373381
elsif @state == :half_closed_remote
374-
self.closed!
382+
self.close!
375383
else
376384
raise ProtocolError, "Cannot send end stream in #{@state}!"
377385
end
@@ -525,18 +533,24 @@ def write_body_and_close(body, head)
525533
self.send_end_stream!
526534
end
527535

536+
# The connection (stream) was closed. It may now be in the idle state.
537+
def closed(error = nil)
538+
end
539+
528540
# Transition to the closed state.
529541
#
530542
# If no error occurred, and the connection is persistent, this will immediately transition to the idle state.
531543
#
532544
# @parameter error [Exxception] the error that caused the connection to close.
533-
def closed!(error = nil)
545+
def close!(error = nil)
534546
if @persistent and !error
535547
# If there was no error, and the connection is persistent, we can reuse it:
536548
@state = :idle
537549
else
538550
@state = :closed
539551
end
552+
553+
self.closed(error)
540554
end
541555

542556
def write_body(version, body, head = false, trailer = nil)
@@ -574,7 +588,7 @@ def receive_end_stream!
574588
if @state == :open
575589
@state = :half_closed_remote
576590
elsif @state == :half_closed_local
577-
self.closed!
591+
self.close!
578592
else
579593
raise ProtocolError, "Cannot receive end stream in #{@state}!"
580594
end

test/protocol/http1/connection.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,7 @@
688688
expect(server).to be(:persistent)
689689
error = Protocol::HTTP1::InvalidRequest.new("Invalid request")
690690

691-
expect(server).to receive(:closed!).with(error)
691+
expect(server).to receive(:closed).with(error)
692692

693693
server.close(error)
694694
expect(server).to be(:closed?)

0 commit comments

Comments
 (0)