Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace lazy_static with std::sync::OnceLock in nats-server #1298

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading