Skip to content

Commit

Permalink
chore: rearrange switch guard function
Browse files Browse the repository at this point in the history
  • Loading branch information
saiintbrisson committed Nov 28, 2024
1 parent f537a94 commit 8f139e3
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 13 deletions.
8 changes: 1 addition & 7 deletions sentry-core/src/hub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use std::sync::{Arc, RwLock};

use crate::hub_impl::SwitchGuard;
// use crate::hub_impl::SwitchGuard;
use crate::protocol::{Event, Level, SessionStatus};
use crate::types::Uuid;
use crate::{Integration, IntoBreadcrumbs, Scope, ScopeGuard};
Expand Down Expand Up @@ -243,10 +243,4 @@ impl Hub {
})
}}
}

#[inline(always)]
/// Consumes self to create switch guard, that can be used to modify current hub in thread local storage
pub fn into_switch_guard(self: Arc<Hub>) -> SwitchGuard {
crate::hub_impl::SwitchGuard::new(self)
}
}
10 changes: 6 additions & 4 deletions sentry-core/src/hub_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@ thread_local! {
);
}

///Hub switch guard
///
///Used to temporarily swap active hub in thread local storage.
/// A Hub switch guard used to temporarily swap
/// active hub in thread local storage.
pub struct SwitchGuard {
inner: Option<(Arc<Hub>, bool)>,
}

impl SwitchGuard {
pub(crate) fn new(mut hub: Arc<Hub>) -> Self {
/// Swaps the current thread's Hub by the one provided
/// and returns a guard that, when dropped, replaces it
/// to the previous one.
pub fn new(mut hub: Arc<Hub>) -> Self {
let inner = THREAD_HUB.with(|(thread_hub, is_process_hub)| {
// SAFETY: `thread_hub` will always be a valid thread local hub,
// by definition not shared between threads.
Expand Down
4 changes: 2 additions & 2 deletions sentry-tracing/src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ where

let mut extensions = span.extensions_mut();
if let Some(data) = extensions.get_mut::<SentrySpanData>() {
data.hub_switch_guard = Some(data.hub.clone().into_switch_guard());
data.hub_switch_guard = Some(sentry_core::HubSwitchGuard::new(data.hub.clone()));
data.hub.configure_scope(|scope| {
scope.set_span(Some(data.sentry_span.clone()));
})
Expand All @@ -311,7 +311,7 @@ where
data.hub.configure_scope(|scope| {
scope.set_span(data.parent_sentry_span.clone());
});
data.hub_switch_guard = None;
data.hub_switch_guard.take();
}
}

Expand Down

0 comments on commit 8f139e3

Please sign in to comment.