//! Per-connection queues, counters and rate limiters shared by the peer actors.
use super::{
channels::{self, Channels},
data::{Data, EncodedData},
relay::{self, Prioritized, Receivers},
throttle::Throttle,
};
use crate::Channel;
use commonware_actor::mailbox;
use commonware_cryptography::PublicKey;
use commonware_macros::select;
use commonware_runtime::{
Clock, Error as RuntimeError, Handle, IoBufs, RateLimiter, Spawner, Supervisor,
telemetry::metrics::{CounterFamily, raw::Counter},
};
use std::{collections::BTreeMap, future::Future, hash::Hash, vec::Drain};
use thiserror::Error;
use tracing::debug;
/// Errors that end a connection in shared peer code.
#[derive(Error, Debug)]
pub(crate) enum Error {
#[error("peer killed: {0}")]
Killed(String),
#[error("peer disconnected")]
Disconnected,
#[error("invalid channel")]
InvalidChannel,
}
/// Counter label for the messages of one connection.
pub(crate) trait Label
: Clone + Hash + Eq {
/// Returns the label for data on `channel`.
fn data(peer: &P, channel: Channel) -> Self;
/// Returns the label for messages that fail to decode or name an unregistered channel.
fn invalid(peer: &P) -> Self;
}
/// Control channel of one connection.
pub(crate) trait Control {
/// Message received on the control channel.
type Message;
/// Awaits the next control message, returning `None` once the channel closes.
fn recv(&mut self) -> impl Future