Skip to content

Commit 43dd013

Browse files
committed
feat(dht): put friend_saddr_sink into the lock
1 parent bdfedb4 commit 43dd013

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/toxcore/dht/server/mod.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ pub struct Server {
118118
/// Tx split of a channel to send packets to this peer via UDP socket.
119119
pub tx: Tx,
120120
/// Sink to send friend's `SocketAddr` when it gets known.
121-
friend_saddr_sink: Option<mpsc::UnboundedSender<PackedNode>>,
121+
friend_saddr_sink: Arc<RwLock<Option<mpsc::UnboundedSender<PackedNode>>>>,
122122
/// Struct that stores and manages requests IDs and timeouts.
123123
request_queue: Arc<RwLock<RequestQueue<PublicKey>>>,
124124
/// Close nodes list which contains nodes close to own DHT `PublicKey`.
@@ -211,7 +211,7 @@ impl Server {
211211
sk,
212212
pk,
213213
tx,
214-
friend_saddr_sink: None,
214+
friend_saddr_sink: Default::default(),
215215
request_queue: Arc::new(RwLock::new(RequestQueue::new(Duration::from_secs(PING_TIMEOUT)))),
216216
close_nodes: Arc::new(RwLock::new(Ktree::new(&pk))),
217217
onion_symmetric_key: Arc::new(RwLock::new(secretbox::gen_key())),
@@ -759,7 +759,7 @@ impl Server {
759759
for friend in friends.values_mut() {
760760
friend.try_add_to_close(node);
761761
}
762-
if let Some(ref friend_saddr_sink) = self.friend_saddr_sink {
762+
if let Some(ref friend_saddr_sink) = *self.friend_saddr_sink.read() {
763763
if friends.contains_key(&node.pk) {
764764
Either::A(send_to(friend_saddr_sink, node)
765765
.map_err(|e| e.context(HandlePacketErrorKind::FriendSaddr).into()))
@@ -1466,8 +1466,8 @@ impl Server {
14661466
}
14671467

14681468
/// Set sink to send friend's `SocketAddr` when it gets known.
1469-
pub fn set_friend_saddr_sink(&mut self, friend_saddr_sink: mpsc::UnboundedSender<PackedNode>) {
1470-
self.friend_saddr_sink = Some(friend_saddr_sink);
1469+
pub fn set_friend_saddr_sink(&self, friend_saddr_sink: mpsc::UnboundedSender<PackedNode>) {
1470+
*self.friend_saddr_sink.write() = Some(friend_saddr_sink);
14711471
}
14721472

14731473
/// Get `PrecomputedKey`s cache.
@@ -1725,7 +1725,7 @@ mod tests {
17251725

17261726
#[test]
17271727
fn handle_ping_resp_not_a_friend() {
1728-
let (mut alice, precomp, bob_pk, _bob_sk, _rx, addr) = create_node();
1728+
let (alice, precomp, bob_pk, _bob_sk, _rx, addr) = create_node();
17291729

17301730
let (friend_saddr_tx, friend_saddr_rx) = mpsc::unbounded();
17311731
alice.set_friend_saddr_sink(friend_saddr_tx);
@@ -1748,7 +1748,7 @@ mod tests {
17481748

17491749
#[test]
17501750
fn handle_ping_resp_friend_saddr() {
1751-
let (mut alice, precomp, bob_pk, _bob_sk, _rx, addr) = create_node();
1751+
let (alice, precomp, bob_pk, _bob_sk, _rx, addr) = create_node();
17521752

17531753
let (friend_saddr_tx, friend_saddr_rx) = mpsc::unbounded();
17541754
alice.set_friend_saddr_sink(friend_saddr_tx);
@@ -2003,7 +2003,7 @@ mod tests {
20032003

20042004
#[test]
20052005
fn handle_nodes_resp_friend_saddr() {
2006-
let (mut alice, precomp, bob_pk, _bob_sk, _rx, addr) = create_node();
2006+
let (alice, precomp, bob_pk, _bob_sk, _rx, addr) = create_node();
20072007

20082008
let (friend_saddr_tx, friend_saddr_rx) = mpsc::unbounded();
20092009
alice.set_friend_saddr_sink(friend_saddr_tx);

0 commit comments

Comments
 (0)