Skip to content

Commit

Permalink
cleanup closed streams to reclaim memory
Browse files Browse the repository at this point in the history
Background: #73

We store a reference to recently closed streams and periodically purge
them whenever some other stream is closed and 15s time interval has
passed.
  • Loading branch information
igrigorik committed Dec 4, 2016
1 parent c11d93a commit 0aeaac2
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion lib/http/2/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def initialize(**settings)

@active_stream_count = 0
@streams = {}
@streams_recently_closed = {}
@pending_settings = []

@framer = Framer.new
Expand Down Expand Up @@ -282,6 +283,12 @@ def receive(data)
parent = @streams[frame[:stream]]
pid = frame[:promise_stream]

# if PUSH parent is recently closed, RST_STREAM the push
if @streams_recently_closed[frame[:stream]]
send(type: :rst_stream, stream: pid, error: :refused_stream)
return
end

connection_error(msg: 'missing parent ID') if parent.nil?

unless parent.state == :open || parent.state == :half_closed_local
Expand Down Expand Up @@ -634,7 +641,18 @@ def activate_stream(id: nil, **args)
# states count toward the maximum number of streams that an endpoint is
# permitted to open.
stream.once(:active) { @active_stream_count += 1 }
stream.once(:close) { @active_stream_count -= 1 }
stream.once(:close) do
@streams.delete id
@active_stream_count -= 1

# Store a reference to the closed stream, such that we can respond
# to any in-flight frames while close is registered on both sides.
# References to such streams will be purged whenever another stream
# is closed, with a minimum of 15s RTT time window.
@streams_recently_closed.delete_if { |_, v| (Time.now - v) > 15 }
@streams_recently_closed[id] = Time.now
end

stream.on(:promise, &method(:promise)) if self.is_a? Server
stream.on(:frame, &method(:send))
stream.on(:window_update, &method(:window_update))
Expand Down

0 comments on commit 0aeaac2

Please sign in to comment.