Skip to content

Commit bd6dc3a

Browse files
authored
impl Drop for server examples (#376)
In #373 I raised my confusion about the lack of a disconnection mechanism in the source code and the examples, but it was pointed out that `Drop` is the intended way of cleaning up resources. This PR adds a Drop implementation for three of the server examples, so that users that refer to these won't be confused about how to implement their own cleanup.
1 parent ad2403b commit bd6dc3a

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

russh/examples/echoserver.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ async fn main() {
3838

3939
#[derive(Clone)]
4040
struct Server {
41-
clients: Arc<Mutex<HashMap<(usize, ChannelId), russh::server::Handle>>>,
41+
clients: Arc<Mutex<HashMap<usize, (ChannelId, russh::server::Handle)>>>,
4242
id: usize,
4343
}
4444

4545
impl Server {
4646
async fn post(&mut self, data: CryptoVec) {
4747
let mut clients = self.clients.lock().await;
48-
for ((id, channel), ref mut s) in clients.iter_mut() {
48+
for (id, (channel, ref mut s)) in clients.iter_mut() {
4949
if *id != self.id {
5050
let _ = s.data(*channel, data.clone()).await;
5151
}
@@ -76,7 +76,7 @@ impl server::Handler for Server {
7676
) -> Result<bool, Self::Error> {
7777
{
7878
let mut clients = self.clients.lock().await;
79-
clients.insert((self.id, channel.id()), session.handle());
79+
clients.insert(self.id, (channel.id(), session.handle()));
8080
}
8181
Ok(true)
8282
}
@@ -135,3 +135,14 @@ impl server::Handler for Server {
135135
Ok(true)
136136
}
137137
}
138+
139+
impl Drop for Server {
140+
fn drop(&mut self) {
141+
let id = self.id;
142+
let clients = self.clients.clone();
143+
tokio::spawn(async move {
144+
let mut clients = clients.lock().await;
145+
clients.remove(&id);
146+
});
147+
}
148+
}

russh/examples/ratatui_app.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,17 @@ impl Handler for AppServer {
208208
}
209209
}
210210

211+
impl Drop for AppServer {
212+
fn drop(&mut self) {
213+
let id = self.id;
214+
let clients = self.clients.clone();
215+
tokio::spawn(async move {
216+
let mut clients = clients.lock().await;
217+
clients.remove(&id);
218+
});
219+
}
220+
}
221+
211222
#[tokio::main]
212223
async fn main() {
213224
let mut server = AppServer::new();

russh/examples/ratatui_shared_app.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,17 @@ impl Handler for AppServer {
207207
}
208208
}
209209

210+
impl Drop for AppServer {
211+
fn drop(&mut self) {
212+
let id = self.id;
213+
let clients = self.clients.clone();
214+
tokio::spawn(async move {
215+
let mut clients = clients.lock().await;
216+
clients.remove(&id);
217+
});
218+
}
219+
}
220+
210221
#[tokio::main]
211222
async fn main() {
212223
let mut server = AppServer::new();

0 commit comments

Comments
 (0)