|
| 1 | +// Copyright 2025, The Tari Project |
| 2 | +// |
| 3 | +// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the |
| 4 | +// following conditions are met: |
| 5 | +// |
| 6 | +// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following |
| 7 | +// disclaimer. |
| 8 | +// |
| 9 | +// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the |
| 10 | +// following disclaimer in the documentation and/or other materials provided with the distribution. |
| 11 | +// |
| 12 | +// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote |
| 13 | +// products derived from this software without specific prior written permission. |
| 14 | +// |
| 15 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 16 | +// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 17 | +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 18 | +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 19 | +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 20 | +// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE |
| 21 | +// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 22 | + |
| 23 | +use std::{ |
| 24 | + collections::HashMap, |
| 25 | + time::{Duration, Instant}, |
| 26 | +}; |
| 27 | + |
| 28 | +use crate::peer_manager::NodeId; |
| 29 | + |
| 30 | +/// Tracks connection history to nodes to enforce cooldown periods |
| 31 | +pub struct ConnectionHistory { |
| 32 | + /// Maps node IDs to the last time we disconnected from them |
| 33 | + last_disconnected: HashMap<NodeId, Instant>, |
| 34 | +} |
| 35 | + |
| 36 | +impl ConnectionHistory { |
| 37 | + pub fn new() -> Self { |
| 38 | + Self { |
| 39 | + last_disconnected: HashMap::new(), |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /// Record that we disconnected from a node |
| 44 | + pub fn record_disconnection(&mut self, node_id: &NodeId) { |
| 45 | + self.last_disconnected.insert(node_id.clone(), Instant::now()); |
| 46 | + } |
| 47 | + |
| 48 | + /// Check if a node is in cooldown period |
| 49 | + pub fn is_in_cooldown(&self, node_id: &NodeId, cooldown: Duration) -> bool { |
| 50 | + if let Some(last_time) = self.last_disconnected.get(node_id) { |
| 51 | + last_time.elapsed() < cooldown |
| 52 | + } else { |
| 53 | + false |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + /// Get the time elapsed since disconnection for a node |
| 58 | + pub fn time_since_disconnection(&self, node_id: &NodeId) -> Option<Duration> { |
| 59 | + self.last_disconnected.get(node_id).map(|time| time.elapsed()) |
| 60 | + } |
| 61 | + |
| 62 | + /// Clean up old history entries |
| 63 | + pub fn cleanup(&mut self, max_age: Duration) { |
| 64 | + self.last_disconnected.retain(|_, time| time.elapsed() < max_age); |
| 65 | + } |
| 66 | + |
| 67 | + /// Get nodes that are not in cooldown as an iterator |
| 68 | + pub fn available_nodes<'a, I>(&'a self, nodes: I, cooldown: Duration) -> impl Iterator<Item = &'a NodeId> + 'a |
| 69 | + where I: Iterator<Item = &'a NodeId> + 'a { |
| 70 | + nodes.filter(move |node_id| !self.is_in_cooldown(node_id, cooldown)) |
| 71 | + } |
| 72 | + |
| 73 | + /// Get nodes that are not in cooldown (returns a Vec) |
| 74 | + pub fn get_available_nodes<'a, I>(&'a self, nodes: I, cooldown: Duration) -> Vec<NodeId> |
| 75 | + where I: Iterator<Item = &'a NodeId> + 'a { |
| 76 | + self.available_nodes(nodes, cooldown).cloned().collect() |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +impl Default for ConnectionHistory { |
| 81 | + fn default() -> Self { |
| 82 | + Self::new() |
| 83 | + } |
| 84 | +} |
0 commit comments