Skip to content

Commit

Permalink
Formatted
Browse files Browse the repository at this point in the history
  • Loading branch information
onnoowl committed Nov 15, 2023
1 parent be89907 commit 64ae373
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions sctp/src/association/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,32 +501,35 @@ impl Association {
let done2 = Arc::clone(&done);
let mut buffer = None;
for raw in packets {
let mut buf = buffer.take().unwrap_or_else(|| BytesMut::with_capacity(16 * 1024));
let mut buf = buffer
.take()
.unwrap_or_else(|| BytesMut::with_capacity(16 * 1024));

// We do the marshalling work in a blocking task here for a reason:
// If we don't tokio tends to run the write_loop and read_loop of one connection on the same OS thread
// This means that even though we release the lock above, the read_loop isn't able to take it, simply because it is not being scheduled by tokio
// Doing it this way, tokio schedules this work on a dedicated blocking thread, this future is suspended, and the read_loop can make progress
match tokio::task::spawn_blocking(move || {
raw.marshal_to(&mut buf).map(|_| buf)
}).await.unwrap() {
Ok(mut buf) => {
let raw = buf.as_ref();
if let Err(err) = net_conn.send(raw.as_ref()).await {
log::warn!("[{}] failed to write packets on net_conn: {}", name2, err);
done2.store(true, Ordering::Relaxed)
} else {
bytes_sent.fetch_add(raw.len(), Ordering::SeqCst);
}

// Reuse allocation. Have to use options, since spawn blocking can't borrow, has to take owernship.
buf.clear();
buffer = Some(buf);
},
Err(err) => {
log::warn!("[{}] failed to serialize a packet: {:?}", name2, err);
match tokio::task::spawn_blocking(move || raw.marshal_to(&mut buf).map(|_| buf))
.await
.unwrap()
{
Ok(mut buf) => {
let raw = buf.as_ref();
if let Err(err) = net_conn.send(raw.as_ref()).await {
log::warn!("[{}] failed to write packets on net_conn: {}", name2, err);
done2.store(true, Ordering::Relaxed)
} else {
bytes_sent.fetch_add(raw.len(), Ordering::SeqCst);
}

// Reuse allocation. Have to use options, since spawn blocking can't borrow, has to take owernship.

Check warning on line 525 in sctp/src/association/mod.rs

View workflow job for this annotation

GitHub Actions / Check formatting style and run clippy

"owernship" should be "ownership".

Check failure on line 525 in sctp/src/association/mod.rs

View workflow job for this annotation

GitHub Actions / Check formatting style and run clippy

`owernship` should be `ownership`
buf.clear();
buffer = Some(buf);
}
Err(err) => {
log::warn!("[{}] failed to serialize a packet: {:?}", name2, err);
}
}
//log::debug!("[{}] sending {} bytes done", name, raw.len());
}

Expand Down

0 comments on commit 64ae373

Please sign in to comment.