Skip to content

Commit b03ed50

Browse files
authored
turn: use ok_or() to simplify command_tx (#689)
Signed-off-by: Xiaobo Liu <cppcoffee@gmail.com>
1 parent d216004 commit b03ed50

File tree

1 file changed

+16
-22
lines changed

1 file changed

+16
-22
lines changed

turn/src/server/mod.rs

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -83,19 +83,16 @@ impl Server {
8383
pub async fn delete_allocations_by_username(&self, username: String) -> Result<()> {
8484
let tx = {
8585
let command_tx = self.command_tx.lock().await;
86-
command_tx.clone()
86+
command_tx.clone().ok_or(Error::ErrClosed)?
8787
};
88-
if let Some(tx) = tx {
89-
let (closed_tx, closed_rx) = mpsc::channel(1);
90-
tx.send(Command::DeleteAllocations(username, Arc::new(closed_rx)))
91-
.map_err(|_| Error::ErrClosed)?;
9288

93-
closed_tx.closed().await;
89+
let (closed_tx, closed_rx) = mpsc::channel(1);
90+
tx.send(Command::DeleteAllocations(username, Arc::new(closed_rx)))
91+
.map_err(|_| Error::ErrClosed)?;
9492

95-
Ok(())
96-
} else {
97-
Err(Error::ErrClosed)
98-
}
93+
closed_tx.closed().await;
94+
95+
Ok(())
9996
}
10097

10198
/// Get information of [`Allocation`][`Allocation`]s by specified [`FiveTuple`]s.
@@ -121,23 +118,20 @@ impl Server {
121118

122119
let tx = {
123120
let command_tx = self.command_tx.lock().await;
124-
command_tx.clone()
121+
command_tx.clone().ok_or(Error::ErrClosed)?
125122
};
126-
if let Some(tx) = tx {
127-
let (infos_tx, mut infos_rx) = mpsc::channel(1);
128-
tx.send(Command::GetAllocationsInfo(five_tuples, infos_tx))
129-
.map_err(|_| Error::ErrClosed)?;
130123

131-
let mut info: HashMap<FiveTuple, AllocationInfo> = HashMap::new();
124+
let (infos_tx, mut infos_rx) = mpsc::channel(1);
125+
tx.send(Command::GetAllocationsInfo(five_tuples, infos_tx))
126+
.map_err(|_| Error::ErrClosed)?;
132127

133-
for _ in 0..tx.receiver_count() {
134-
info.extend(infos_rx.recv().await.ok_or(Error::ErrClosed)?);
135-
}
128+
let mut info: HashMap<FiveTuple, AllocationInfo> = HashMap::new();
136129

137-
Ok(info)
138-
} else {
139-
Err(Error::ErrClosed)
130+
for _ in 0..tx.receiver_count() {
131+
info.extend(infos_rx.recv().await.ok_or(Error::ErrClosed)?);
140132
}
133+
134+
Ok(info)
141135
}
142136

143137
async fn read_loop(

0 commit comments

Comments
 (0)