66 dashmap:: DashMap ,
77 ring:: signature:: Ed25519KeyPair ,
88 serde:: { Deserialize , Serialize } ,
9+ std:: sync:: atomic:: AtomicU64 ,
910 std:: sync:: Arc ,
1011 tokio:: net:: TcpStream ,
1112 tokio:: sync:: mpsc:: { UnboundedReceiver , UnboundedSender } ,
@@ -57,15 +58,15 @@ pub struct RoutingRequest {
5758
5859#[ derive( Clone ) ]
5960pub struct Peers {
60- max_peers : u64 ,
61+ max_peers : Arc < AtomicU64 > ,
6162 send_to_loop : MessageSender ,
6263 peers : Arc < DashMap < String , Peer > > ,
6364}
6465
6566impl Peers {
6667 pub fn new ( max_peers : u64 , send_to_loop : MessageSender ) -> Self {
6768 Self {
68- max_peers,
69+ max_peers : Arc :: new ( max_peers . into ( ) ) ,
6970 send_to_loop,
7071 peers : Arc :: new ( DashMap :: new ( ) ) ,
7172 }
@@ -75,6 +76,15 @@ impl Peers {
7576 & self . peers
7677 }
7778
79+ pub fn max_peers ( & self ) -> u64 {
80+ self . max_peers . load ( std:: sync:: atomic:: Ordering :: Relaxed )
81+ }
82+
83+ pub fn set_max_peers ( & self , max_peers : u64 ) {
84+ self . max_peers
85+ . store ( max_peers, std:: sync:: atomic:: Ordering :: Relaxed ) ;
86+ }
87+
7888 pub fn get ( & self , name : & str ) -> Option < dashmap:: mapref:: one:: Ref < ' _ , String , Peer > > {
7989 self . peers . get ( name)
8090 }
@@ -94,15 +104,15 @@ impl Peers {
94104 /// remove the one with the oldest last_message.
95105 pub async fn insert ( & self , name : String , peer : Peer ) {
96106 self . peers . insert ( name, peer) ;
97- if self . peers . len ( ) > self . max_peers as usize {
107+ if self . peers . len ( ) as u64 > self . max_peers . load ( std :: sync :: atomic :: Ordering :: Relaxed ) {
98108 let oldest = self
99109 . peers
100110 . iter ( )
101111 . min_by_key ( |p| p. last_message )
102112 . unwrap ( )
103113 . key ( )
104114 . clone ( ) ;
105- self . peers . remove ( & oldest) ;
115+ self . remove ( & oldest) . await ;
106116 crate :: fd_manager:: send_fd_manager_hit_fds_limit (
107117 & Address :: new ( "our" , NET_PROCESS_ID . clone ( ) ) ,
108118 & self . send_to_loop ,
@@ -122,7 +132,7 @@ impl Peers {
122132 sorted_peers. sort_by_key ( |p| p. last_message ) ;
123133 to_remove. extend ( sorted_peers. iter ( ) . take ( n) ) ;
124134 for peer in to_remove {
125- self . peers . remove ( & peer. identity . name ) ;
135+ self . remove ( & peer. identity . name ) . await ;
126136 }
127137 crate :: fd_manager:: send_fd_manager_hit_fds_limit (
128138 & Address :: new ( "our" , NET_PROCESS_ID . clone ( ) ) ,
@@ -189,9 +199,13 @@ impl Peer {
189199 }
190200
191201 /// Send a message to the peer.
192- pub fn send ( & mut self , km : KernelMessage ) {
193- self . sender . send ( km) . expect ( "net: peer sender was dropped" ) ;
202+ pub fn send (
203+ & mut self ,
204+ km : KernelMessage ,
205+ ) -> Result < ( ) , tokio:: sync:: mpsc:: error:: SendError < KernelMessage > > {
206+ self . sender . send ( km) ?;
194207 self . set_last_message ( ) ;
208+ Ok ( ( ) )
195209 }
196210
197211 /// Update the last message time to now.
@@ -222,7 +236,6 @@ pub struct NetData {
222236 pub pending_passthroughs : PendingPassthroughs ,
223237 /// only used by routers
224238 pub active_passthroughs : ActivePassthroughs ,
225- pub max_peers : u64 ,
226239 pub max_passthroughs : u64 ,
227240 pub fds_limit : u64 ,
228241}
0 commit comments