mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-25 15:51:34 +00:00
refactor(relay): directly store actions instead of events (#3372)
Storing `NetworkBehaviourAction`s within the behaviour is more flexible than only storing `OutEvent`s. Additionally, I find expression-oriented code easier to reason about because it typically doesn't contain side-effects.
This commit is contained in:
@ -99,7 +99,7 @@ pub struct Behaviour {
|
|||||||
directly_connected_peers: HashMap<PeerId, Vec<ConnectionId>>,
|
directly_connected_peers: HashMap<PeerId, Vec<ConnectionId>>,
|
||||||
|
|
||||||
/// Queue of actions to return when polled.
|
/// Queue of actions to return when polled.
|
||||||
queued_actions: VecDeque<Event>,
|
queued_actions: VecDeque<NetworkBehaviourAction<Event, handler::Prototype>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a new client relay [`Behaviour`] with it's corresponding [`Transport`].
|
/// Create a new client relay [`Behaviour`] with it's corresponding [`Transport`].
|
||||||
@ -201,52 +201,48 @@ impl NetworkBehaviour for Behaviour {
|
|||||||
Either::Right(v) => void::unreachable(v),
|
Either::Right(v) => void::unreachable(v),
|
||||||
};
|
};
|
||||||
|
|
||||||
match handler_event {
|
let event = match handler_event {
|
||||||
handler::Event::ReservationReqAccepted { renewal, limit } => self
|
handler::Event::ReservationReqAccepted { renewal, limit } => {
|
||||||
.queued_actions
|
Event::ReservationReqAccepted {
|
||||||
.push_back(Event::ReservationReqAccepted {
|
|
||||||
relay_peer_id: event_source,
|
relay_peer_id: event_source,
|
||||||
renewal,
|
renewal,
|
||||||
limit,
|
limit,
|
||||||
}),
|
}
|
||||||
|
}
|
||||||
handler::Event::ReservationReqFailed { renewal, error } => {
|
handler::Event::ReservationReqFailed { renewal, error } => {
|
||||||
self.queued_actions.push_back(Event::ReservationReqFailed {
|
Event::ReservationReqFailed {
|
||||||
relay_peer_id: event_source,
|
relay_peer_id: event_source,
|
||||||
renewal,
|
renewal,
|
||||||
error,
|
error,
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
handler::Event::OutboundCircuitEstablished { limit } => {
|
handler::Event::OutboundCircuitEstablished { limit } => {
|
||||||
self.queued_actions
|
Event::OutboundCircuitEstablished {
|
||||||
.push_back(Event::OutboundCircuitEstablished {
|
relay_peer_id: event_source,
|
||||||
relay_peer_id: event_source,
|
limit,
|
||||||
limit,
|
}
|
||||||
})
|
|
||||||
}
|
}
|
||||||
handler::Event::OutboundCircuitReqFailed { error } => {
|
handler::Event::OutboundCircuitReqFailed { error } => Event::OutboundCircuitReqFailed {
|
||||||
self.queued_actions
|
relay_peer_id: event_source,
|
||||||
.push_back(Event::OutboundCircuitReqFailed {
|
error,
|
||||||
relay_peer_id: event_source,
|
},
|
||||||
error,
|
handler::Event::InboundCircuitEstablished { src_peer_id, limit } => {
|
||||||
})
|
Event::InboundCircuitEstablished { src_peer_id, limit }
|
||||||
}
|
}
|
||||||
handler::Event::InboundCircuitEstablished { src_peer_id, limit } => self
|
handler::Event::InboundCircuitReqFailed { error } => Event::InboundCircuitReqFailed {
|
||||||
.queued_actions
|
relay_peer_id: event_source,
|
||||||
.push_back(Event::InboundCircuitEstablished { src_peer_id, limit }),
|
error,
|
||||||
handler::Event::InboundCircuitReqFailed { error } => {
|
},
|
||||||
self.queued_actions
|
handler::Event::InboundCircuitReqDenied { src_peer_id } => {
|
||||||
.push_back(Event::InboundCircuitReqFailed {
|
Event::InboundCircuitReqDenied { src_peer_id }
|
||||||
relay_peer_id: event_source,
|
|
||||||
error,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
handler::Event::InboundCircuitReqDenied { src_peer_id } => self
|
handler::Event::InboundCircuitReqDenyFailed { src_peer_id, error } => {
|
||||||
.queued_actions
|
Event::InboundCircuitReqDenyFailed { src_peer_id, error }
|
||||||
.push_back(Event::InboundCircuitReqDenied { src_peer_id }),
|
}
|
||||||
handler::Event::InboundCircuitReqDenyFailed { src_peer_id, error } => self
|
};
|
||||||
.queued_actions
|
|
||||||
.push_back(Event::InboundCircuitReqDenyFailed { src_peer_id, error }),
|
self.queued_actions
|
||||||
}
|
.push_back(NetworkBehaviourAction::GenerateEvent(event));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn poll(
|
fn poll(
|
||||||
@ -255,7 +251,7 @@ impl NetworkBehaviour for Behaviour {
|
|||||||
_poll_parameters: &mut impl PollParameters,
|
_poll_parameters: &mut impl PollParameters,
|
||||||
) -> Poll<NetworkBehaviourAction<Self::OutEvent, Self::ConnectionHandler>> {
|
) -> Poll<NetworkBehaviourAction<Self::OutEvent, Self::ConnectionHandler>> {
|
||||||
if let Some(event) = self.queued_actions.pop_front() {
|
if let Some(event) = self.queued_actions.pop_front() {
|
||||||
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event));
|
return Poll::Ready(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
let action = match ready!(self.from_transport.poll_next_unpin(cx)) {
|
let action = match ready!(self.from_transport.poll_next_unpin(cx)) {
|
||||||
|
Reference in New Issue
Block a user