Skip to content

Commit

Permalink
[ISSUES #210] register persistent service instance (#213)
Browse files Browse the repository at this point in the history
- add PersistentInstanceRequest structure
- add register and deregister persistent service method

Close #210
  • Loading branch information
onewe authored Jan 14, 2024
1 parent 75bee4f commit c843196
Show file tree
Hide file tree
Showing 5 changed files with 364 additions and 18 deletions.
2 changes: 2 additions & 0 deletions src/naming/message/request/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
mod batch_instance_request;
mod instance_request;
mod notify_subscriber_request;
mod persistent_instance_request;
mod service_list_request;
mod service_query_request;
mod subscribe_service_request;

pub(crate) use batch_instance_request::*;
pub(crate) use instance_request::*;
pub(crate) use notify_subscriber_request::*;
pub(crate) use persistent_instance_request::*;
pub(crate) use service_list_request::*;
pub(crate) use service_query_request::*;
pub(crate) use subscribe_service_request::*;
85 changes: 85 additions & 0 deletions src/naming/message/request/persistent_instance_request.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use nacos_macro::request;

use crate::{api::naming::ServiceInstance, common::remote::generate_request_id};

#[request(identity = "PersistentInstanceRequest", module = "naming")]
pub(crate) struct PersistentInstanceRequest {
#[serde(rename = "type")]
pub(crate) r_type: String,
pub(crate) instance: ServiceInstance,
}

impl PersistentInstanceRequest {
pub(crate) fn register(
instance: ServiceInstance,
service_name: Option<String>,
namespace: Option<String>,
group_name: Option<String>,
) -> Self {
PersistentInstanceRequest::new(
RequestType::Register,
instance,
service_name,
namespace,
group_name,
)
}

pub(crate) fn deregister(
instance: ServiceInstance,
service_name: Option<String>,
namespace: Option<String>,
group_name: Option<String>,
) -> Self {
PersistentInstanceRequest::new(
RequestType::Deregister,
instance,
service_name,
namespace,
group_name,
)
}

fn new(
request_type: RequestType,
instance: ServiceInstance,
service_name: Option<String>,
namespace: Option<String>,
group_name: Option<String>,
) -> Self {
let request_id = Some(generate_request_id());
Self {
r_type: request_type.request_code(),
instance,
request_id,
namespace,
service_name,
group_name,
..Default::default()
}
}
}

pub(crate) enum RequestType {
Register,
Deregister,
}

impl RequestType {
pub(crate) fn request_code(&self) -> String {
match self {
RequestType::Register => "registerInstance".to_string(),
RequestType::Deregister => "deregisterInstance".to_string(),
}
}
}

impl From<&str> for RequestType {
fn from(code: &str) -> Self {
match code {
"registerInstance" => RequestType::Register,
"deregisterInstance" => RequestType::Deregister,
_ => RequestType::Register,
}
}
}
Loading

0 comments on commit c843196

Please sign in to comment.