-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
150 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use std::collections::BTreeMap; | ||
|
||
use my_no_sql_server_core::rust_extensions::date_time::DateTimeAsMicroseconds; | ||
use tokio::sync::Mutex; | ||
|
||
#[derive(Debug)] | ||
pub struct WriterInfo { | ||
pub version: String, | ||
pub last_ping: DateTimeAsMicroseconds, | ||
} | ||
|
||
pub struct HttpWriters { | ||
data: Mutex<BTreeMap<String, WriterInfo>>, | ||
} | ||
|
||
impl HttpWriters { | ||
pub fn new() -> Self { | ||
Self { | ||
data: Mutex::new(BTreeMap::new()), | ||
} | ||
} | ||
|
||
pub async fn update(&self, name: &str, version: &str, now: DateTimeAsMicroseconds) { | ||
let mut data = self.data.lock().await; | ||
match data.get_mut(name) { | ||
Some(writer_info) => { | ||
writer_info.last_ping = now; | ||
if writer_info.version != version { | ||
writer_info.version = version.to_string(); | ||
} | ||
} | ||
None => { | ||
data.insert( | ||
name.to_string(), | ||
WriterInfo { | ||
version: version.to_string(), | ||
last_ping: now, | ||
}, | ||
); | ||
} | ||
} | ||
} | ||
|
||
pub async fn get<TResult>( | ||
&self, | ||
convert: impl Fn(&str, &WriterInfo) -> TResult, | ||
) -> Vec<TResult> { | ||
let data = self.data.lock().await; | ||
|
||
let mut result = Vec::with_capacity(data.len()); | ||
|
||
for (key, itm) in data.iter() { | ||
let itm = convert(key, itm); | ||
result.push(itm); | ||
} | ||
|
||
result | ||
} | ||
|
||
pub async fn gc(&self, now: DateTimeAsMicroseconds) { | ||
let mut data = self.data.lock().await; | ||
let mut to_remove = Vec::new(); | ||
for (name, writer_info) in data.iter() { | ||
if now.duration_since(writer_info.last_ping).get_full_minutes() > 1 { | ||
to_remove.push(name.clone()); | ||
} | ||
} | ||
|
||
for name in to_remove { | ||
data.remove(&name); | ||
} | ||
} | ||
} |
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,20 +1,52 @@ | ||
use my_http_server::{macros::http_route, HttpContext, HttpFailResult, HttpOkResult, HttpOutput}; | ||
use std::sync::Arc; | ||
|
||
use my_http_server::{ | ||
macros::{http_route, MyHttpInput}, | ||
HttpContext, HttpFailResult, HttpOkResult, HttpOutput, | ||
}; | ||
use my_no_sql_server_core::rust_extensions::date_time::DateTimeAsMicroseconds; | ||
|
||
use crate::app::AppContext; | ||
|
||
#[http_route( | ||
method: "GET", | ||
route: "/api/Ping", | ||
controller: "Monitoring", | ||
description: "Endpoint to ping the service", | ||
summary: "Endpoint to ping the service", | ||
input_data: PingHttpInputModel, | ||
result:[ | ||
{status_code: 204, description: "Ok result"}, | ||
] | ||
)] | ||
pub struct PingAction; | ||
pub struct PingAction { | ||
app: Arc<AppContext>, | ||
} | ||
|
||
impl PingAction { | ||
pub fn new(app: Arc<AppContext>) -> Self { | ||
Self { app } | ||
} | ||
} | ||
|
||
async fn handle_request( | ||
_: &PingAction, | ||
action: &PingAction, | ||
input_data: PingHttpInputModel, | ||
_ctx: &mut HttpContext, | ||
) -> Result<HttpOkResult, HttpFailResult> { | ||
let now = DateTimeAsMicroseconds::now(); | ||
action | ||
.app | ||
.http_writers | ||
.update(&input_data.name, &input_data.version, now) | ||
.await; | ||
HttpOutput::Empty.into_ok_result(false).into() | ||
} | ||
|
||
#[derive(Debug, MyHttpInput)] | ||
pub struct PingHttpInputModel { | ||
#[http_query(name = "clientName", description = "Client Name")] | ||
pub name: String, | ||
#[http_query(name = "clientVersion", description = "Client Version")] | ||
pub version: String, | ||
} |
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