Skip to content

erl: refine client APIs #80

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

Merged
merged 6 commits into from
Oct 27, 2022
Merged
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
7 changes: 4 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 19 additions & 7 deletions src/hstreamdb/src/consumer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ use crate::utils::decode_received_records;

impl Client {
pub async fn streaming_fetch(
&mut self,
&self,
consumer_name: String,
subscription_id: String,
) -> common::Result<UnboundedReceiverStream<(Payload, AckFn)>> {
) -> common::Result<UnboundedReceiverStream<(Payload, Responder)>> {
let channel = self.lookup_subscription(subscription_id.clone()).await?;
let mut channel = HStreamApiClient::connect(channel).await?;

Expand All @@ -36,7 +36,7 @@ impl Client {
.send(request)
.map_err(common::Error::StreamingFetchInitError)?;

let (sender, receiver) = tokio::sync::mpsc::unbounded_channel::<(Payload, AckFn)>();
let (sender, receiver) = tokio::sync::mpsc::unbounded_channel::<(Payload, Responder)>();

_ = tokio::spawn(fetching(
consumer_name,
Expand All @@ -55,7 +55,7 @@ async fn fetching(
subscription_id: String,
ack_sender: UnboundedSender<StreamingFetchRequest>,
mut init_response: Streaming<StreamingFetchResponse>,
fetch_stream: UnboundedSender<(Payload, AckFn)>,
fetch_stream: UnboundedSender<(Payload, Responder)>,
) {
loop {
match init_response.message().await {
Expand All @@ -79,14 +79,26 @@ async fn fetching(
}
}

pub type AckFn = Box<dyn FnOnce() -> Result<(), SendError<StreamingFetchRequest>> + Send>;
type AckFn = Box<dyn FnOnce() -> Result<(), SendError<StreamingFetchRequest>> + Send>;

pub struct Responder(AckFn);

impl Responder {
pub fn ack(self) -> Result<(), SendError<StreamingFetchRequest>> {
self.0()
}

fn new(ack_fn: AckFn) -> Self {
Responder(ack_fn)
}
}

fn process_streaming_fetch_response(
consumer_name: String,
subscription_id: String,
message: StreamingFetchResponse,
ack_sender: UnboundedSender<StreamingFetchRequest>,
fetch_stream: UnboundedSender<(Payload, AckFn)>,
fetch_stream: UnboundedSender<(Payload, Responder)>,
) {
match message.received_records {
None => {
Expand Down Expand Up @@ -130,7 +142,7 @@ fn process_streaming_fetch_response(
ack_ids: vec![record_id],
})
});
match fetch_stream.send((record, ack_fn)) {
match fetch_stream.send((record, Responder::new(ack_fn))) {
Ok(()) => (),
Err(err) => {
log::error!("send to fetch stream error: {err}")
Expand Down
1 change: 1 addition & 0 deletions src/hstreamdb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub mod reader;
pub mod utils;

pub use channel_provider::ChannelProviderSettings;
pub use client::Client;
pub use common::{
CompressionType, Error, Payload, Record, RecordId, Result, ShardId, SpecialOffset, Stream,
StreamShardOffset, Subscription,
Expand Down
6 changes: 3 additions & 3 deletions src/hstreamdb/tests/consumer_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ async fn test_consumer() {
env_logger::init();

let addr = env::var("TEST_SERVER_ADDR").unwrap();
let mut client = Client::new(
let client = Client::new(
addr,
ChannelProviderSettings::builder()
.set_concurrency_limit(8)
Expand Down Expand Up @@ -105,10 +105,10 @@ async fn test_consumer() {
.unwrap();

let mut records = Vec::new();
while let Some((record, ack)) = stream.next().await {
while let Some((record, responder)) = stream.next().await {
println!("{record:?}");
records.push(record);
ack().unwrap();
responder.ack().unwrap();
if records.len() == 10 * 100 {
println!("done");
break;
Expand Down
4 changes: 3 additions & 1 deletion src/x/hstreamdb-erl-nifs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ env_logger = "0.9.0"

flume = "0.10.14"
once_cell = "1.14.0"
parking_lot = "0.12.1"
rustler = "0.26.0"
tokio = { version = "1.21.0", features = ["rt-multi-thread", "parking_lot"] }
tokio-stream = "0.1.11"

prost = "0.11.0"
Loading