-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Gossip KV): Make member random suffix length configurable. (#1556)
- Loading branch information
1 parent
0bd3a2d
commit 1d35639
Showing
3 changed files
with
28 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,34 @@ | ||
use std::sync::OnceLock; | ||
|
||
use gossip_kv::membership::MemberId; | ||
// use rand::distributions::Distribution; | ||
// use rand::{Rng}; | ||
use rand::distributions::Distribution; | ||
use rand::{thread_rng, Rng}; | ||
|
||
// /// This is a simple distribution that generates a random lower-case alphanumeric | ||
// struct LowercaseAlphanumeric; | ||
// | ||
// impl Distribution<char> for LowercaseAlphanumeric { | ||
// fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> char { | ||
// let choices = b"abcdefghijklmnopqrstuvwxyz0123456789"; | ||
// choices[rng.gen_range(0..choices.len())] as char | ||
// } | ||
// } | ||
/// This is a simple distribution that generates a random lower-case alphanumeric | ||
struct LowercaseAlphanumeric; | ||
|
||
impl Distribution<char> for LowercaseAlphanumeric { | ||
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> char { | ||
let choices = b"abcdefghijklmnopqrstuvwxyz0123456789"; | ||
choices[rng.gen_range(0..choices.len())] as char | ||
} | ||
} | ||
|
||
/// Gets a name for the current process. | ||
pub fn member_name() -> &'static MemberId { | ||
pub fn member_name(random_suffix_len: usize) -> &'static MemberId { | ||
static MEMBER_NAME: OnceLock<MemberId> = OnceLock::new(); | ||
MEMBER_NAME.get_or_init(|| { | ||
// Generate a lower-case alphanumeric suffix of length 4 | ||
|
||
// TODO: Random suffixes are good, but make benchmarking a pain. For now, we'll just use | ||
// the hostname as-is. | ||
|
||
// let suffix: String = thread_rng() | ||
// .sample_iter(&LowercaseAlphanumeric) | ||
// .take(4) | ||
// .map(char::from) | ||
// .collect(); | ||
|
||
// Retrieve hostname | ||
hostname::get().unwrap().to_str().unwrap().to_string() | ||
let hostname = hostname::get().unwrap().to_str().unwrap().to_string(); | ||
|
||
// format!("{}-{}", hostname, suffix) | ||
if random_suffix_len > 0 { | ||
let suffix: String = thread_rng() | ||
.sample_iter(&LowercaseAlphanumeric) | ||
.take(4) | ||
.map(char::from) | ||
.collect(); | ||
format!("{}-{}", hostname, suffix) | ||
} else { | ||
hostname | ||
} | ||
}) | ||
} |