@@ -6,6 +6,7 @@ use lightning::ln::msgs::SocketAddress;
6
6
7
7
use bitcoin:: secp256k1:: PublicKey ;
8
8
9
+ use std:: collections:: hash_map:: { self , HashMap } ;
9
10
use std:: net:: ToSocketAddrs ;
10
11
use std:: ops:: Deref ;
11
12
use std:: sync:: { Arc , Mutex } ;
16
17
L :: Target : Logger ,
17
18
{
18
19
pending_connections :
19
- Mutex < Vec < ( PublicKey , Vec < tokio:: sync:: oneshot:: Sender < Result < ( ) , Error > > > ) > > ,
20
+ Mutex < HashMap < PublicKey , Vec < tokio:: sync:: oneshot:: Sender < Result < ( ) , Error > > > > > ,
20
21
peer_manager : Arc < PeerManager > ,
21
22
logger : L ,
22
23
}
26
27
L :: Target : Logger ,
27
28
{
28
29
pub ( crate ) fn new ( peer_manager : Arc < PeerManager > , logger : L ) -> Self {
29
- let pending_connections = Mutex :: new ( Vec :: new ( ) ) ;
30
+ let pending_connections = Mutex :: new ( HashMap :: new ( ) ) ;
30
31
Self { pending_connections, peer_manager, logger }
31
32
}
32
33
@@ -110,26 +111,23 @@ where
110
111
& self , node_id : & PublicKey ,
111
112
) -> Option < tokio:: sync:: oneshot:: Receiver < Result < ( ) , Error > > > {
112
113
let mut pending_connections_lock = self . pending_connections . lock ( ) . unwrap ( ) ;
113
- if let Some ( ( _, connection_ready_senders) ) =
114
- pending_connections_lock. iter_mut ( ) . find ( |( id, _) | id == node_id)
115
- {
116
- let ( tx, rx) = tokio:: sync:: oneshot:: channel ( ) ;
117
- connection_ready_senders. push ( tx) ;
118
- Some ( rx)
119
- } else {
120
- pending_connections_lock. push ( ( * node_id, Vec :: new ( ) ) ) ;
121
- None
114
+ match pending_connections_lock. entry ( * node_id) {
115
+ hash_map:: Entry :: Occupied ( mut entry) => {
116
+ let ( tx, rx) = tokio:: sync:: oneshot:: channel ( ) ;
117
+ entry. get_mut ( ) . push ( tx) ;
118
+ Some ( rx)
119
+ } ,
120
+ hash_map:: Entry :: Vacant ( entry) => {
121
+ entry. insert ( Vec :: new ( ) ) ;
122
+ None
123
+ } ,
122
124
}
123
125
}
124
126
125
127
fn propagate_result_to_subscribers ( & self , node_id : & PublicKey , res : Result < ( ) , Error > ) {
126
128
// Send the result to any other tasks that might be waiting on it by now.
127
129
let mut pending_connections_lock = self . pending_connections . lock ( ) . unwrap ( ) ;
128
- if let Some ( ( _, connection_ready_senders) ) = pending_connections_lock
129
- . iter ( )
130
- . position ( |( id, _) | id == node_id)
131
- . map ( |i| pending_connections_lock. remove ( i) )
132
- {
130
+ if let Some ( connection_ready_senders) = pending_connections_lock. remove ( node_id) {
133
131
for sender in connection_ready_senders {
134
132
let _ = sender. send ( res) . map_err ( |e| {
135
133
debug_assert ! (
0 commit comments