Skip to content

Commit f3dd352

Browse files
committed
feat(nat): inline var in log & avoid unwrap
Signed-off-by: Fredi Raspall <[email protected]>
1 parent 3fb11e9 commit f3dd352

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

nat/src/stateful/mod.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -181,34 +181,36 @@ impl StatefulNat {
181181
Some((translation_data, state.idle_timeout))
182182
}
183183

184+
fn session_timeout_time(timeout: Duration) -> Instant {
185+
Instant::now() + timeout
186+
}
187+
184188
fn create_session_with_dst_vpcd<I: NatIpWithBitmap>(
185189
&self,
186190
flow_key: &FlowKey,
187191
dst_vpcd: VpcDiscriminant,
188192
state: NatFlowState<I>,
189193
idle_timeout: Duration,
190194
) {
191-
fn session_timeout_time(timeout: Duration) -> Instant {
192-
Instant::now() + timeout
193-
}
194-
195195
debug!(
196196
"{}: Creating new flow session entry: {} -> {}",
197197
self.name(),
198198
flow_key.data(),
199199
state
200200
);
201201

202-
let flow_info = FlowInfo::new(session_timeout_time(idle_timeout));
203-
let mut write_guard = flow_info.locked.write().unwrap();
204-
// Write NAT state
205-
write_guard.nat_state = Some(Box::new(state));
206-
// Write destination VPC information, so that pipeline can look it up from the flow table
207-
// when it's not possibly to uniquely determine the destination VPC from source VPC and
208-
// packet's destination address.
209-
write_guard.dst_vpc_info = Some(Box::new(dst_vpcd));
210-
drop(write_guard);
211-
202+
let flow_info = FlowInfo::new(Self::session_timeout_time(idle_timeout));
203+
if let Ok(mut write_guard) = flow_info.locked.write() {
204+
// Write NAT state
205+
write_guard.nat_state = Some(Box::new(state));
206+
// Write destination VPC information, so that pipeline can look it up from the flow table
207+
// when it's not possibly to uniquely determine the destination VPC from source VPC and
208+
// packet's destination address.
209+
write_guard.dst_vpc_info = Some(Box::new(dst_vpcd));
210+
} else {
211+
// flow info is just locally created
212+
unreachable!()
213+
}
212214
self.sessions.insert(*flow_key, flow_info);
213215
}
214216

@@ -606,13 +608,14 @@ impl StatefulNat {
606608
// new session: we don't allow packets "from the outside" to create new sessions.
607609
debug_assert!(alloc.src.is_some());
608610

609-
debug!("{}: Allocated translation data: {}", self.name(), alloc);
611+
debug!("{}: Allocated translation data: {alloc}", self.name());
610612

611613
// Given that at least one of alloc.src or alloc.dst is set, we should always have at
612614
// least one timeout set.
613615
let idle_timeout = alloc.idle_timeout().unwrap_or_else(|| unreachable!());
614616

615617
let translation_data = Self::get_translation_data(&alloc.src, &alloc.dst);
618+
616619
let mut reverse_flow_key =
617620
Self::new_reverse_session(&flow_key, &alloc, src_vpc_id, dst_vpc_id)?;
618621
let (forward_state, reverse_state) = Self::new_states_from_alloc(alloc, idle_timeout);

0 commit comments

Comments
 (0)