await_tree/global.rs
1// Copyright 2023 RisingWave Labs
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::sync::OnceLock;
16
17use crate::{Config, Registry};
18
19static GLOBAL_REGISTRY: OnceLock<Registry> = OnceLock::new();
20
21/// Initialize the global registry with the given configuration.
22/// Panics if the global registry has already been initialized.
23///
24/// This is **optional** and only needed if you want to use the global registry.
25/// You can always create a new registry with [`Registry::new`] and pass it around to achieve
26/// better encapsulation.
27pub fn init_global_registry(config: Config) {
28 if let Err(_r) = GLOBAL_REGISTRY.set(Registry::new(config)) {
29 panic!("global registry already initialized")
30 }
31}
32
33pub(crate) fn global_registry() -> Option<Registry> {
34 GLOBAL_REGISTRY.get().cloned()
35}