Skip to content

Commit

Permalink
Adds automatic window updates
Browse files Browse the repository at this point in the history
Implements the first two items in the feature list of #27

- [x] add window_update method to both Stream and Connection
- [x] automatic WU after emit(:data)
  • Loading branch information
Tim Emiola committed Aug 10, 2015
1 parent 35df8e0 commit 0047115
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
6 changes: 6 additions & 0 deletions lib/http/2/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ def settings(payload)
@pending_settings << payload
end

# Sends a WINDOW_UPDATE frame
def window_update(increment)
send(type: :window_update, stream: 0, increment: increment)
end

# Decodes incoming bytes into HTTP 2.0 frames and routes them to
# appropriate receivers: connection frames are handled directly, and
# stream frames are passed to appropriate stream objects.
Expand Down Expand Up @@ -626,6 +631,7 @@ def activate_stream(id: nil, **args)
stream.once(:close) { @active_stream_count -= 1 }
stream.on(:promise, &method(:promise)) if self.is_a? Server
stream.on(:frame, &method(:send))
stream.on(:window_update, &method(:window_update))

@streams[id] = stream
end
Expand Down
16 changes: 15 additions & 1 deletion lib/http/2/stream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,14 @@ def receive(frame)

case frame[:type]
when :data
@local_window -= frame[:payload].size
@local_window -= frame[:payload].bytesize
emit(:data, frame[:payload]) unless frame[:ignore]

# Automatically send a window_update with the size of
# the received bytes as the increment
if frame[:payload] && frame[:payload].bytesize > 0
window_update(frame[:payload].bytesize)
end
when :headers, :push_promise
emit(:headers, frame[:payload]) unless frame[:ignore]
when :priority
Expand Down Expand Up @@ -206,6 +212,14 @@ def refuse
send(type: :rst_stream, error: :refused_stream)
end

# Sends a WINDOW_UPDATE frame if the stream is in the correct state
# for doing so
def window_update(increment)
emit(:window_update, increment) # alway allow the connection update
return if @state == :closed || @state == :remote_closed
send(type: :window_update, increment: increment)
end

private

# HTTP 2.0 Stream States
Expand Down

0 comments on commit 0047115

Please sign in to comment.