Skip to content

Commit 10b83c1

Browse files
committed
Make CAN_DO more robust
We don't need to do anything in the loop except clone the original packet. Also reviewing more unwraps and converting to expects as we review the code underneath.
1 parent b57f3ca commit 10b83c1

File tree

2 files changed

+51
-17
lines changed

2 files changed

+51
-17
lines changed

rustygear/src/client.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ impl Client {
461461
}
462462
reader_conns
463463
.lock()
464-
.unwrap()
464+
.expect("Threads should not panic while holding lock")
465465
.get_mut(offset)
466466
.and_then(|conn| Some(conn.set_active(false)));
467467
if let Err(e) = reader_ctx.send(offset).await {
@@ -479,7 +479,7 @@ impl Client {
479479
error!("Connection ({}) dropped", offset);
480480
writer_conns
481481
.lock()
482-
.unwrap()
482+
.expect("Threads should not panic while holding lock")
483483
.get_mut(offset)
484484
.and_then(|conn| {
485485
Some(conn.set_active(false))
@@ -715,10 +715,13 @@ impl Client {
715715
{
716716
let (tx, mut rx) = channel(CLIENT_CHANNEL_BOUND_SIZE); // Some day we'll use this param right
717717
{
718+
let mut payload = BytesMut::with_capacity(function.len());
719+
payload.extend(function.bytes());
720+
let can_do = new_req(CAN_DO, payload.freeze());
718721
for (i, conn) in self
719722
.conns
720723
.lock()
721-
.unwrap()
724+
.expect("Threads should not panic while holding lock.")
722725
.iter_mut()
723726
.filter_map(|c| c.to_owned())
724727
.enumerate()
@@ -729,10 +732,7 @@ impl Client {
729732
// Same tx for all jobs, the jobs themselves will have a response conn ref
730733
self.client_data.set_jobs_tx_by_func(k, tx.clone());
731734
}
732-
let mut payload = BytesMut::with_capacity(function.len());
733-
payload.extend(function.bytes());
734-
let can_do = new_req(CAN_DO, payload.freeze());
735-
conn.send_packet(can_do).await?;
735+
conn.send_packet(can_do.clone()).await?;
736736
info!("Sent CAN_DO({}) to {}", function, self.servers[i]);
737737
}
738738
}

rustygear/src/clientdata.rs

+44-10
Original file line numberDiff line numberDiff line change
@@ -115,31 +115,59 @@ impl ClientData {
115115

116116
pub fn receivers(&self) -> MutexGuard<ClientReceivers> {
117117
trace!("Locking receivers");
118-
self.receivers.lock().unwrap()
118+
self.receivers
119+
.lock()
120+
.expect("Threads should not panic while holding lock.")
119121
}
120122

121123
pub fn echo_tx(&self) -> Sender<Bytes> {
122-
self.senders.read().unwrap().echo_tx.clone()
124+
self.senders
125+
.read()
126+
.expect("Threads should not panic while holding lock.")
127+
.echo_tx
128+
.clone()
123129
}
124130

125131
pub fn job_created_tx(&self) -> Sender<JobCreated> {
126-
self.senders.read().unwrap().job_created_tx.clone()
132+
self.senders
133+
.read()
134+
.expect("Threads should not panic while holding lock.")
135+
.job_created_tx
136+
.clone()
127137
}
128138

129139
pub fn error_tx(&self) -> Sender<(Bytes, Bytes)> {
130-
self.senders.read().unwrap().error_tx.clone()
140+
self.senders
141+
.read()
142+
.expect("Threads should not panic while holding lock.")
143+
.error_tx
144+
.clone()
131145
}
132146

133147
pub fn status_res_tx(&self) -> Sender<JobStatus> {
134-
self.senders.read().unwrap().status_res_tx.clone()
148+
self.senders
149+
.read()
150+
.expect("Threads should not panic while holding lock.")
151+
.status_res_tx
152+
.clone()
135153
}
136154

137155
pub fn worker_job_tx(&self) -> Sender<WorkerJob> {
138-
self.senders.read().unwrap().worker_job_tx.clone()
156+
self.senders
157+
.read()
158+
.expect("Threads should not panic while holding lock.")
159+
.worker_job_tx
160+
.clone()
139161
}
140162

141163
pub fn get_sender_by_handle(&self, handle: &ServerHandle) -> Option<Sender<WorkUpdate>> {
142-
match self.senders.read().unwrap().senders_by_handle.get(handle) {
164+
match self
165+
.senders
166+
.read()
167+
.expect("Threads should not panic while holding lock.")
168+
.senders_by_handle
169+
.get(handle)
170+
{
143171
None => None,
144172
Some(sender) => Some(sender.clone()),
145173
}
@@ -148,13 +176,19 @@ impl ClientData {
148176
pub fn set_sender_by_handle(&mut self, handle: ServerHandle, tx: Sender<WorkUpdate>) {
149177
self.senders
150178
.write()
151-
.unwrap()
179+
.expect("Threads should not panic while holding lock.")
152180
.senders_by_handle
153181
.insert(handle, tx);
154182
}
155183

156184
pub fn get_jobs_tx_by_func(&self, func: &Vec<u8>) -> Option<Sender<WorkerJob>> {
157-
match self.senders.read().unwrap().jobs_tx_by_func.get(func) {
185+
match self
186+
.senders
187+
.read()
188+
.expect("Threads should not panic while holding lock.")
189+
.jobs_tx_by_func
190+
.get(func)
191+
{
158192
None => None,
159193
Some(tx) => Some(tx.clone()),
160194
}
@@ -163,7 +197,7 @@ impl ClientData {
163197
pub fn set_jobs_tx_by_func(&mut self, func: Vec<u8>, tx: Sender<WorkerJob>) {
164198
self.senders
165199
.write()
166-
.unwrap()
200+
.expect("Threads should not panic while holding lock.")
167201
.jobs_tx_by_func
168202
.insert(func, tx);
169203
}

0 commit comments

Comments
 (0)