Skip to content

Commit 4d3cdb4

Browse files
committed
Remove unnecessary mutexes
Simplify discovery.rs a bit by removing some mutexes that are not strictly necessary. We do however need to work around proxy-wasm/proxy-wasm-rust-sdk#303 by using the hostcalls module (around which the HttpContext trait is just a trivial wrapper) directly
1 parent 4d06f8d commit 4d3cdb4

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

src/discovery.rs

+14-16
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ pub struct Root {
3232
/// Plugin config loaded from the envoy configuration
3333
pub plugin_config: Option<Arc<PluginConfiguration>>,
3434
/// A set of Open ID Resolvers which are used to load the configuration from the discovery endpoint
35-
pub open_id_resolvers: Mutex<Vec<OpenIdResolver>>,
35+
pub open_id_resolvers: Vec<OpenIdResolver>,
3636
/// A set of Open ID Providers which are used to store the configuration from the discovery endpoint
37-
pub open_id_providers: Mutex<Vec<OpenIdProvider>>,
37+
pub open_id_providers: Vec<OpenIdProvider>,
3838
/// Queue of waiting requests which are waiting for the configuration to be loaded
3939
pub waiting: Mutex<Vec<u32>>,
4040
/// Flag to determine if the discovery is active
@@ -159,7 +159,7 @@ impl RootContext for Root {
159159
};
160160
resolvers.push(open_id_resolver);
161161
}
162-
self.open_id_resolvers = Mutex::new(resolvers);
162+
self.open_id_resolvers = resolvers;
163163

164164
// Tick immediately to load the configuration.
165165
// See `on_tick` for more information.
@@ -187,7 +187,7 @@ impl RootContext for Root {
187187

188188
// Return the http context.
189189
Some(Box::new(ConfiguredOidc {
190-
open_id_providers: Arc::new(self.open_id_providers.lock().unwrap().to_vec()),
190+
open_id_providers: Arc::new(self.open_id_providers.clone()),
191191
plugin_config: self.plugin_config.clone()?,
192192
token_id: None,
193193
request_id: None,
@@ -231,7 +231,7 @@ impl RootContext for Root {
231231

232232
// Set discovery to active and set the state of all resolvers to `LoadingConfig`.
233233
self.discovery_active = true;
234-
for resolver in self.open_id_resolvers.lock().unwrap().iter_mut() {
234+
for resolver in self.open_id_resolvers.iter_mut() {
235235
resolver.state = OpenIdResolverState::LoadingConfig;
236236
}
237237
// Tick every x ms to not overload the openid configuration endpoint. x is the configured interval.
@@ -245,8 +245,6 @@ impl RootContext for Root {
245245
// configured interval.
246246
let all_resolvers_done = self
247247
.open_id_resolvers
248-
.lock()
249-
.unwrap()
250248
.iter_mut()
251249
.all(|r| matches!(r.state, OpenIdResolverState::Ready { .. }));
252250

@@ -278,12 +276,12 @@ impl RootContext for Root {
278276
}
279277

280278
// Make call to openid configuration endpoint for all providers whose state is not ready.
281-
for resolver in self.open_id_resolvers.lock().unwrap().iter_mut() {
279+
for resolver in self.open_id_resolvers.iter_mut() {
282280
match &resolver.state {
283281
OpenIdResolverState::LoadingConfig { .. } => {
284282
// Make call to openid configuration endpoint and load configuration
285283
// The response is handled in `on_http_call_response`.
286-
match self.dispatch_http_call(
284+
match hostcalls::dispatch_http_call(
287285
&resolver.open_id_config.upstream_cluster,
288286
vec![
289287
(":method", "GET"),
@@ -309,7 +307,7 @@ impl RootContext for Root {
309307
// Make call to jwks endpoint for all providers whose state is not ready.
310308
// The response is handled in `on_http_call_response`.
311309
OpenIdResolverState::LoadingJwks { open_id_response } => {
312-
match self.dispatch_http_call(
310+
match hostcalls::dispatch_http_call(
313311
&resolver.open_id_config.upstream_cluster,
314312
vec![
315313
(":method", "GET"),
@@ -356,9 +354,10 @@ impl Context for Root {
356354
_num_trailers: usize,
357355
) {
358356
debug!("received http call response with token_id: {}", token_id);
357+
let body = self.get_http_call_response_body(0, _body_size);
359358

360359
// Find resolver to update based on toke_id
361-
let mut binding = self.open_id_resolvers.lock().unwrap();
360+
let binding = &mut self.open_id_resolvers;
362361
let resolver_to_update = match binding
363362
.iter_mut()
364363
.find(|resolver| resolver.token_ids.contains(&token_id))
@@ -381,7 +380,7 @@ impl Context for Root {
381380
// openid configuration.
382381
OpenIdResolverState::LoadingConfig => {
383382
// Parse the response body as json.
384-
let body = match self.get_http_call_response_body(0, _body_size) {
383+
let body = match body {
385384
Some(body) => body,
386385
None => {
387386
warn!("no body in openid config response");
@@ -416,7 +415,7 @@ impl Context for Root {
416415
open_id_response, ..
417416
} => {
418417
// Parse body using serde_json or fail
419-
let body = match self.get_http_call_response_body(0, _body_size) {
418+
let body = match body {
420419
Some(body) => body,
421420
None => {
422421
warn!("no body in jwks response");
@@ -450,15 +449,14 @@ impl Context for Root {
450449
}
451450

452451
// Find OpenIdProvider to update or create a new one
453-
let mut open_id_providers = self.open_id_providers.lock().unwrap();
454-
let provider = open_id_providers.iter_mut().find(|provider| {
452+
let provider = self.open_id_providers.iter_mut().find(|provider| {
455453
provider.issuer == resolver_to_update.open_id_config.authority
456454
});
457455

458456
if let Some(p) = provider {
459457
p.public_keys = keys;
460458
} else {
461-
open_id_providers.push(OpenIdProvider {
459+
self.open_id_providers.push(OpenIdProvider {
462460
open_id_config: resolver_to_update.open_id_config.clone(),
463461
auth_endpoint: open_id_response.authorization_endpoint.clone(),
464462
token_endpoint: open_id_response.token_endpoint.clone(),

src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ proxy_wasm::main! {{
4848
// plugin config and the discovery endpoints.
4949
proxy_wasm::set_root_context(|_| -> Box<dyn RootContext> { Box::new(Root {
5050
plugin_config: None,
51-
open_id_providers: Mutex::new(vec![]),
52-
open_id_resolvers: Mutex::new(vec![]),
51+
open_id_providers: vec![],
52+
open_id_resolvers: vec![],
5353
waiting: Mutex::new(Vec::new()),
5454
discovery_active: false,
5555
}) });

0 commit comments

Comments
 (0)