Skip to content

Commit c0b1ddb

Browse files
committed
chore: remove entry if peer pending response is empty
1 parent a346f25 commit c0b1ddb

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/p2p/request_response.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use libp2p::swarm::{
2121
};
2222
use libp2p::{request_response, PeerId, StreamProtocol};
2323
use pollable_map::futures::FutureMap;
24+
use std::collections::hash_map::Entry;
2425
use std::collections::HashMap;
2526
use std::io::{Error as IoError, ErrorKind as IoErrorKind};
2627
use std::task::{Context, Poll};
@@ -185,23 +186,34 @@ impl Behaviour {
185186
peer_id: PeerId,
186187
error: OutboundFailure,
187188
) {
188-
if let Some(list) = self.pending_response.get_mut(&peer_id) {
189+
if let Entry::Occupied(mut entry) = self.pending_response.entry(peer_id) {
190+
let list = entry.get_mut();
191+
189192
if let Some(tx) = list.remove(&id) {
190193
_ = tx.send(Err(std::io::Error::new(
191194
std::io::ErrorKind::BrokenPipe,
192195
error,
193196
)));
194197
}
198+
199+
if list.is_empty() {
200+
entry.remove();
201+
}
195202
}
196203
}
204+
197205
fn process_inbound_failure(
198206
&mut self,
199207
id: InboundRequestId,
200208
peer_id: PeerId,
201209
_: InboundFailure,
202210
) {
203-
if let Some(list) = self.pending_request.get_mut(&peer_id) {
211+
if let Entry::Occupied(mut entry) = self.pending_request.entry(peer_id) {
212+
let list = entry.get_mut();
204213
list.remove(&id);
214+
if list.is_empty() {
215+
entry.remove();
216+
}
205217
}
206218
}
207219
}

0 commit comments

Comments
 (0)