Skip to content

Commit 508bcb1

Browse files
authored
Store buffered data size as usize (fixes #269) (#542)
1 parent 465f033 commit 508bcb1

File tree

4 files changed

+29
-43
lines changed

4 files changed

+29
-43
lines changed

src/proto/streams/flow_control.rs

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ impl FlowControl {
173173
);
174174

175175
// Ensure that the argument is correct
176-
assert!(sz <= self.window_size);
176+
assert!(self.window_size >= sz as usize);
177177

178178
// Update values
179179
self.window_size -= sz;
@@ -206,38 +206,22 @@ impl Window {
206206
}
207207
}
208208

209-
impl PartialEq<WindowSize> for Window {
210-
fn eq(&self, other: &WindowSize) -> bool {
209+
impl PartialEq<usize> for Window {
210+
fn eq(&self, other: &usize) -> bool {
211211
if self.0 < 0 {
212212
false
213213
} else {
214-
(self.0 as WindowSize).eq(other)
214+
(self.0 as usize).eq(other)
215215
}
216216
}
217217
}
218218

219-
impl PartialEq<Window> for WindowSize {
220-
fn eq(&self, other: &Window) -> bool {
221-
other.eq(self)
222-
}
223-
}
224-
225-
impl PartialOrd<WindowSize> for Window {
226-
fn partial_cmp(&self, other: &WindowSize) -> Option<::std::cmp::Ordering> {
219+
impl PartialOrd<usize> for Window {
220+
fn partial_cmp(&self, other: &usize) -> Option<::std::cmp::Ordering> {
227221
if self.0 < 0 {
228222
Some(::std::cmp::Ordering::Less)
229223
} else {
230-
(self.0 as WindowSize).partial_cmp(other)
231-
}
232-
}
233-
}
234-
235-
impl PartialOrd<Window> for WindowSize {
236-
fn partial_cmp(&self, other: &Window) -> Option<::std::cmp::Ordering> {
237-
if other.0 < 0 {
238-
Some(::std::cmp::Ordering::Greater)
239-
} else {
240-
self.partial_cmp(&(other.0 as WindowSize))
224+
(self.0 as usize).partial_cmp(other)
241225
}
242226
}
243227
}

src/proto/streams/prioritize.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ impl Prioritize {
158158
}
159159

160160
// Update the buffered data counter
161-
stream.buffered_send_data += sz;
161+
stream.buffered_send_data += sz as usize;
162162

163163
let span =
164164
tracing::trace_span!("send_data", sz, requested = stream.requested_send_capacity);
@@ -167,9 +167,10 @@ impl Prioritize {
167167

168168
// Implicitly request more send capacity if not enough has been
169169
// requested yet.
170-
if stream.requested_send_capacity < stream.buffered_send_data {
170+
if (stream.requested_send_capacity as usize) < stream.buffered_send_data {
171171
// Update the target requested capacity
172-
stream.requested_send_capacity = stream.buffered_send_data;
172+
stream.requested_send_capacity =
173+
cmp::min(stream.buffered_send_data, WindowSize::MAX as usize) as WindowSize;
173174

174175
self.try_assign_capacity(stream);
175176
}
@@ -217,28 +218,28 @@ impl Prioritize {
217218
"reserve_capacity",
218219
?stream.id,
219220
requested = capacity,
220-
effective = capacity + stream.buffered_send_data,
221+
effective = (capacity as usize) + stream.buffered_send_data,
221222
curr = stream.requested_send_capacity
222223
);
223224
let _e = span.enter();
224225

225226
// Actual capacity is `capacity` + the current amount of buffered data.
226227
// If it were less, then we could never send out the buffered data.
227-
let capacity = capacity + stream.buffered_send_data;
228+
let capacity = (capacity as usize) + stream.buffered_send_data;
228229

229-
if capacity == stream.requested_send_capacity {
230+
if capacity == stream.requested_send_capacity as usize {
230231
// Nothing to do
231-
} else if capacity < stream.requested_send_capacity {
232+
} else if capacity < stream.requested_send_capacity as usize {
232233
// Update the target requested capacity
233-
stream.requested_send_capacity = capacity;
234+
stream.requested_send_capacity = capacity as WindowSize;
234235

235236
// Currently available capacity assigned to the stream
236237
let available = stream.send_flow.available().as_size();
237238

238239
// If the stream has more assigned capacity than requested, reclaim
239240
// some for the connection
240-
if available > capacity {
241-
let diff = available - capacity;
241+
if available as usize > capacity {
242+
let diff = available - capacity as WindowSize;
242243

243244
stream.send_flow.claim_capacity(diff);
244245

@@ -252,7 +253,8 @@ impl Prioritize {
252253
}
253254

254255
// Update the target requested capacity
255-
stream.requested_send_capacity = capacity;
256+
stream.requested_send_capacity =
257+
cmp::min(capacity, WindowSize::MAX as usize) as WindowSize;
256258

257259
// Try to assign additional capacity to the stream. If none is
258260
// currently available, the stream will be queued to receive some
@@ -316,8 +318,8 @@ impl Prioritize {
316318
/// it to the connection
317319
pub fn reclaim_reserved_capacity(&mut self, stream: &mut store::Ptr, counts: &mut Counts) {
318320
// only reclaim requested capacity that isn't already buffered
319-
if stream.requested_send_capacity > stream.buffered_send_data {
320-
let reserved = stream.requested_send_capacity - stream.buffered_send_data;
321+
if stream.requested_send_capacity as usize > stream.buffered_send_data {
322+
let reserved = stream.requested_send_capacity - stream.buffered_send_data as WindowSize;
321323

322324
stream.send_flow.claim_capacity(reserved);
323325
self.assign_connection_capacity(reserved, stream, counts);
@@ -377,7 +379,7 @@ impl Prioritize {
377379

378380
// Total requested should never go below actual assigned
379381
// (Note: the window size can go lower than assigned)
380-
debug_assert!(total_requested >= stream.send_flow.available());
382+
debug_assert!(stream.send_flow.available() <= total_requested as usize);
381383

382384
// The amount of additional capacity that the stream requests.
383385
// Don't assign more than the window has available!
@@ -435,7 +437,7 @@ impl Prioritize {
435437
has_unavailable = %stream.send_flow.has_unavailable()
436438
);
437439

438-
if stream.send_flow.available() < stream.requested_send_capacity
440+
if stream.send_flow.available() < stream.requested_send_capacity as usize
439441
&& stream.send_flow.has_unavailable()
440442
{
441443
// The stream requires additional capacity and the stream's
@@ -735,8 +737,8 @@ impl Prioritize {
735737
stream.send_flow.send_data(len);
736738

737739
// Decrement the stream's buffered data counter
738-
debug_assert!(stream.buffered_send_data >= len);
739-
stream.buffered_send_data -= len;
740+
debug_assert!(stream.buffered_send_data >= len as usize);
741+
stream.buffered_send_data -= len as usize;
740742
stream.requested_send_capacity -= len;
741743

742744
// Assign the capacity back to the connection that

src/proto/streams/send.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,10 +332,10 @@ impl Send {
332332
let available = stream.send_flow.available().as_size();
333333
let buffered = stream.buffered_send_data;
334334

335-
if available <= buffered {
335+
if available as usize <= buffered {
336336
0
337337
} else {
338-
available - buffered
338+
available - buffered as WindowSize
339339
}
340340
}
341341

src/proto/streams/stream.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub(super) struct Stream {
4545

4646
/// Amount of data buffered at the prioritization layer.
4747
/// TODO: Technically this could be greater than the window size...
48-
pub buffered_send_data: WindowSize,
48+
pub buffered_send_data: usize,
4949

5050
/// Task tracking additional send capacity (i.e. window updates).
5151
send_task: Option<Waker>,

0 commit comments

Comments
 (0)