pub struct PeerCapabilityNegotiator {
pub config: NegotiatorConfig,
pub negotiation_history: VecDeque<NegotiationRecord>,
pub max_history: usize,
pub total_negotiations: u64,
pub total_accepted: u64,
pub total_rejected: u64,
}Expand description
Production-grade peer capability negotiator.
PeerCapabilityNegotiator handles the full lifecycle of capability negotiation:
- Building
NegotiationOffers from local config for outbound connections. - Evaluating incoming offers and producing
NegotiationResults. - Recording negotiation outcomes in a bounded history ring.
- Exposing aggregate statistics for monitoring dashboards.
§Thread Safety
PeerCapabilityNegotiator is Send + Sync (all fields are Send + Sync).
For concurrent access, wrap in Arc<Mutex<_>> or Arc<RwLock<_>>.
Fields§
§config: NegotiatorConfigLocal negotiation configuration (policy, capabilities, limits).
negotiation_history: VecDeque<NegotiationRecord>Bounded ring of historical negotiation records (newest last).
max_history: usizeMaximum number of historical records retained.
total_negotiations: u64Total number of negotiate() calls.
total_accepted: u64Negotiations that returned Accepted.
total_rejected: u64Negotiations that returned Rejected.
Implementations§
Source§impl PeerCapabilityNegotiator
impl PeerCapabilityNegotiator
Sourcepub fn new(config: NegotiatorConfig, max_history: usize) -> Self
pub fn new(config: NegotiatorConfig, max_history: usize) -> Self
Create a new negotiator with the given config and maximum history size.
§Panics
Does not panic; a max_history of 0 is valid (no history retained).
Sourcepub fn build_offer(&self, peer_id: String, now: u64) -> NegotiationOffer
pub fn build_offer(&self, peer_id: String, now: u64) -> NegotiationOffer
Build a NegotiationOffer from this node’s local capabilities.
peer_id is the identifier we place in the offer (usually our own peer ID),
and now is the current Unix-millisecond timestamp.
Sourcepub fn evaluate_offer(&self, offer: &NegotiationOffer) -> NegotiationResult
pub fn evaluate_offer(&self, offer: &NegotiationOffer) -> NegotiationResult
Evaluate an incoming peer offer and return the negotiation outcome.
The evaluation proceeds in three phases:
- Protocol version gate — if
offer.protocol_version < min_protocol_version, immediately returnRejected { reason: "protocol version too low", ... }. - Required-capability scan — identify which locally
requiredcapabilities are absent from (or version-incompatible with) the offer. - Policy decision — apply
CapabilityPolicyto decide Accepted/Rejected.
Sourcepub fn negotiate(
&mut self,
peer_id: String,
offer: NegotiationOffer,
start_ms: u64,
end_ms: u64,
) -> &NegotiationResult
pub fn negotiate( &mut self, peer_id: String, offer: NegotiationOffer, start_ms: u64, end_ms: u64, ) -> &NegotiationResult
Evaluate an offer, record it in history, update counters, and return the result.
peer_id— identifier of the remote peer.offer— the remote peer’s capability offer.start_ms— Unix-millisecond timestamp when the negotiation began.end_ms— Unix-millisecond timestamp when evaluation completed.
Returns a reference to the stored NegotiationResult in the history ring.
§History Eviction
When the history ring is full (len() == max_history), the oldest record
is evicted before the new one is appended. If max_history is 0, the
record is computed and counters are updated but nothing is stored.
Sourcepub fn is_capability_supported(
&self,
name: &str,
version: &CapabilityVersion,
) -> bool
pub fn is_capability_supported( &self, name: &str, version: &CapabilityVersion, ) -> bool
Returns true if the local config contains a capability with name whose
version is compatible with version.
Sourcepub fn common_capabilities(&self, offer: &NegotiationOffer) -> Vec<String>
pub fn common_capabilities(&self, offer: &NegotiationOffer) -> Vec<String>
Returns the names of capabilities present in both the local config and the offer, with a compatible version.
Sourcepub fn history_for_peer(&self, peer_id: &str) -> Vec<&NegotiationRecord>
pub fn history_for_peer(&self, peer_id: &str) -> Vec<&NegotiationRecord>
Returns all historical records for the specified peer.
Sourcepub fn negotiator_stats(&self) -> NegotiatorStats
pub fn negotiator_stats(&self) -> NegotiatorStats
Snapshot of aggregate negotiation statistics.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for PeerCapabilityNegotiator
impl RefUnwindSafe for PeerCapabilityNegotiator
impl Send for PeerCapabilityNegotiator
impl Sync for PeerCapabilityNegotiator
impl Unpin for PeerCapabilityNegotiator
impl UnsafeUnpin for PeerCapabilityNegotiator
impl UnwindSafe for PeerCapabilityNegotiator
Blanket Implementations§
impl<T> Allocation for T
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more