Skip to content

Commit

Permalink
Remove unnecessary memory re-allocation for sessions cache
Browse files Browse the repository at this point in the history
  • Loading branch information
emhane committed Jan 14, 2024
1 parent 3620b37 commit b32e750
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 23 deletions.
22 changes: 9 additions & 13 deletions src/handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ pub enum HandlerIn {
/// [`Notification::RelayMsg`] we intend to relay to that peer.
HolePunchEnr(Enr, Notification),

/// Observed socket has been update. The old socket and the current socket.
SocketUpdate(Option<SocketAddr>, SocketAddr),
/// Observed socket has been update, contains the current socket.
SocketUpdate(SocketAddr),
}

/// Messages sent between a node on the network and `Handler`.
Expand Down Expand Up @@ -377,19 +377,15 @@ impl Handler {
warn!("Failed to relay. Error: {}", e);
}
}
HandlerIn::SocketUpdate(old_socket, socket) => {
HandlerIn::SocketUpdate(socket) => {
let ip = socket.ip();
let port = socket.port();
if old_socket.is_none() {
// This node goes from being unreachable to being reachable. Remove
// its sessions to trigger a WHOAREYOU from peers on next sent
// message. If the peer is running this implementation of
// discovery, this makes it possible for the local node to be
// inserted into its peers' kbuckets before the session they
// already had expires. Session duration, in this impl defaults to
// 24 hours.
self.sessions.cache.clear()
}
// This node goes from being unreachable to being reachable.
// Reasonably assuming all its peers are indexing sessions based on
// `node_id`, like this implementation, the first message sent in each
// session from here on will trigger a WHOAREYOU message from the peer
// (since the peer won't be able to find the decryption key for the
// session with the new node id as message's src id).
self.nat_hole_puncher.set_is_behind_nat(self.listen_sockets.iter(), Some(ip), Some(port));
}
}
Expand Down
16 changes: 6 additions & 10 deletions src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -855,11 +855,9 @@ impl Service {
new_ip6,
));
// Notify Handler of socket update
if let Err(e) =
self.handler_send.send(HandlerIn::SocketUpdate(
local_ip6_socket.map(SocketAddr::V6),
new_ip6,
))
if let Err(e) = self
.handler_send
.send(HandlerIn::SocketUpdate(new_ip6))
{
warn!("Failed to send socket update to handler: {}", e);
};
Expand All @@ -883,11 +881,9 @@ impl Service {
new_ip4,
));
// Notify Handler of socket update
if let Err(e) =
self.handler_send.send(HandlerIn::SocketUpdate(
local_ip4_socket.map(SocketAddr::V4),
new_ip4,
))
if let Err(e) = self
.handler_send
.send(HandlerIn::SocketUpdate(new_ip4))
{
warn!("Failed to send socket update {}", e);
};
Expand Down

0 comments on commit b32e750

Please sign in to comment.