Skip to content

Commit 303dbd7

Browse files
committed
refactor LocalVersions to never give out a mutex guard
1 parent dcc7deb commit 303dbd7

File tree

2 files changed

+30
-15
lines changed

2 files changed

+30
-15
lines changed

crates/xds/src/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ impl DeltaClientStream {
446446
) -> Result<()> {
447447
crate::metrics::actions_total(KIND_CLIENT, "refresh").inc();
448448
for (rt, names) in subs {
449-
let initial_resource_versions = local.get(rt).clone();
449+
let initial_resource_versions = local.get_versions(rt);
450450
self.req_tx
451451
.send(DeltaDiscoveryRequest {
452452
node: Some(Node {

crates/xds/src/config.rs

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ impl LocalVersions {
4040
}
4141

4242
#[inline]
43-
pub fn get(&self, ty: &str) -> parking_lot::MutexGuard<'_, VersionMap> {
43+
fn get_map_guard(&self, ty: &str) -> parking_lot::MutexGuard<'_, VersionMap> {
4444
let g = self
4545
.versions
4646
.iter()
@@ -53,6 +53,33 @@ impl LocalVersions {
5353
panic!("unable to retrieve `{ty}` versions, available versions are {versions:?}");
5454
}
5555
}
56+
57+
#[inline]
58+
pub fn update_versions(
59+
&self,
60+
type_url: &str,
61+
removed_resources: &Vec<String>,
62+
updated_resources: Vec<(String, String)>,
63+
) {
64+
let mut guard = self.get_map_guard(type_url);
65+
66+
// Remove any resources the upstream server has removed/doesn't have,
67+
// we do this before applying any new/updated resources in case a
68+
// resource is in both lists, though really that would be a bug in
69+
// the upstream server
70+
for removed in removed_resources {
71+
guard.remove(removed);
72+
}
73+
74+
for (k, v) in updated_resources {
75+
guard.insert(k, v);
76+
}
77+
}
78+
79+
#[inline]
80+
pub fn get_versions(&self, type_url: &str) -> VersionMap {
81+
self.get_map_guard(type_url).clone()
82+
}
5683
}
5784

5885
pub struct ClientState {
@@ -268,19 +295,7 @@ pub fn handle_delta_discovery_responses<C: Configuration>(
268295
let res = config.apply_delta(&type_url, response.resources, &response.removed_resources, remote_addr);
269296

270297
if res.is_ok() {
271-
let mut lock = local.get(&type_url);
272-
273-
// Remove any resources the upstream server has removed/doesn't have,
274-
// we do this before applying any new/updated resources in case a
275-
// resource is in both lists, though really that would be a bug in
276-
// the upstream server
277-
for removed in response.removed_resources {
278-
lock.remove(&removed);
279-
}
280-
281-
for (k, v) in version_map {
282-
lock.insert(k, v);
283-
}
298+
local.update_versions(&type_url, &response.removed_resources, version_map);
284299
}
285300

286301
res

0 commit comments

Comments
 (0)