Skip to content

Commit 8eb0ce2

Browse files
committed
chore: remove unnecessary rwlock
1 parent 4baa98d commit 8eb0ce2

File tree

4 files changed

+20
-27
lines changed

4 files changed

+20
-27
lines changed

network/src/protocols/hole_punching/component/connection_request.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl TryFrom<&packed::ConnectionRequestReader<'_>> for RequestContent {
8484

8585
pub(crate) struct ConnectionRequestProcess<'a> {
8686
message: packed::ConnectionRequestReader<'a>,
87-
protocol: &'a HolePunching,
87+
protocol: &'a mut HolePunching,
8888
peer: PeerIndex,
8989
p2p_control: &'a ServiceAsyncControl,
9090
msg_item_id: u32,
@@ -93,7 +93,7 @@ pub(crate) struct ConnectionRequestProcess<'a> {
9393
impl<'a> ConnectionRequestProcess<'a> {
9494
pub(crate) fn new(
9595
message: packed::ConnectionRequestReader<'a>,
96-
protocol: &'a HolePunching,
96+
protocol: &'a mut HolePunching,
9797
peer: PeerIndex,
9898
p2p_control: &'a ServiceAsyncControl,
9999
msg_item_id: u32,
@@ -107,7 +107,7 @@ impl<'a> ConnectionRequestProcess<'a> {
107107
}
108108
}
109109

110-
pub(crate) async fn execute(self) -> Status {
110+
pub(crate) async fn execute(mut self) -> Status {
111111
let content = match RequestContent::try_from(&self.message) {
112112
Ok(content) => content,
113113
Err(status) => return status,
@@ -153,12 +153,12 @@ impl<'a> ConnectionRequestProcess<'a> {
153153
}
154154

155155
async fn respond_delivered(
156-
&self,
156+
&mut self,
157157
from_peer_id: PeerId,
158158
to_peer_id: &PeerId,
159159
remote_listens: Vec<Multiaddr>,
160160
) -> Status {
161-
if let Some((_, t)) = self.protocol.pending_delivered.read().get(&from_peer_id) {
161+
if let Some((_, t)) = self.protocol.pending_delivered.get(&from_peer_id) {
162162
let now = unix_time_as_millis();
163163
if now - t < HOLE_PUNCHING_INTERVAL {
164164
return StatusCode::Ignore
@@ -231,9 +231,10 @@ impl<'a> ConnectionRequestProcess<'a> {
231231
return StatusCode::ForwardError.with_context(error);
232232
}
233233

234-
let mut pending_delivered = self.protocol.pending_delivered.write();
235234
let now = unix_time_as_millis();
236-
pending_delivered.insert(from_peer_id, (remote_listens, now));
235+
self.protocol
236+
.pending_delivered
237+
.insert(from_peer_id, (remote_listens, now));
237238

238239
Status::ok()
239240
}

network/src/protocols/hole_punching/component/connection_request_delivered.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ impl TryFrom<&packed::ConnectionRequestDeliveredReader<'_>> for DeliverdContent
9191

9292
pub struct ConnectionRequestDeliveredProcess<'a> {
9393
message: packed::ConnectionRequestDeliveredReader<'a>,
94-
protocol: &'a HolePunching,
94+
protocol: &'a mut HolePunching,
9595
p2p_control: &'a ServiceAsyncControl,
9696
peer: PeerIndex,
9797
bind_addr: Option<SocketAddr>,
@@ -101,7 +101,7 @@ pub struct ConnectionRequestDeliveredProcess<'a> {
101101
impl<'a> ConnectionRequestDeliveredProcess<'a> {
102102
pub(crate) fn new(
103103
message: packed::ConnectionRequestDeliveredReader<'a>,
104-
protocol: &'a HolePunching,
104+
protocol: &'a mut HolePunching,
105105
p2p_control: &'a ServiceAsyncControl,
106106
peer: PeerIndex,
107107
bind_addr: Option<SocketAddr>,
@@ -153,7 +153,7 @@ impl<'a> ConnectionRequestDeliveredProcess<'a> {
153153
self.forward_delivered(&content.from).await
154154
} else {
155155
// the current peer is the target peer, respond the sync back
156-
let request_start = self.protocol.inflight_requests.write().remove(&content.to);
156+
let request_start = self.protocol.inflight_requests.remove(&content.to);
157157

158158
match request_start {
159159
Some(start) => {

network/src/protocols/hole_punching/component/connection_sync.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ impl<'a> ConnectionSyncProcess<'a> {
107107
let listens_info = self
108108
.protocol
109109
.pending_delivered
110-
.read()
111110
.get(&content.from)
112111
.map(|info| info.0.clone());
113112

network/src/protocols/hole_punching/mod.rs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::{collections::HashMap, net::SocketAddr, sync::Arc, time::Duration};
33
use ckb_logger::{debug, error, trace, warn};
44
use ckb_systemtime::unix_time_as_millis;
55
use ckb_types::{packed, prelude::*};
6-
use ckb_util::RwLock;
76
use p2p::{
87
async_trait, bytes,
98
context::{ProtocolContext, ProtocolContextMutRef},
@@ -40,9 +39,9 @@ pub(crate) struct HolePunching {
4039
network_state: Arc<NetworkState>,
4140
bind_addr: Option<SocketAddr>,
4241
// Request timestamp recorded
43-
inflight_requests: RwLock<HashMap<PeerId, u64>>,
42+
inflight_requests: HashMap<PeerId, u64>,
4443
// Delivered timestamp recorded
45-
pending_delivered: RwLock<HashMap<PeerId, PendingDeliveredInfo>>,
44+
pending_delivered: HashMap<PeerId, PendingDeliveredInfo>,
4645
rate_limiter: RateLimiter<(PeerIndex, u32)>,
4746
forward_rate_limiter: RateLimiter<(PeerId, PeerId, u32)>,
4847
}
@@ -170,14 +169,10 @@ impl ServiceProtocol for HolePunching {
170169
async fn notify(&mut self, context: &mut ProtocolContext, _token: u64) {
171170
let status = self.network_state.connection_status();
172171

173-
{
174-
let mut pending_delivered = self.pending_delivered.write();
175-
let mut inflight_requests = self.inflight_requests.write();
176-
177-
let now = unix_time_as_millis();
178-
pending_delivered.retain(|_, (_, t)| (now - *t) < TIMEOUT);
179-
inflight_requests.retain(|_, t| (now - *t) < TIMEOUT);
180-
}
172+
let now = unix_time_as_millis();
173+
self.pending_delivered
174+
.retain(|_, (_, t)| (now - *t) < TIMEOUT);
175+
self.inflight_requests.retain(|_, t| (now - *t) < TIMEOUT);
181176

182177
if status.non_whitelist_outbound < status.max_outbound && status.total > 0 {
183178
let target = &self.network_state.required_flags;
@@ -241,10 +236,9 @@ impl ServiceProtocol for HolePunching {
241236
}
242237
}
243238

244-
let mut inflight_requests = self.inflight_requests.write();
245239
let now = unix_time_as_millis();
246240
for peer_id in inflight {
247-
inflight_requests.insert(peer_id, now);
241+
self.inflight_requests.insert(peer_id, now);
248242
}
249243
}
250244
}
@@ -280,12 +274,11 @@ impl HolePunching {
280274
}
281275
}
282276
}
283-
284277
bind_addr
285278
},
286279
network_state,
287-
pending_delivered: RwLock::new(HashMap::new()),
288-
inflight_requests: RwLock::new(HashMap::new()),
280+
pending_delivered: HashMap::new(),
281+
inflight_requests: HashMap::new(),
289282
rate_limiter,
290283
forward_rate_limiter,
291284
}

0 commit comments

Comments
 (0)