Skip to content
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
17 changes: 8 additions & 9 deletions src/network/http_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1138,9 +1138,8 @@ pub struct HubEvent {
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct EventsResponse {
pub events: Vec<HubEvent>,
// TODO: What's the best way to support next page token with multiple shards?
Copy link
Copy Markdown
Contributor Author

@PirosB3 PirosB3 Nov 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually a legacy comment because next page token is already supported on other paginated endpoints, even across shards

// #[serde(rename = "nextPageToken", skip_serializing_if = "Option::is_none")]
// pub next_page_token: Option<String>,
#[serde(rename = "nextPageToken", skip_serializing_if = "Option::is_none")]
pub next_page_token: Option<String>,
}

#[allow(non_snake_case)]
Expand Down Expand Up @@ -1973,12 +1972,9 @@ fn map_proto_messages_response_to_json_paged_response(
.iter()
.map(|m| map_proto_message_to_json_message(m.clone()).unwrap())
.collect(),
next_page_token: Some(
messages_response
.next_page_token
.map(|t| BASE64_STANDARD.encode(t))
.unwrap_or_else(|| "".to_string()),
),
next_page_token: messages_response
.next_page_token
.map(|t| BASE64_STANDARD.encode(t)),
})
}

Expand Down Expand Up @@ -2958,6 +2954,9 @@ where
.iter()
.map(|e| map_proto_hub_event_to_json_hub_event(e.clone()).unwrap())
.collect(),
next_page_token: events_response
.next_page_token
.map(|t| BASE64_STANDARD.encode(t)),
})
}

Expand Down
44 changes: 26 additions & 18 deletions src/network/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,20 @@ impl MyHubService {
}
}

/// Helper function to serialize page tokens only if at least one shard has more data.
/// Returns None if all shards have finished pagination (all tokens are None).
fn serialize_page_token_if_not_empty(
next_page_tokens: Vec<Option<Vec<u8>>>,
) -> Result<Option<Vec<u8>>, Status> {
if next_page_tokens.iter().any(|token| token.is_some()) {
let new_page_token = serde_json::to_vec(&next_page_tokens)
.map_err(|e| Status::internal(format!("Failed to serialize next_page_token: {}", e)))?;
Ok(Some(new_page_token))
} else {
Ok(None)
}
}

#[tonic::async_trait]
impl HubService for MyHubService {
async fn submit_message(
Expand Down Expand Up @@ -1219,11 +1233,11 @@ impl HubService for MyHubService {
pages.iter().flat_map(|page| page.events.clone()).collect();
let next_page_tokens: Vec<Option<Vec<u8>>> =
pages.into_iter().map(|page| page.next_page_token).collect();
let new_page_token = serde_json::to_vec(&next_page_tokens)
.map_err(|e| Status::internal(format!("Failed to serialize next_page_token: {}", e)))?;
let next_page_token = serialize_page_token_if_not_empty(next_page_tokens)?;

let response = EventsResponse {
events: combined_events,
next_page_token: Some(new_page_token),
next_page_token,
};

Ok(Response::new(response))
Expand Down Expand Up @@ -1552,11 +1566,11 @@ impl HubService for MyHubService {
.collect();
let next_page_tokens: Vec<Option<Vec<u8>>> =
pages.into_iter().map(|page| page.next_page_token).collect();
let new_page_token = serde_json::to_vec(&next_page_tokens)
.map_err(|e| Status::internal(format!("Failed to serialize next_page_token: {}", e)))?;
let next_page_token = serialize_page_token_if_not_empty(next_page_tokens)?;

let response = MessagesResponse {
messages: combined_messages,
next_page_token: Some(new_page_token),
next_page_token,
};

Ok(Response::new(response))
Expand Down Expand Up @@ -1611,13 +1625,11 @@ impl HubService for MyHubService {

let next_page_tokens: Vec<Option<Vec<u8>>> =
pages.into_iter().map(|page| page.next_page_token).collect();

let new_page_token = serde_json::to_vec(&next_page_tokens)
.map_err(|e| Status::internal(format!("Failed to serialize next_page_token: {}", e)))?;
let next_page_token = serialize_page_token_if_not_empty(next_page_tokens)?;

let response = MessagesResponse {
messages: combined_messages,
next_page_token: Some(new_page_token),
next_page_token,
};

Ok(Response::new(response))
Expand Down Expand Up @@ -1689,13 +1701,11 @@ impl HubService for MyHubService {

let next_page_tokens: Vec<Option<Vec<u8>>> =
pages.into_iter().map(|page| page.next_page_token).collect();

let new_page_token = serde_json::to_vec(&next_page_tokens)
.map_err(|e| Status::internal(format!("Failed to serialize next_page_token: {}", e)))?;
let next_page_token = serialize_page_token_if_not_empty(next_page_tokens)?;

let response = MessagesResponse {
messages: combined_messages,
next_page_token: Some(new_page_token),
next_page_token,
};

Ok(Response::new(response))
Expand Down Expand Up @@ -2198,13 +2208,11 @@ impl HubService for MyHubService {

let next_page_tokens: Vec<Option<Vec<u8>>> =
pages.into_iter().map(|page| page.next_page_token).collect();

let new_page_token = serde_json::to_vec(&next_page_tokens)
.map_err(|e| Status::internal(format!("Failed to serialize next_page_token: {}", e)))?;
let next_page_token = serialize_page_token_if_not_empty(next_page_tokens)?;

let response = MessagesResponse {
messages: combined_messages,
next_page_token: Some(new_page_token),
next_page_token,
};

Ok(Response::new(response))
Expand Down
Loading