0g-storage-node/node/sync/src/context.rs
Bo QIU 1de7afec14
Some checks are pending
abi-consistent-check / build-and-compare (push) Waiting to run
code-coverage / unittest-cov (push) Waiting to run
rust / check (push) Waiting to run
rust / test (push) Waiting to run
rust / lints (push) Waiting to run
functional-test / test (push) Waiting to run
Add more metrics for network unbounded channel (#264)
* Add metrics for file finalization in chunk pool

* Add metrics for network unbounded channel
2024-11-12 17:25:49 +08:00

46 lines
1.3 KiB
Rust

use network::{NetworkMessage, NetworkSender, PeerAction, PeerId, PubsubMessage, ReportSource};
pub struct SyncNetworkContext {
network_send: NetworkSender,
}
impl SyncNetworkContext {
pub fn new(network_send: NetworkSender) -> Self {
Self { network_send }
}
/// Sends an arbitrary network message.
pub fn send(&self, msg: NetworkMessage) {
self.network_send.send(msg).unwrap_or_else(|_| {
warn!("Could not send message to the network service");
})
}
/// Publishes a single message.
pub fn publish(&self, msg: PubsubMessage) {
self.send(NetworkMessage::Publish {
messages: vec![msg],
});
}
pub fn report_peer(&self, peer_id: PeerId, action: PeerAction, msg: &'static str) {
debug!(%peer_id, ?action, %msg, "Report peer");
self.send(NetworkMessage::ReportPeer {
peer_id,
action,
source: ReportSource::SyncService,
msg,
})
}
pub fn ban_peer(&self, peer_id: PeerId, msg: &'static str) {
info!(%peer_id, %msg, "Ban peer");
self.send(NetworkMessage::ReportPeer {
peer_id,
action: PeerAction::Fatal,
source: ReportSource::SyncService,
msg,
})
}
}