@@ -195,12 +195,12 @@ defmodule Mint.HTTP2 do
195195 # matching WINDOW_UPDATE to bring the server's view from the spec
196196 # default of 65_535 up to the advertised peak.
197197 receive_window_size: @ default_window_size ,
198- # `receive_window ` is the server's current view of our receive
198+ # `receive_window_remaining ` is the server's current view of our receive
199199 # window — decremented by DATA frame sizes as they arrive, bumped
200200 # back up to `receive_window_size` whenever we send a
201201 # WINDOW_UPDATE. When it drops to `receive_window_update_threshold`, we
202202 # refill it back to the peak in one frame.
203- receive_window : @ default_window_size ,
203+ receive_window_remaining : @ default_window_size ,
204204 # Minimum remaining receive window before we send a WINDOW_UPDATE.
205205 # Configurable via the `:receive_window_update_threshold` connect option.
206206 receive_window_update_threshold: @ default_receive_window_update_threshold ,
@@ -912,7 +912,7 @@ defmodule Mint.HTTP2 do
912912 def set_window_size ( % __MODULE__ { } = conn , :connection , new_size ) do
913913 do_set_window_size ( conn , 0 , conn . receive_window_size , new_size , fn conn , size ->
914914 conn = put_in ( conn . receive_window_size , size )
915- put_in ( conn . receive_window , size )
915+ put_in ( conn . receive_window_remaining , size )
916916 end )
917917 catch
918918 :throw , { :mint , conn , error } -> { :error , conn , error }
@@ -925,7 +925,7 @@ defmodule Mint.HTTP2 do
925925
926926 do_set_window_size ( conn , stream_id , current , new_size , fn conn , size ->
927927 conn = put_in ( conn . streams [ stream_id ] . receive_window_size , size )
928- put_in ( conn . streams [ stream_id ] . receive_window , size )
928+ put_in ( conn . streams [ stream_id ] . receive_window_remaining , size )
929929 end )
930930
931931 :error ->
@@ -935,7 +935,8 @@ defmodule Mint.HTTP2 do
935935 :throw , { :mint , conn , error } -> { :error , conn , error }
936936 end
937937
938- defp do_set_window_size ( conn , _stream_id , current , new_size , _update ) when new_size == current do
938+ defp do_set_window_size ( conn , _stream_id , current , new_size , _update )
939+ when new_size == current do
939940 { :ok , conn }
940941 end
941942
@@ -1121,9 +1122,19 @@ defmodule Mint.HTTP2 do
11211122 scheme_string = Atom . to_string ( scheme )
11221123 mode = Keyword . get ( opts , :mode , :active )
11231124 log? = Keyword . get ( opts , :log , false )
1124- connection_window_size = Keyword . get ( opts , :connection_window_size , @ default_connection_window_size )
1125+
1126+ connection_window_size =
1127+ Keyword . get ( opts , :connection_window_size , @ default_connection_window_size )
1128+
11251129 validate_window_size! ( :connection_window_size , connection_window_size )
1126- receive_window_update_threshold = Keyword . get ( opts , :receive_window_update_threshold , @ default_receive_window_update_threshold )
1130+
1131+ receive_window_update_threshold =
1132+ Keyword . get (
1133+ opts ,
1134+ :receive_window_update_threshold ,
1135+ @ default_receive_window_update_threshold
1136+ )
1137+
11271138 validate_receive_window_update_threshold! ( receive_window_update_threshold )
11281139 client_settings_params = Keyword . get ( opts , :client_settings , [ ] )
11291140
@@ -1160,10 +1171,21 @@ defmodule Mint.HTTP2 do
11601171 state: :handshaking ,
11611172 log: log? ,
11621173 receive_window_size: connection_window_size ,
1163- receive_window : connection_window_size ,
1174+ receive_window_remaining : connection_window_size ,
11641175 receive_window_update_threshold: receive_window_update_threshold
11651176 }
11661177
1178+ # Mirror the advertised client settings into `conn.client_settings` up
1179+ # front. Streams opened before the server's SETTINGS ACK arrives read
1180+ # their initial receive window from this map; without this, they would
1181+ # track the library default instead of the value we actually sent in
1182+ # the SETTINGS frame, and stream-level WINDOW_UPDATEs would never fire
1183+ # when the advertised window is smaller than the default.
1184+ conn =
1185+ update_in ( conn . client_settings , fn settings ->
1186+ Enum . into ( client_settings_params , settings )
1187+ end )
1188+
11671189 preface = build_preface ( client_settings_params , connection_window_size )
11681190
11691191 with :ok <- Util . inet_opts ( transport , socket ) ,
@@ -1286,7 +1308,7 @@ defmodule Mint.HTTP2 do
12861308 receive_window_size: conn . client_settings . initial_window_size ,
12871309 # Current remaining receive window for this stream, tracked
12881310 # independently from the peak so that refills can be batched.
1289- receive_window : conn . client_settings . initial_window_size ,
1311+ receive_window_remaining : conn . client_settings . initial_window_size ,
12901312 received_first_headers?: false
12911313 }
12921314
@@ -1417,10 +1439,14 @@ defmodule Mint.HTTP2 do
14171439
14181440 cond do
14191441 data_size > stream . send_window_size ->
1420- throw ( { :mint , conn , wrap_error ( { :exceeds_window_size , :request , stream . send_window_size } ) } )
1442+ throw (
1443+ { :mint , conn , wrap_error ( { :exceeds_window_size , :request , stream . send_window_size } ) }
1444+ )
14211445
14221446 data_size > conn . send_window_size ->
1423- throw ( { :mint , conn , wrap_error ( { :exceeds_window_size , :connection , conn . send_window_size } ) } )
1447+ throw (
1448+ { :mint , conn , wrap_error ( { :exceeds_window_size , :connection , conn . send_window_size } ) }
1449+ )
14241450
14251451 # If the data size is greater than the max frame size, we chunk automatically based
14261452 # on the max frame size.
@@ -1790,12 +1816,12 @@ defmodule Mint.HTTP2 do
17901816 # roughly one update per `(receive_window_size - threshold)` bytes
17911817 # consumed.
17921818 defp refill_client_windows ( conn , stream_id , data_size ) do
1793- conn = update_in ( conn . receive_window , & ( & 1 - data_size ) )
1819+ conn = update_in ( conn . receive_window_remaining , & ( & 1 - data_size ) )
17941820
17951821 conn =
17961822 case Map . fetch ( conn . streams , stream_id ) do
17971823 { :ok , _stream } ->
1798- update_in ( conn . streams [ stream_id ] . receive_window , & ( & 1 - data_size ) )
1824+ update_in ( conn . streams [ stream_id ] . receive_window_remaining , & ( & 1 - data_size ) )
17991825
18001826 :error ->
18011827 conn
@@ -1815,8 +1841,8 @@ defmodule Mint.HTTP2 do
18151841 end
18161842
18171843 defp maybe_refill_conn ( frames , conn ) do
1818- if conn . receive_window <= conn . receive_window_update_threshold do
1819- increment = conn . receive_window_size - conn . receive_window
1844+ if conn . receive_window_remaining <= conn . receive_window_update_threshold do
1845+ increment = conn . receive_window_size - conn . receive_window_remaining
18201846 [ window_update ( stream_id: 0 , window_size_increment: increment ) | frames ]
18211847 else
18221848 frames
@@ -1826,8 +1852,8 @@ defmodule Mint.HTTP2 do
18261852 defp maybe_refill_stream ( frames , conn , stream_id ) do
18271853 case Map . fetch ( conn . streams , stream_id ) do
18281854 { :ok , stream } ->
1829- if stream . receive_window <= conn . receive_window_update_threshold do
1830- increment = stream . receive_window_size - stream . receive_window
1855+ if stream . receive_window_remaining <= conn . receive_window_update_threshold do
1856+ increment = stream . receive_window_size - stream . receive_window_remaining
18311857
18321858 [
18331859 window_update ( stream_id: stream_id , window_size_increment: increment ) | frames
@@ -1844,11 +1870,11 @@ defmodule Mint.HTTP2 do
18441870 defp apply_refills ( conn , frames ) do
18451871 Enum . reduce ( frames , conn , fn
18461872 window_update ( stream_id: 0 ) , conn ->
1847- put_in ( conn . receive_window , conn . receive_window_size )
1873+ put_in ( conn . receive_window_remaining , conn . receive_window_size )
18481874
18491875 window_update ( stream_id: stream_id ) , conn ->
18501876 put_in (
1851- conn . streams [ stream_id ] . receive_window ,
1877+ conn . streams [ stream_id ] . receive_window_remaining ,
18521878 conn . streams [ stream_id ] . receive_window_size
18531879 )
18541880 end )
@@ -2199,7 +2225,7 @@ defmodule Mint.HTTP2 do
21992225 state: :reserved_remote ,
22002226 send_window_size: conn . server_settings . initial_window_size ,
22012227 receive_window_size: conn . client_settings . initial_window_size ,
2202- receive_window : conn . client_settings . initial_window_size ,
2228+ receive_window_remaining : conn . client_settings . initial_window_size ,
22032229 received_first_headers?: false
22042230 }
22052231
0 commit comments