Skip to content

Commit d3dc398

Browse files
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.
1 parent 520523b commit d3dc398

File tree

1 file changed

+32
-36
lines changed

1 file changed

+32
-36
lines changed

protocols/relay/src/priv_client.rs

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ pub struct Behaviour {
9999
directly_connected_peers: HashMap<PeerId, Vec<ConnectionId>>,
100100

101101
/// Queue of actions to return when polled.
102-
queued_actions: VecDeque<Event>,
102+
queued_actions: VecDeque<NetworkBehaviourAction<Event, handler::Prototype>>,
103103
}
104104

105105
/// Create a new client relay [`Behaviour`] with it's corresponding [`Transport`].
@@ -201,52 +201,48 @@ impl NetworkBehaviour for Behaviour {
201201
Either::Right(v) => void::unreachable(v),
202202
};
203203

204-
match handler_event {
205-
handler::Event::ReservationReqAccepted { renewal, limit } => self
206-
.queued_actions
207-
.push_back(Event::ReservationReqAccepted {
204+
let event = match handler_event {
205+
handler::Event::ReservationReqAccepted { renewal, limit } => {
206+
Event::ReservationReqAccepted {
208207
relay_peer_id: event_source,
209208
renewal,
210209
limit,
211-
}),
210+
}
211+
}
212212
handler::Event::ReservationReqFailed { renewal, error } => {
213-
self.queued_actions.push_back(Event::ReservationReqFailed {
213+
Event::ReservationReqFailed {
214214
relay_peer_id: event_source,
215215
renewal,
216216
error,
217-
})
217+
}
218218
}
219219
handler::Event::OutboundCircuitEstablished { limit } => {
220-
self.queued_actions
221-
.push_back(Event::OutboundCircuitEstablished {
222-
relay_peer_id: event_source,
223-
limit,
224-
})
220+
Event::OutboundCircuitEstablished {
221+
relay_peer_id: event_source,
222+
limit,
223+
}
225224
}
226-
handler::Event::OutboundCircuitReqFailed { error } => {
227-
self.queued_actions
228-
.push_back(Event::OutboundCircuitReqFailed {
229-
relay_peer_id: event_source,
230-
error,
231-
})
225+
handler::Event::OutboundCircuitReqFailed { error } => Event::OutboundCircuitReqFailed {
226+
relay_peer_id: event_source,
227+
error,
228+
},
229+
handler::Event::InboundCircuitEstablished { src_peer_id, limit } => {
230+
Event::InboundCircuitEstablished { src_peer_id, limit }
232231
}
233-
handler::Event::InboundCircuitEstablished { src_peer_id, limit } => self
234-
.queued_actions
235-
.push_back(Event::InboundCircuitEstablished { src_peer_id, limit }),
236-
handler::Event::InboundCircuitReqFailed { error } => {
237-
self.queued_actions
238-
.push_back(Event::InboundCircuitReqFailed {
239-
relay_peer_id: event_source,
240-
error,
241-
})
232+
handler::Event::InboundCircuitReqFailed { error } => Event::InboundCircuitReqFailed {
233+
relay_peer_id: event_source,
234+
error,
235+
},
236+
handler::Event::InboundCircuitReqDenied { src_peer_id } => {
237+
Event::InboundCircuitReqDenied { src_peer_id }
242238
}
243-
handler::Event::InboundCircuitReqDenied { src_peer_id } => self
244-
.queued_actions
245-
.push_back(Event::InboundCircuitReqDenied { src_peer_id }),
246-
handler::Event::InboundCircuitReqDenyFailed { src_peer_id, error } => self
247-
.queued_actions
248-
.push_back(Event::InboundCircuitReqDenyFailed { src_peer_id, error }),
249-
}
239+
handler::Event::InboundCircuitReqDenyFailed { src_peer_id, error } => {
240+
Event::InboundCircuitReqDenyFailed { src_peer_id, error }
241+
}
242+
};
243+
244+
self.queued_actions
245+
.push_back(NetworkBehaviourAction::GenerateEvent(event));
250246
}
251247

252248
fn poll(
@@ -255,7 +251,7 @@ impl NetworkBehaviour for Behaviour {
255251
_poll_parameters: &mut impl PollParameters,
256252
) -> Poll<NetworkBehaviourAction<Self::OutEvent, Self::ConnectionHandler>> {
257253
if let Some(event) = self.queued_actions.pop_front() {
258-
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event));
254+
return Poll::Ready(event);
259255
}
260256

261257
let action = match ready!(self.from_transport.poll_next_unpin(cx)) {

0 commit comments

Comments
 (0)