feat: add seed peer tag to IDGenerator (#154)

Signed-off-by: Gaius <gaius.qi@gmail.com>
This commit is contained in:
Gaius 2023-12-19 20:53:21 +08:00 committed by GitHub
parent 80337d5cad
commit df33010fb2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 2 deletions

View File

@ -97,6 +97,7 @@ async fn main() -> Result<(), anyhow::Error> {
let id_generator = IDGenerator::new(
config.host.ip.unwrap().to_string(),
config.host.hostname.clone(),
config.seed_peer.enable,
);
let id_generator = Arc::new(id_generator);

View File

@ -26,17 +26,28 @@ pub struct IDGenerator {
// hostname is the hostname of the host.
hostname: String,
// is_seed_peer indicates whether the host is a seed peer.
is_seed_peer: bool,
}
// IDGenerator implements the IDGenerator.
impl IDGenerator {
// new creates a new IDGenerator.
pub fn new(ip: String, hostname: String) -> Self {
IDGenerator { ip, hostname }
pub fn new(ip: String, hostname: String, is_seed_peer: bool) -> Self {
IDGenerator {
ip,
hostname,
is_seed_peer,
}
}
// host_id generates the host id.
pub fn host_id(&self) -> String {
if self.is_seed_peer {
return format!("{}-{}-{}", self.ip, self.hostname, "seed");
}
format!("{}-{}", self.ip, self.hostname)
}
@ -88,6 +99,16 @@ impl IDGenerator {
// peer_id generates the peer id.
pub fn peer_id(&self) -> String {
if self.is_seed_peer {
return format!(
"{}-{}-{}-{}",
self.ip,
self.hostname,
"seed",
Uuid::new_v4()
);
}
format!("{}-{}-{}", self.ip, self.hostname, Uuid::new_v4())
}
}