Skip to content

Commit a74818e

Browse files
authored
refactor(cubestore): Use OnceLock for GLOBAL_SINK (#9023)
1 parent 3ca3811 commit a74818e

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

rust/cubestore/cubestore/src/util/metrics.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use crate::CubeError;
1818
use std::net::ToSocketAddrs;
1919
use std::net::UdpSocket;
20-
use std::sync::Once;
2120

2221
#[derive(Debug, PartialEq, Eq)]
2322
pub enum Compatibility {
@@ -189,8 +188,9 @@ impl Sink {
189188

190189
mod global_sink {
191190
use super::*;
192-
static mut GLOBAL_SINK: Option<Sink> = None;
193-
static ONCE: Once = Once::new();
191+
use std::sync::OnceLock;
192+
193+
static GLOBAL_SINK: OnceLock<Option<Sink>> = OnceLock::new();
194194

195195
pub fn init(
196196
bind_addr: impl ToSocketAddrs,
@@ -201,11 +201,9 @@ mod global_sink {
201201
let s = Sink::connect(bind_addr, server_addr, mode, constant_tags)?;
202202

203203
let mut called = false;
204-
ONCE.call_once(|| {
205-
unsafe {
206-
GLOBAL_SINK = Some(s);
207-
}
204+
GLOBAL_SINK.get_or_init(|| {
208205
called = true;
206+
Some(s)
209207
});
210208
if !called {
211209
panic!("Metrics initialized twice or used before initialization");
@@ -214,9 +212,7 @@ mod global_sink {
214212
}
215213

216214
pub(super) fn sink() -> &'static Option<Sink> {
217-
// Ensure we synchronize access to GLOBAL_SINK.
218-
ONCE.call_once(|| {});
219-
unsafe { &GLOBAL_SINK }
215+
GLOBAL_SINK.get_or_init(|| None)
220216
}
221217
}
222218

0 commit comments

Comments
 (0)