Return outgoing channel and release lock
This change gets a copy of the outgoing channel and releases the lock,
as the channel is a sync channel, and therefore can hold the lock until
it the other side has pulled the value from its receiving end.
@@ -153,19 +153,23 @@ impl Mesh {
/// This is a convenience function and is equivalent to
/// `mesh.outgoing(envelope.id()).send(Vec::from(envelope))`.
pub fn send(&self, envelope: Envelope) -> Result<(), SendError> {
- let state = &self.state.read().map_err(|_| SendError::PoisonedLock)?;
let id = envelope.id().to_string();
- if let Some(mesh_id) = state.unique_ids.get_by_key(&id) {
- match state.outgoings.get(mesh_id) {
- Some(ref outgoing) => match outgoing.send(Vec::from(envelope)) {
- Ok(()) => Ok(()),
- Err(err) => Err(SendError::from_outgoing_send_error(err, id)),
- },
- None => Err(SendError::NotFound),
- }
- } else {
- Err(SendError::NotFound)
- }
+ let outgoing = {
+ let state = self.state.read().map_err(|_| SendError::PoisonedLock)?;
+ let mesh_id = state
+ .unique_ids
+ .get_by_key(&id)
+ .ok_or(SendError::NotFound)?;
+ state
+ .outgoings
+ .get(mesh_id)
+ .cloned()
+ .ok_or(SendError::NotFound)?
+ };
+
+ outgoing
+ .send(Vec::from(envelope))
+ .map_err(|err| SendError::from_outgoing_send_error(err, id))
}
/// Receive a new envelope from the mesh.
