Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Respect limits with read session resumption #139

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ use crate::{
},
ServiceRequest, ServiceStreamingResponse, Streaming,
},
types::{self, MIB_BYTES},
types::{self, MeteredBytes, MIB_BYTES},
};

const DEFAULT_CONNECTOR: Option<HttpConnector> = None;
Expand Down Expand Up @@ -854,8 +854,15 @@ fn read_resumption_stream(
backoff = None;
}
if let Ok(types::ReadOutput::Batch(types::SequencedRecordBatch { records })) = &item {
let req = request.req_mut();
if let Some(record) = records.last() {
request.set_start_seq_num(record.seq_num + 1);
req.start_seq_num = record.seq_num + 1;
}
if let Some(count) = req.limit.count.as_mut() {
*count = count.saturating_sub(records.len() as u64);
}
if let Some(bytes) = req.limit.bytes.as_mut() {
*bytes = bytes.saturating_sub(records.metered_bytes());
}
}
yield item;
Expand Down
4 changes: 2 additions & 2 deletions src/service/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ impl ReadSessionServiceRequest {
}
}

pub fn set_start_seq_num(&mut self, start_seq_num: u64) {
self.req.start_seq_num = start_seq_num;
pub(crate) fn req_mut(&mut self) -> &mut types::ReadSessionRequest {
&mut self.req
}
}

Expand Down
43 changes: 16 additions & 27 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1378,7 +1378,7 @@ pub struct ReadLimit {
#[derive(Debug, Clone, Default)]
pub struct ReadRequest {
pub start_seq_num: u64,
pub limit: Option<ReadLimit>,
pub limit: ReadLimit,
}

impl ReadRequest {
Expand All @@ -1392,10 +1392,7 @@ impl ReadRequest {

/// Overwrite limit.
pub fn with_limit(self, limit: ReadLimit) -> Self {
Self {
limit: Some(limit),
..self
}
Self { limit, ..self }
}
}

Expand All @@ -1409,26 +1406,21 @@ impl ReadRequest {
limit,
} = self;

let limit: Option<api::ReadLimit> = match limit {
None => None,
Some(limit) => Some({
if limit.count > Some(1000) {
Err("read limit: count must not exceed 1000 for unary request")
} else if limit.bytes > Some(MIB_BYTES) {
Err("read limit: bytes must not exceed 1MiB for unary request")
} else {
Ok(api::ReadLimit {
count: limit.count,
bytes: limit.bytes,
})
}
}?),
};
let limit = if limit.count > Some(1000) {
Err("read limit: count must not exceed 1000 for unary request")
} else if limit.bytes > Some(MIB_BYTES) {
Err("read limit: bytes must not exceed 1MiB for unary request")
} else {
Ok(api::ReadLimit {
count: limit.count,
bytes: limit.bytes,
})
}?;

Ok(api::ReadRequest {
stream: stream.into(),
start_seq_num,
limit,
limit: Some(limit),
})
}
}
Expand Down Expand Up @@ -1549,7 +1541,7 @@ impl TryFrom<api::ReadResponse> for ReadOutput {
#[derive(Debug, Clone, Default)]
pub struct ReadSessionRequest {
pub start_seq_num: u64,
pub limit: Option<ReadLimit>,
pub limit: ReadLimit,
}

impl ReadSessionRequest {
Expand All @@ -1563,10 +1555,7 @@ impl ReadSessionRequest {

/// Overwrite limit.
pub fn with_limit(self, limit: ReadLimit) -> Self {
Self {
limit: Some(limit),
..self
}
Self { limit, ..self }
}

pub(crate) fn into_api_type(self, stream: impl Into<String>) -> api::ReadSessionRequest {
Expand All @@ -1577,7 +1566,7 @@ impl ReadSessionRequest {
api::ReadSessionRequest {
stream: stream.into(),
start_seq_num,
limit: limit.map(|limit| api::ReadLimit {
limit: Some(api::ReadLimit {
count: limit.count,
bytes: limit.bytes,
}),
Expand Down
Loading