Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f82dccf

Browse files
author
Rafael Bachmann
committedAug 5, 2024·
Replace lazy_static with std::sync::OnceLock
1 parent e9919a0 commit f82dccf

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed
 

‎nats-server/Cargo.toml

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ license = "Apache-2.0"
77
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
88

99
[dependencies]
10-
lazy_static = "1.4.0"
1110
regex = { version = "1.7.1", default-features = false, features = ["std", "unicode-perl"] }
1211
url = "2"
1312
serde_json = "1.0.104"
@@ -17,5 +16,5 @@ tokio-retry = "0.3.0"
1716

1817
[dev-dependencies]
1918
async-nats = "0.31"
20-
tokio = { version = "1", features = ["full"] }
19+
tokio = { version = "1", features = ["full"] }
2120
futures = "0.3"

‎nats-server/src/lib.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ use std::process::{Child, Command};
1919
use std::{env, fs};
2020
use std::{thread, time::Duration};
2121

22-
use lazy_static::lazy_static;
2322
use rand::Rng;
24-
use regex::Regex;
2523
use serde_json::{self, Value};
2624

2725
pub struct Server {
@@ -37,9 +35,11 @@ struct Inner {
3735
pidfile: PathBuf,
3836
}
3937

40-
lazy_static! {
41-
static ref SD_RE: Regex = Regex::new(r#".+\sStore Directory:\s+"([^"]+)""#).unwrap();
42-
static ref CLIENT_RE: Regex = Regex::new(r".+\sclient connections on\s+(\S+)").unwrap();
38+
macro_rules! regex {
39+
($re:literal $(,)?) => {{
40+
static RE: std::sync::OnceLock<regex::Regex> = std::sync::OnceLock::new();
41+
RE.get_or_init(|| regex::Regex::new($re).unwrap())
42+
}};
4343
}
4444

4545
impl Drop for Server {
@@ -48,7 +48,7 @@ impl Drop for Server {
4848
self.inner.child.wait().unwrap();
4949
if let Ok(log) = fs::read_to_string(self.inner.logfile.as_os_str()) {
5050
// Check if we had JetStream running and if so cleanup the storage directory.
51-
if let Some(caps) = SD_RE.captures(&log) {
51+
if let Some(caps) = regex!(r#".+\sStore Directory:\s+"([^"]+)""#).captures(&log) {
5252
let sd = caps.get(1).map_or("", |m| m.as_str());
5353
fs::remove_dir_all(sd).ok();
5454
}
@@ -120,7 +120,7 @@ impl Server {
120120
for _ in 0..100 {
121121
match fs::read_to_string(self.inner.logfile.as_os_str()) {
122122
Ok(l) => {
123-
if let Some(cre) = CLIENT_RE.captures(&l) {
123+
if let Some(cre) = regex!(r".+\sclient connections on\s+(\S+)").captures(&l) {
124124
return cre.get(1).unwrap().as_str().replace("0.0.0.0", "localhost");
125125
} else {
126126
thread::sleep(Duration::from_millis(500));

0 commit comments

Comments
 (0)
Please sign in to comment.