Skip to content

Commit

Permalink
Replace lazy_static with std::sync::OnceLock
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafael Bachmann authored and Jarema committed Sep 4, 2024
1 parent 8726093 commit 603a762
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
3 changes: 1 addition & 2 deletions nats-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ license = "Apache-2.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

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

[dev-dependencies]
async-nats = "0.31"
tokio = { version = "1", features = ["full"] }
tokio = { version = "1", features = ["full"] }
futures = "0.3"
14 changes: 7 additions & 7 deletions nats-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ use std::process::{Child, Command};
use std::{env, fs};
use std::{thread, time::Duration};

use lazy_static::lazy_static;
use rand::Rng;
use regex::Regex;
use serde_json::{self, Value};

pub struct Server {
Expand All @@ -37,9 +35,11 @@ struct Inner {
pidfile: PathBuf,
}

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

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

0 comments on commit 603a762

Please sign in to comment.