Skip to content

Commit

Permalink
Better window update synchronization.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Oct 2, 2024
1 parent b9e1b10 commit 8a56b35
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions lib/async/http/protocol/http2/output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ def initialize(stream, body, trailer = nil)

@task = nil

@window_updated = Async::Condition.new
@guard = ::Mutex.new
@window_updated = ::ConditionVariable.new
end

attr :trailer
Expand All @@ -33,17 +34,26 @@ def start(parent: Task.current)
end

def window_updated(size)
@window_updated.signal
@guard.synchronize do
@window_updated.signal
end
end

def write(chunk)
until chunk.empty?
maximum_size = @stream.available_frame_size

while maximum_size <= 0
@window_updated.wait

maximum_size = @stream.available_frame_size
# We try to avoid synchronization if possible:
if maximum_size <= 0
@guard.synchronize do
maximum_size = @stream.available_frame_size

while maximum_size <= 0
@window_updated.wait(@guard)

maximum_size = @stream.available_frame_size
end
end
end

break unless chunk = send_data(chunk, maximum_size)
Expand Down

0 comments on commit 8a56b35

Please sign in to comment.