Skip to content

erl: add stop_producer #39

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 1 commit into from
Sep 19, 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
20 changes: 15 additions & 5 deletions src/x/hstreamdb-erl-nifs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ rustler::atoms! {

rustler::init!(
"hstreamdb",
[create_stream, start_producer, append],
[create_stream, start_producer, stop_producer, append],
load = load
);

#[derive(Clone)]
pub struct NifAppender(UnboundedSender<Record>);
pub struct NifAppender(UnboundedSender<Option<Record>>);

fn load(env: Env, _: Term) -> bool {
resource!(NifAppender, env);
Expand Down Expand Up @@ -65,7 +65,7 @@ pub fn start_producer(
compression_type: Atom,
flush_settings: Term,
) -> ResourceArc<NifAppender> {
let (request_sender, request_receiver) = unbounded_channel::<Record>();
let (request_sender, request_receiver) = unbounded_channel::<Option<Record>>();
let compression_type = atom_to_compression_type(compression_type);
let flush_settings = new_flush_settings(flush_settings);
let future = async move {
Expand Down Expand Up @@ -93,7 +93,10 @@ pub fn start_producer(
let mut request_receiver = request_receiver;
let mut appender = appender;
while let Some(record) = request_receiver.recv().await {
_ = appender.append(record).unwrap()
match record {
Some(record) => _ = appender.append(record).unwrap(),
None => request_receiver.close(),
}
}
});
producer.start().await
Expand All @@ -102,14 +105,21 @@ pub fn start_producer(
ResourceArc::new(NifAppender(request_sender))
}

#[rustler::nif]
fn stop_producer(producer: ResourceArc<NifAppender>) -> Atom {
let producer = &producer.0;
producer.send(None).unwrap();
ok()
}

#[rustler::nif]
fn append(producer: ResourceArc<NifAppender>, partition_key: String, raw_payload: String) -> Atom {
let record = Record {
partition_key,
payload: hstreamdb::Payload::RawRecord(raw_payload.into_bytes()),
};
let producer = &producer.0;
producer.send(record).unwrap();
producer.send(Some(record)).unwrap();
ok()
}

Expand Down
7 changes: 6 additions & 1 deletion x/hstreamdb_erl/src/hstreamdb.erl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

-on_load(init/0).

-export([create_stream/5, start_producer/4, append/3]).
-export([create_stream/5, start_producer/4, stop_producer/1, append/3]).

-export_type([producer/0, compression_type/0]).

Expand Down Expand Up @@ -36,6 +36,11 @@ create_stream(ServerUrl, StreamName, ReplicationFactor, BacklogDuration, ShardCo
start_producer(ServerUrl, StreamName, CompressionType, FlushSettings) ->
none.

-spec stop_producer(
Producer :: producer()
) -> ok.
stop_producer(Producer) -> none.

-spec append(Producer :: producer(), PartitionKey :: binary(), RawPayload :: binary()) ->
ok.
append(Producer, PartitionKey, RawPayload) ->
Expand Down