Skip to content

Commit 39d8a15

Browse files
fix: outdated Client::server_info after reconnect
1 parent 8a85b9b commit 39d8a15

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

watermelon/src/client/mod.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
use std::net::{IpAddr, Ipv4Addr};
33
use std::{fmt::Write, num::NonZero, process::abort, sync::Arc, time::Duration};
44

5-
use arc_swap::ArcSwap;
5+
use arc_swap::ArcSwapOption;
66
use bytes::Bytes;
77
use tokio::{
88
select,
@@ -77,7 +77,7 @@ pub struct Client {
7777
#[derive(Debug)]
7878
struct ClientInner {
7979
sender: mpsc::Sender<HandlerCommand>,
80-
info: Arc<ArcSwap<ServerInfo>>,
80+
info: Arc<ArcSwapOption<ServerInfo>>,
8181
quick_info: Arc<RawQuickInfo>,
8282
multiplexed_subscription_prefix: Subject,
8383
next_subscription_id: AtomicU64,
@@ -199,7 +199,7 @@ impl Client {
199199
let builder = Self::builder();
200200
let (sender, receiver) = mpsc::channel(client_to_handler_chan_size);
201201
let (shutdown_sender, _shutdown_receiver) = mpsc::channel(1);
202-
let info = Arc::new(ArcSwap::new(Arc::from(ServerInfo {
202+
let info = Arc::new(ArcSwapOption::new(Some(Arc::new(ServerInfo {
203203
id: "1234".to_owned(),
204204
name: "watermelon-test".to_owned(),
205205
version: "2.10.17".to_owned(),
@@ -226,7 +226,7 @@ impl Client {
226226
domain: None,
227227

228228
non_standard: NonStandardServerInfo::default(),
229-
})));
229+
}))));
230230
let quick_info = Arc::new(RawQuickInfo::new());
231231
let multiplexed_subscription_prefix = create_inbox_subject(&builder.inbox_prefix);
232232

@@ -418,8 +418,12 @@ impl Client {
418418
/// Consider calling [`Client::quick_info`] if you only need
419419
/// information about Lame Duck Mode.
420420
#[must_use]
421+
#[expect(
422+
clippy::missing_panics_doc,
423+
reason = "we don't expect the panic to ever happen"
424+
)]
421425
pub fn server_info(&self) -> Arc<ServerInfo> {
422-
self.inner.info.load_full()
426+
self.inner.info.load_full().expect("never connected")
423427
}
424428

425429
/// Get information about the client

watermelon/src/client/tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{collections::BTreeSet, sync::Arc};
22

3-
use arc_swap::ArcSwap;
3+
use arc_swap::ArcSwapOption;
44
use tokio::sync::mpsc;
55
use watermelon_proto::{ServerInfo, Subject};
66

@@ -12,7 +12,7 @@ use crate::{
1212
#[derive(Debug)]
1313
pub(crate) struct TestHandler {
1414
pub(crate) receiver: mpsc::Receiver<HandlerCommand>,
15-
pub(crate) _info: Arc<ArcSwap<ServerInfo>>,
15+
pub(crate) _info: Arc<ArcSwapOption<ServerInfo>>,
1616
pub(crate) quick_info: Arc<RawQuickInfo>,
1717
}
1818

watermelon/src/handler/mod.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::{
99
task::{Context, Poll},
1010
};
1111

12-
use arc_swap::ArcSwap;
12+
use arc_swap::ArcSwapOption;
1313
use bytes::Bytes;
1414
use thiserror::Error;
1515
use tokio::{
@@ -52,7 +52,7 @@ pub(crate) struct Handler {
5252
ConnectionCompression<ConnectionSecurity<TcpStream>>,
5353
ConnectionSecurity<TcpStream>,
5454
>,
55-
info: Arc<ArcSwap<ServerInfo>>,
55+
info: Arc<ArcSwapOption<ServerInfo>>,
5656
quick_info: Arc<RawQuickInfo>,
5757
delayed_write: Delayed,
5858
writing: bool,
@@ -75,6 +75,7 @@ pub(crate) struct Handler {
7575
#[derive(Debug)]
7676
pub(crate) struct RecycledHandler {
7777
commands: mpsc::Receiver<HandlerCommand>,
78+
info: Arc<ArcSwapOption<ServerInfo>>,
7879
quick_info: Arc<RawQuickInfo>,
7980

8081
multiplexed_subscription_prefix: Subject,
@@ -202,9 +203,10 @@ impl Handler {
202203

203204
let delayed_write = Delayed::new(builder.write_delay);
204205

206+
recycle.info.store(Some(Arc::from(info)));
205207
Ok(Some(Self {
206208
conn,
207-
info: Arc::new(ArcSwap::new(Arc::from(info))),
209+
info: recycle.info,
208210
quick_info: recycle.quick_info,
209211
delayed_write,
210212
writing: false,
@@ -227,14 +229,15 @@ impl Handler {
227229

228230
RecycledHandler {
229231
commands: self.commands,
232+
info: self.info,
230233
quick_info: self.quick_info,
231234
subscriptions: self.subscriptions,
232235
multiplexed_subscription_prefix: self.multiplexed_subscription_prefix,
233236
shutdown_recv: self.shutdown_recv,
234237
}
235238
}
236239

237-
pub(crate) fn info(&self) -> &Arc<ArcSwap<ServerInfo>> {
240+
pub(crate) fn info(&self) -> &Arc<ArcSwapOption<ServerInfo>> {
238241
&self.info
239242
}
240243

@@ -341,7 +344,7 @@ impl Handler {
341344
}
342345
ServerOp::Info { info } => {
343346
self.quick_info.store_is_lameduck(info.lame_duck_mode);
344-
self.info.store(Arc::from(info));
347+
self.info.store(Some(Arc::from(info)));
345348
}
346349
}
347350

@@ -680,6 +683,7 @@ impl RecycledHandler {
680683
) -> Self {
681684
Self {
682685
commands,
686+
info: Arc::new(ArcSwapOption::new(None)),
683687
quick_info,
684688
subscriptions: BTreeMap::new(),
685689
multiplexed_subscription_prefix: create_inbox_subject(&builder.inbox_prefix),

0 commit comments

Comments
 (0)