Skip to content

Commit

Permalink
feat(networking): drop SwarmCmds when under load
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuef committed Jun 17, 2024
1 parent 55b3af1 commit f6c2cb9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
33 changes: 33 additions & 0 deletions sn_networking/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,39 @@ pub enum SwarmCmd {
FetchCompleted(RecordKey),
}

impl SwarmCmd {
/// Check if the command can be dropped safely in times of high load.
pub(crate) fn can_be_dropped_safely(&self) -> bool {
match self {
SwarmCmd::Dial { .. } => true,
SwarmCmd::DialWithOpts { .. } => true,
SwarmCmd::GetAllLocalPeers { .. } => false,
SwarmCmd::GetKBuckets { .. } => true,
SwarmCmd::GetClosestKLocalPeers { .. } => false,
SwarmCmd::GetClosestPeersToAddressFromNetwork { .. } => true,
SwarmCmd::GetCloseGroupLocalPeers { .. } => false,
SwarmCmd::GetSwarmLocalState { .. } => false,
SwarmCmd::SendRequest { .. } => true,
SwarmCmd::SendResponse { .. } => true,
SwarmCmd::RecordStoreHasKey { .. } => true,
SwarmCmd::GetAllLocalRecordAddresses { .. } => true,
SwarmCmd::GetNetworkRecord { .. } => true,
SwarmCmd::GetLocalStoreCost { .. } => true,
SwarmCmd::PaymentReceived => false,
SwarmCmd::GetLocalRecord { .. } => true,
SwarmCmd::PutRecord { .. } => false,
SwarmCmd::PutRecordTo { .. } => false,
SwarmCmd::PutLocalRecord { .. } => false,
SwarmCmd::RemoveFailedLocalRecord { .. } => false,
SwarmCmd::AddLocalRecordAsStored { .. } => false,
SwarmCmd::TriggerIntervalReplication => true,
SwarmCmd::RecordNodeIssue { .. } => false,
SwarmCmd::IsPeerShunned { .. } => true,
SwarmCmd::QuoteVerification { .. } => false,
SwarmCmd::FetchCompleted { .. } => false,
}
}
}
/// Debug impl for SwarmCmd to avoid printing full Record, instead only RecodKey
/// and RecordKind are printed.
impl Debug for SwarmCmd {
Expand Down
5 changes: 5 additions & 0 deletions sn_networking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,11 @@ pub(crate) fn send_swarm_cmd(swarm_cmd_sender: Sender<SwarmCmd>, cmd: SwarmCmd)
let capacity = swarm_cmd_sender.capacity();

if capacity == 0 {
if cmd.can_be_dropped_safely() {
warn!("Dropping {cmd:?} as we're at max capacity");
return;
}

error!(
"SwarmCmd channel is full. Await capacity to send: {:?}",
cmd
Expand Down

0 comments on commit f6c2cb9

Please sign in to comment.