From 06bc375cca85aeb298327b0363d36352b73b0865 Mon Sep 17 00:00:00 2001 From: Tyler Neely Date: Wed, 19 May 2021 14:48:39 +0200 Subject: [PATCH 1/2] Idempotent UNSUB avoids sending multiple UNSUB messages when subscriptions are dropped or manually unsubscribed. --- CHANGELOG.md | 7 +++++++ Cargo.toml | 2 +- async-nats/Cargo.toml | 4 ++-- src/client.rs | 10 +++++++++- 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 54c837910..70ca6761c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ ## Improvements +- #180 idempotent unsubscription avoids sending + multiple UNSUB messages. + +# 0.9.16 + +## Improvements + - #178 client state has been reorganized to allow reading and writing to make progress independently, preventing issues that were sometimes encountered diff --git a/Cargo.toml b/Cargo.toml index 8ecc17b21..a384d1d07 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "nats" -version = "0.9.16" +version = "0.9.17" description = "A Rust NATS client" authors = ["Derek Collison ", "Tyler Neely ", "Stjepan Glavina "] edition = "2018" diff --git a/async-nats/Cargo.toml b/async-nats/Cargo.toml index aa68d9542..bf71aeafa 100644 --- a/async-nats/Cargo.toml +++ b/async-nats/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "async-nats" -version = "0.9.16" +version = "0.9.17" description = "An async Rust NATS client" authors = ["Derek Collison ", "Tyler Neely ", "Stjepan Glavina "] edition = "2018" @@ -17,7 +17,7 @@ maintenance = { status = "actively-developed" } [dependencies] blocking = "1.0.2" -nats = { path = "..", version = "0.9.16" } +nats = { path = "..", version = "0.9.17" } [dev-dependencies] smol = "1.2.5" diff --git a/src/client.rs b/src/client.rs index 533ae7575..a692ed346 100644 --- a/src/client.rs +++ b/src/client.rs @@ -348,7 +348,15 @@ impl Client { let mut read = self.state.read.lock(); // Remove the subscription from the map. - read.subscriptions.remove(&sid); + if read.subscriptions.remove(&sid).is_none() { + // already unsubscribed + + // NB see locking protocol for state.write and state.read + drop(read); + drop(write); + + return Ok(()); + } // Send an UNSUB message. if let Some(writer) = write.writer.as_mut() { From a4596177b94533a821f065ce21a2633aee62a4c8 Mon Sep 17 00:00:00 2001 From: Tyler Neely Date: Wed, 19 May 2021 14:49:24 +0200 Subject: [PATCH 2/2] Fix CHANGELOG --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 70ca6761c..173c65f27 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -# 0.9.16 +# 0.9.17 ## Improvements