pub struct AdaptiveChunkSizeStrategy {
pub current_size: u64,
pub max_size: u64,
pub successful_chunks: u32,
pub bytes_uploaded: u64,
pub total_upload_time: Duration,
/* private fields */
}Expand description
Chunk size strategy for adaptive upload.
Normative for capsule-sdk (transfer upload doc, §Adaptive Chunk Sizing): the size is the
one that completes in a target request duration at the goodput
measured over a sliding window — shorter under an adverse link. Because the server cuts a
chunk only when it stalls, never for its total duration, a chunk’s size never has to fit a
deadline: a slow link gets chunks that take the target time at its own rate, a fast one
chunks up to the file-size tier’s ceiling.
It starts from the server’s advised size, clamped to [PROTOCOL_MIN_CHUNK, tier ceiling].
After each chunk it moves towards the target size: down to it at once when the target is
half the current size or less, up by doubling after a warm-up when it is twice or more.
Every candidate is aligned down to 4 KiB and clamped, so the size is aligned and within the
protocol bounds by construction.
A stall — the server’s 408 error.upload.chunk_timeout, or the client’s own stall cut —
re-sends the same offset at half the stalled length (shrink_after_stall), and the size
does not grow again until a run of successes shows the link recovered. A chunk that got no
answer within the response wait is shrunk the same way: its tail may have been too slow to
drain out of the send buffers in time.
Fields§
§current_size: u64Current chunk size.
max_size: u64The file-size tier’s ceiling: no chunk is larger.
successful_chunks: u32Successful chunks measured since the size last changed.
bytes_uploaded: u64Total bytes uploaded.
total_upload_time: DurationTotal time spent uploading.
Implementations§
Source§impl AdaptiveChunkSizeStrategy
impl AdaptiveChunkSizeStrategy
Sourcepub fn for_file_size(total_size: u64) -> Self
pub fn for_file_size(total_size: u64) -> Self
Create a new chunk size strategy based on total file size: the tier names the starting size and the ceiling.
Sourcepub fn shrink_after_stall(&mut self, stalled: u64) -> bool
pub fn shrink_after_stall(&mut self, stalled: u64) -> bool
Shrink after a chunk of stalled bytes stalled — the server answered
408 error.upload.chunk_timeout, or the client cut it: the next chunk, sent at the same
offset, is half that, 4 KiB-aligned down, and at least PROTOCOL_MIN_CHUNK. The size
then holds until a run of chunks succeeds.
Returns false when stalled is already at or below the protocol minimum, so nothing
smaller can be sent; the size is then left as it is.
Sourcepub fn with_connection_class(self, class: ConnectionClass) -> Self
pub fn with_connection_class(self, class: ConnectionClass) -> Self
Couple the chunk size to a detected ConnectionClass: under
ConnectionClass::Adverse each request targets
ADVERSE_TARGET_REQUEST_DURATION, otherwise TARGET_REQUEST_DURATION.
Sourcepub fn apply_connection_class(&mut self, class: ConnectionClass)
pub fn apply_connection_class(&mut self, class: ConnectionClass)
Apply the connection-class coupling in place (see
with_connection_class).
Sourcepub fn target_duration(&self) -> Duration
pub fn target_duration(&self) -> Duration
How long one chunk request should take at the measured goodput.
Sourcepub fn seeded_from_suggested(self, suggested: u64) -> Self
pub fn seeded_from_suggested(self, suggested: u64) -> Self
Seed the starting chunk size from the server’s suggestion: the chunk member of
X-Capsule-Advice, or the create body’s suggested_chunk_size from a server that
sends no advice.
The suggestion is a starting point only (transfer upload doc, §Adaptive Chunk Sizing):
4 KiB-aligned down and clamped into [PROTOCOL_MIN_CHUNK, tier ceiling], so the size
is aligned and within the protocol bounds by construction.
Sourcepub fn throughput_bytes_per_second(&self) -> f64
pub fn throughput_bytes_per_second(&self) -> f64
Goodput in bytes per second over the sliding window.
Sourcepub fn target_size(&self) -> Option<u64>
pub fn target_size(&self) -> Option<u64>
The size that completes in the target duration at the measured goodput, aligned and
clamped; None before anything was measured.
Sourcepub fn record_chunk(&mut self, chunk_size: u64, upload_duration: Duration)
pub fn record_chunk(&mut self, chunk_size: u64, upload_duration: Duration)
Record a successful chunk upload and move the size towards the target.
Sourcepub fn next_chunk_size(&self) -> u64
pub fn next_chunk_size(&self) -> u64
Get the next chunk size to use.
Alignment is a hard guarantee by construction: every candidate is aligned down and clamped by one function, so this can never return an unaligned size — the debug_assert is a tripwire for future edits, not a runtime dependency.
Trait Implementations§
Source§impl Clone for AdaptiveChunkSizeStrategy
impl Clone for AdaptiveChunkSizeStrategy
Source§fn clone(&self) -> AdaptiveChunkSizeStrategy
fn clone(&self) -> AdaptiveChunkSizeStrategy
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for AdaptiveChunkSizeStrategy
impl RefUnwindSafe for AdaptiveChunkSizeStrategy
impl Send for AdaptiveChunkSizeStrategy
impl Sync for AdaptiveChunkSizeStrategy
impl Unpin for AdaptiveChunkSizeStrategy
impl UnsafeUnpin for AdaptiveChunkSizeStrategy
impl UnwindSafe for AdaptiveChunkSizeStrategy
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> CompatExt for T
impl<T> CompatExt for T
§impl<T, UT> HandleAlloc<UT> for T
impl<T, UT> HandleAlloc<UT> for T
§fn new_handle(value: Arc<T>) -> Handle
fn new_handle(value: Arc<T>) -> Handle
§unsafe fn clone_handle(handle: Handle) -> Handle
unsafe fn clone_handle(handle: Handle) -> Handle
§unsafe fn consume_handle(handle: Handle) -> Arc<T> ⓘ
unsafe fn consume_handle(handle: Handle) -> Arc<T> ⓘ
Arc<> Read more§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more