-
Notifications
You must be signed in to change notification settings - Fork 286
feat: return token when request is made, resolve token when request handling completes #916
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
Open
de-sh
wants to merge
31
commits into
main
Choose a base branch
from
ack-notify
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,576
−500
Open
Changes from 23 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
dcabfdb
feat: acknowledge notification
bc8de88
fix: don't panic if promise dropped
176fe5c
fix: don't remove slots, just take contents
c4ce2f7
fix: direct resolve for QoS 0
b6c5ed3
fix: validate and notify sub/unsub acks
ab96189
fix: forget acked packet
656cf74
refactor
4a936fe
fix: panic if `max_inflight == u16::MAX`
d508595
feat: notify acks in v5 also
6b0c3ec
doc: add changelog
f43ea21
fix: bug observed in https://github.com/bytebeamio/rumqtt/pull/916#is…
2632ab1
doc: add examples of ack notify
8ad2223
feat: return pkid of ack
db7c322
doc: update example with pkids
31887ca
feat: return reason of request failure
066783a
rm unnecessary example
10d843c
test: reliability of ack promises
15ffccf
fix: rm dup import
b664fd6
test: run on unique port
b6d447d
doc: code comments
23e4d9a
fix: don't expose waiters outside crate
979cb8f
feat: non-blocking `try_resolve`
79d5ff8
doc: comment on `blocking_wait`
165d169
refactor: condense and simplify examples
bfeb44d
test: working of sub/unsub promises
a0e678b
feat: tokens for all requests (#921)
d67e919
fix: imports
swanandx 8be8afe
fix: clippy lint
swanandx 919c981
fix: token interfaces (#946)
de-sh c7ec9b4
fix: clear waiting subacks and unsubacks state
swanandx 2f4def7
Merge branch 'main' into ack-notify
swanandx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
use tokio::task::{self, JoinSet}; | ||
|
||
use rumqttc::{AsyncClient, MqttOptions, QoS}; | ||
use std::error::Error; | ||
use std::time::Duration; | ||
|
||
#[tokio::main(flavor = "current_thread")] | ||
async fn main() -> Result<(), Box<dyn Error>> { | ||
pretty_env_logger::init(); | ||
// color_backtrace::install(); | ||
|
||
let mut mqttoptions = MqttOptions::new("test-1", "localhost", 1883); | ||
mqttoptions.set_keep_alive(Duration::from_secs(5)); | ||
|
||
let (client, mut eventloop) = AsyncClient::new(mqttoptions, 10); | ||
task::spawn(async move { | ||
loop { | ||
let event = eventloop.poll().await; | ||
match &event { | ||
Ok(v) => { | ||
println!("Event = {v:?}"); | ||
} | ||
Err(e) => { | ||
println!("Error = {e:?}"); | ||
} | ||
} | ||
} | ||
}); | ||
|
||
// Subscribe and wait for broker acknowledgement | ||
match client | ||
.subscribe("hello/world", QoS::AtMostOnce) | ||
.await | ||
.unwrap() | ||
.await | ||
{ | ||
Ok(pkid) => println!("Acknowledged Sub({pkid})"), | ||
Err(e) => println!("Subscription failed: {e:?}"), | ||
} | ||
|
||
// Publish at all QoS levels and wait for broker acknowledgement | ||
match client | ||
.publish("hello/world", QoS::AtMostOnce, false, vec![1; 1]) | ||
.await | ||
.unwrap() | ||
.await | ||
{ | ||
Ok(pkid) => println!("Acknowledged Pub({pkid})"), | ||
Err(e) => println!("Publish failed: {e:?}"), | ||
} | ||
|
||
match client | ||
.publish("hello/world", QoS::AtLeastOnce, false, vec![1; 2]) | ||
.await | ||
.unwrap() | ||
.await | ||
{ | ||
Ok(pkid) => println!("Acknowledged Pub({pkid})"), | ||
Err(e) => println!("Publish failed: {e:?}"), | ||
} | ||
|
||
match client | ||
.publish("hello/world", QoS::ExactlyOnce, false, vec![1; 3]) | ||
.await | ||
.unwrap() | ||
.await | ||
{ | ||
Ok(pkid) => println!("Acknowledged Pub({pkid})"), | ||
Err(e) => println!("Publish failed: {e:?}"), | ||
} | ||
|
||
// Publish and spawn wait for notification | ||
let mut set = JoinSet::new(); | ||
|
||
let future = client | ||
.publish("hello/world", QoS::AtMostOnce, false, vec![1; 1]) | ||
.await | ||
.unwrap(); | ||
set.spawn(async { future.await }); | ||
|
||
let future = client | ||
.publish("hello/world", QoS::AtLeastOnce, false, vec![1; 2]) | ||
.await | ||
.unwrap(); | ||
set.spawn(async { future.await }); | ||
|
||
let future = client | ||
.publish("hello/world", QoS::ExactlyOnce, false, vec![1; 3]) | ||
.await | ||
.unwrap(); | ||
set.spawn(async { future.await }); | ||
|
||
while let Some(Ok(res)) = set.join_next().await { | ||
match res { | ||
Ok(pkid) => println!("Acknowledged Pub({pkid})"), | ||
Err(e) => println!("Publish failed: {e:?}"), | ||
} | ||
} | ||
|
||
// Unsubscribe and wait for broker acknowledgement | ||
match client.unsubscribe("hello/world").await.unwrap().await { | ||
Ok(pkid) => println!("Acknowledged Unsub({pkid})"), | ||
Err(e) => println!("Unsubscription failed: {e:?}"), | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
use flume::bounded; | ||
use rumqttc::{Client, MqttOptions, PromiseError, QoS}; | ||
use std::error::Error; | ||
use std::thread::{self, sleep}; | ||
use std::time::Duration; | ||
|
||
fn main() -> Result<(), Box<dyn Error>> { | ||
pretty_env_logger::init(); | ||
// color_backtrace::install(); | ||
|
||
let mut mqttoptions = MqttOptions::new("test-1", "localhost", 1883); | ||
mqttoptions.set_keep_alive(Duration::from_secs(5)); | ||
|
||
let (client, mut conn) = Client::new(mqttoptions, 10); | ||
thread::spawn(move || { | ||
for event in conn.iter() { | ||
match &event { | ||
Ok(v) => { | ||
println!("Event = {v:?}"); | ||
} | ||
Err(e) => { | ||
println!("Error = {e:?}"); | ||
} | ||
} | ||
} | ||
}); | ||
|
||
// Subscribe and wait for broker acknowledgement | ||
match client | ||
.subscribe("hello/world", QoS::AtMostOnce) | ||
.unwrap() | ||
.blocking_wait() | ||
{ | ||
Ok(pkid) => println!("Acknowledged Sub({pkid})"), | ||
Err(e) => println!("Subscription failed: {e:?}"), | ||
} | ||
|
||
// Publish at all QoS levels and wait for broker acknowledgement | ||
match client | ||
.publish("hello/world", QoS::AtMostOnce, false, vec![1; 1]) | ||
.unwrap() | ||
.blocking_wait() | ||
{ | ||
Ok(pkid) => println!("Acknowledged Pub({pkid})"), | ||
Err(e) => println!("Publish failed: {e:?}"), | ||
} | ||
|
||
match client | ||
.publish("hello/world", QoS::AtLeastOnce, false, vec![1; 2]) | ||
.unwrap() | ||
.try_resolve() | ||
{ | ||
Ok(pkid) => println!("Acknowledged Pub({pkid})"), | ||
Err(e) => println!("Publish failed: {e:?}"), | ||
} | ||
|
||
match client | ||
.publish("hello/world", QoS::ExactlyOnce, false, vec![1; 3]) | ||
.unwrap() | ||
.blocking_wait() | ||
{ | ||
Ok(pkid) => println!("Acknowledged Pub({pkid})"), | ||
Err(e) => println!("Publish failed: {e:?}"), | ||
} | ||
|
||
// Spawn threads for each publish, use channel to notify result | ||
let (tx, rx) = bounded(1); | ||
|
||
let future = client | ||
.publish("hello/world", QoS::AtMostOnce, false, vec![1; 1]) | ||
.unwrap(); | ||
let tx_clone = tx.clone(); | ||
thread::spawn(move || { | ||
let res = future.blocking_wait(); | ||
tx_clone.send(res).unwrap() | ||
}); | ||
|
||
let future = client | ||
.publish("hello/world", QoS::AtLeastOnce, false, vec![1; 2]) | ||
.unwrap(); | ||
let tx_clone = tx.clone(); | ||
thread::spawn(move || { | ||
let res = future.blocking_wait(); | ||
tx_clone.send(res).unwrap() | ||
}); | ||
|
||
let mut future = client | ||
.publish("hello/world", QoS::ExactlyOnce, false, vec![1; 3]) | ||
.unwrap(); | ||
thread::spawn(move || loop { | ||
match future.try_resolve() { | ||
Err(PromiseError::Waiting) => { | ||
println!("Promise yet to resolve, retrying"); | ||
sleep(Duration::from_secs(1)); | ||
} | ||
res => { | ||
tx.send(res).unwrap(); | ||
break; | ||
} | ||
} | ||
}); | ||
|
||
while let Ok(res) = rx.recv() { | ||
match res { | ||
Ok(pkid) => println!("Acknowledged Pub({pkid})"), | ||
Err(e) => println!("Publish failed: {e:?}"), | ||
} | ||
} | ||
|
||
// Unsubscribe and wait for broker acknowledgement | ||
match client.unsubscribe("hello/world").unwrap().blocking_wait() { | ||
Ok(pkid) => println!("Acknowledged Unsub({pkid})"), | ||
Err(e) => println!("Unsubscription failed: {e:?}"), | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
use tokio::task::{self, JoinSet}; | ||
|
||
use rumqttc::v5::{mqttbytes::QoS, AsyncClient, MqttOptions}; | ||
use std::error::Error; | ||
use std::time::Duration; | ||
|
||
#[tokio::main(flavor = "current_thread")] | ||
async fn main() -> Result<(), Box<dyn Error>> { | ||
pretty_env_logger::init(); | ||
// color_backtrace::install(); | ||
|
||
let mut mqttoptions = MqttOptions::new("test-1", "localhost", 1883); | ||
mqttoptions.set_keep_alive(Duration::from_secs(5)); | ||
|
||
let (client, mut eventloop) = AsyncClient::new(mqttoptions, 10); | ||
task::spawn(async move { | ||
loop { | ||
let event = eventloop.poll().await; | ||
match &event { | ||
Ok(v) => { | ||
println!("Event = {v:?}"); | ||
} | ||
Err(e) => { | ||
println!("Error = {e:?}"); | ||
} | ||
} | ||
} | ||
}); | ||
|
||
// Subscribe and wait for broker acknowledgement | ||
match client | ||
.subscribe("hello/world", QoS::AtMostOnce) | ||
.await | ||
.unwrap() | ||
.await | ||
{ | ||
Ok(pkid) => println!("Acknowledged Sub({pkid})"), | ||
Err(e) => println!("Subscription failed: {e:?}"), | ||
} | ||
|
||
// Publish at all QoS levels and wait for broker acknowledgement | ||
match client | ||
.publish("hello/world", QoS::AtMostOnce, false, vec![1; 1]) | ||
.await | ||
.unwrap() | ||
.await | ||
{ | ||
Ok(pkid) => println!("Acknowledged Pub({pkid})"), | ||
Err(e) => println!("Publish failed: {e:?}"), | ||
} | ||
|
||
match client | ||
.publish("hello/world", QoS::AtLeastOnce, false, vec![1; 2]) | ||
.await | ||
.unwrap() | ||
.await | ||
{ | ||
Ok(pkid) => println!("Acknowledged Pub({pkid})"), | ||
Err(e) => println!("Publish failed: {e:?}"), | ||
} | ||
|
||
match client | ||
.publish("hello/world", QoS::ExactlyOnce, false, vec![1; 3]) | ||
.await | ||
.unwrap() | ||
.await | ||
{ | ||
Ok(pkid) => println!("Acknowledged Pub({pkid})"), | ||
Err(e) => println!("Publish failed: {e:?}"), | ||
} | ||
|
||
// Publish and spawn wait for notification | ||
let mut set = JoinSet::new(); | ||
|
||
let future = client | ||
.publish("hello/world", QoS::AtMostOnce, false, vec![1; 1]) | ||
.await | ||
.unwrap(); | ||
set.spawn(async { future.await }); | ||
|
||
let future = client | ||
.publish("hello/world", QoS::AtLeastOnce, false, vec![1; 2]) | ||
.await | ||
.unwrap(); | ||
set.spawn(async { future.await }); | ||
|
||
let future = client | ||
.publish("hello/world", QoS::ExactlyOnce, false, vec![1; 3]) | ||
.await | ||
.unwrap(); | ||
set.spawn(async { future.await }); | ||
|
||
while let Some(Ok(res)) = set.join_next().await { | ||
match res { | ||
Ok(pkid) => println!("Acknowledged Pub({pkid})"), | ||
Err(e) => println!("Publish failed: {e:?}"), | ||
} | ||
} | ||
|
||
// Unsubscribe and wait for broker acknowledgement | ||
match client.unsubscribe("hello/world").await.unwrap().await { | ||
Ok(pkid) => println!("Acknowledged Unsub({pkid})"), | ||
Err(e) => println!("Unsubscription failed: {e:?}"), | ||
} | ||
|
||
Ok(()) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.