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

Merge periodic flush into connection handler loop #687

Merged
merged 1 commit into from
Nov 15, 2022
Merged
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
25 changes: 12 additions & 13 deletions async-nats/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,11 @@ impl ConnectionHandler {
pub(crate) async fn process(
&mut self,
ping_period: Duration,
flush_period: Duration,
mut receiver: mpsc::Receiver<Command>,
) -> Result<(), io::Error> {
let mut ping_interval = interval(ping_period);
let mut flush_interval = interval(flush_period);

loop {
select! {
Expand All @@ -329,6 +331,11 @@ impl ConnectionHandler {

self.connection.flush().await?;
},
_ = flush_interval.tick().fuse() => {
if let Err(_err) = self.connection.flush().await {
self.handle_disconnect().await?;
}
},
maybe_command = receiver.recv().fuse() => {
match maybe_command {
Some(command) => if let Err(err) = self.handle_command(command).await {
Expand Down Expand Up @@ -637,7 +644,7 @@ pub async fn connect_with_options<A: ToServerAddrs>(
options: ConnectOptions,
) -> Result<Client, io::Error> {
let ping_period = options.ping_interval;
let flush_interval = options.flush_interval;
let flush_period = options.flush_interval;

let (events_tx, mut events_rx) = mpsc::channel(128);
let (state_tx, state_rx) = tokio::sync::watch::channel(State::Pending);
Expand Down Expand Up @@ -673,22 +680,12 @@ pub async fn connect_with_options<A: ToServerAddrs>(
let client = Client::new(
info_watcher,
state_rx,
sender.clone(),
sender,
options.subscription_capacity,
options.inbox_prefix,
options.request_timeout,
);

tokio::spawn(async move {
loop {
tokio::time::sleep(flush_interval).await;
match sender.send(Command::TryFlush).await {
Ok(()) => {}
Err(_) => return,
}
}
});

task::spawn(async move {
while let Some(event) = events_rx.recv().await {
options.event_callback.call(event).await
Expand All @@ -703,7 +700,9 @@ pub async fn connect_with_options<A: ToServerAddrs>(
}
let connection = connection.unwrap();
let mut connection_handler = ConnectionHandler::new(connection, connector, info_sender);
connection_handler.process(ping_period, receiver).await
connection_handler
.process(ping_period, flush_period, receiver)
.await
});

Ok(client)
Expand Down