Skip to content

Commit 8a56b35

Browse files
committed
Better window update synchronization.
1 parent b9e1b10 commit 8a56b35

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

lib/async/http/protocol/http2/output.rb

+16-6
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ def initialize(stream, body, trailer = nil)
1717

1818
@task = nil
1919

20-
@window_updated = Async::Condition.new
20+
@guard = ::Mutex.new
21+
@window_updated = ::ConditionVariable.new
2122
end
2223

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

3536
def window_updated(size)
36-
@window_updated.signal
37+
@guard.synchronize do
38+
@window_updated.signal
39+
end
3740
end
3841

3942
def write(chunk)
4043
until chunk.empty?
4144
maximum_size = @stream.available_frame_size
4245

43-
while maximum_size <= 0
44-
@window_updated.wait
45-
46-
maximum_size = @stream.available_frame_size
46+
# We try to avoid synchronization if possible:
47+
if maximum_size <= 0
48+
@guard.synchronize do
49+
maximum_size = @stream.available_frame_size
50+
51+
while maximum_size <= 0
52+
@window_updated.wait(@guard)
53+
54+
maximum_size = @stream.available_frame_size
55+
end
56+
end
4757
end
4858

4959
break unless chunk = send_data(chunk, maximum_size)

0 commit comments

Comments
 (0)