Added new session information to the queue.
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Has been cancelled
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Has been cancelled
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
use crate::{
|
||||
action::{Field, MsgAction},
|
||||
name::NameType,
|
||||
queue::data_director::{Include, Path, Route, SessionEntry},
|
||||
queue::data_director::{Include, Path, Route, Session},
|
||||
};
|
||||
use uuid::Uuid;
|
||||
|
||||
@@ -41,7 +41,7 @@ pub struct Message {
|
||||
msg_id: MessageID,
|
||||
action: MsgAction,
|
||||
route: Route,
|
||||
session: SessionEntry,
|
||||
session: Session,
|
||||
}
|
||||
|
||||
impl Message {
|
||||
@@ -62,23 +62,16 @@ impl Message {
|
||||
self.session.id()
|
||||
}
|
||||
|
||||
pub fn override_session(&mut self, session: Session) {
|
||||
self.session = session;
|
||||
}
|
||||
|
||||
pub fn set_session<F>(&self, session: F) -> Self
|
||||
where
|
||||
F: Into<Field>,
|
||||
{
|
||||
/*
|
||||
let sess_id = match session.into() {
|
||||
Field::Uuid(data) => Field::Uuid(data.clone()),
|
||||
Field::StaticString(data) => match Uuid::try_from(data) {
|
||||
Ok(id) => Field::Uuid(id.clone()),
|
||||
Err(_) => Field::None,
|
||||
},
|
||||
_ => Field::None,
|
||||
};
|
||||
*/
|
||||
let mut output = self.clone();
|
||||
// output.session = sess_id;
|
||||
output.session = SessionEntry::new(session);
|
||||
output.session = Session::new(session);
|
||||
output
|
||||
}
|
||||
|
||||
@@ -122,7 +115,7 @@ impl Default for Message {
|
||||
msg_id: MessageID::new(),
|
||||
action: MsgAction::None,
|
||||
route: Route::default(),
|
||||
session: SessionEntry::new(Field::None),
|
||||
session: Session::new(Field::None),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -487,12 +487,12 @@ mod route_storeage {
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct SessionEntry {
|
||||
pub struct Session {
|
||||
id: Field,
|
||||
expire_time: DateTime<Utc>,
|
||||
}
|
||||
|
||||
impl SessionEntry {
|
||||
impl Session {
|
||||
const EXPIRE_IN: Duration = Duration::from_hours(1);
|
||||
|
||||
pub fn new<F>(id: F) -> Self
|
||||
@@ -501,7 +501,7 @@ impl SessionEntry {
|
||||
{
|
||||
Self {
|
||||
id: id.into(),
|
||||
expire_time: Utc::now() + SessionEntry::EXPIRE_IN,
|
||||
expire_time: Utc::now() + Session::EXPIRE_IN,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -510,7 +510,7 @@ impl SessionEntry {
|
||||
}
|
||||
|
||||
fn extend(&mut self) {
|
||||
self.expire_time = Utc::now() + SessionEntry::EXPIRE_IN;
|
||||
self.expire_time = Utc::now() + Session::EXPIRE_IN;
|
||||
}
|
||||
|
||||
fn is_expired(&self) -> bool {
|
||||
@@ -527,9 +527,9 @@ mod session_entries {
|
||||
#[test]
|
||||
fn does_entry_return_id() {
|
||||
let id: Field = Uuid::new_v4().into();
|
||||
let start = Utc::now() + SessionEntry::EXPIRE_IN;
|
||||
let entry = SessionEntry::new(id.clone());
|
||||
let end = Utc::now() + SessionEntry::EXPIRE_IN;
|
||||
let start = Utc::now() + Session::EXPIRE_IN;
|
||||
let entry = Session::new(id.clone());
|
||||
let end = Utc::now() + Session::EXPIRE_IN;
|
||||
assert_eq!(entry.id(), &id);
|
||||
assert!(
|
||||
start < entry.expire_time,
|
||||
@@ -547,7 +547,7 @@ mod session_entries {
|
||||
|
||||
#[test]
|
||||
fn can_determine_if_entry_expired() {
|
||||
let mut entry = SessionEntry::new(Uuid::nil());
|
||||
let mut entry = Session::new(Uuid::nil());
|
||||
let data = Utc::now();
|
||||
entry.expire_time = data + Duration::from_secs(1);
|
||||
assert!(!entry.is_expired(), "entry should not be expired");
|
||||
@@ -557,11 +557,11 @@ mod session_entries {
|
||||
|
||||
#[test]
|
||||
fn can_expiration_be_reset() {
|
||||
let mut entry = SessionEntry::new(Uuid::nil());
|
||||
let mut entry = Session::new(Uuid::nil());
|
||||
entry.expire_time = Utc::now();
|
||||
let start = Utc::now() + SessionEntry::EXPIRE_IN;
|
||||
let start = Utc::now() + Session::EXPIRE_IN;
|
||||
entry.extend();
|
||||
let end = Utc::now() + SessionEntry::EXPIRE_IN;
|
||||
let end = Utc::now() + Session::EXPIRE_IN;
|
||||
assert!(
|
||||
start < entry.expire_time,
|
||||
"{:?} should have been after {:?}",
|
||||
@@ -577,18 +577,18 @@ mod session_entries {
|
||||
}
|
||||
}
|
||||
|
||||
struct Session {
|
||||
entries: HashMap<Field, SessionEntry>,
|
||||
struct SessionStorage {
|
||||
entries: HashMap<Field, Session>,
|
||||
}
|
||||
|
||||
impl Session {
|
||||
impl SessionStorage {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
entries: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn get(&mut self, id: &Field) -> SessionEntry {
|
||||
fn get(&mut self, id: &Field) -> Session {
|
||||
let converted = match id {
|
||||
Field::Uuid(data) => Field::Uuid(data.clone()),
|
||||
Field::StaticString(data) => match Uuid::try_from(data.clone()) {
|
||||
@@ -607,7 +607,7 @@ impl Session {
|
||||
while self.entries.contains_key(&new_id) {
|
||||
new_id = Uuid::new_v4().into();
|
||||
}
|
||||
let output = SessionEntry::new(new_id.clone());
|
||||
let output = Session::new(new_id.clone());
|
||||
self.entries.insert(new_id, output.clone());
|
||||
output
|
||||
}
|
||||
@@ -628,7 +628,7 @@ impl Session {
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod sessions {
|
||||
mod session_storage {
|
||||
use super::*;
|
||||
use crate::FieldType;
|
||||
|
||||
@@ -636,7 +636,7 @@ mod sessions {
|
||||
fn are_session_ids_unique() {
|
||||
let count = 10;
|
||||
let mut ids: Vec<Field> = Vec::new();
|
||||
let mut sess = Session::new();
|
||||
let mut sess = SessionStorage::new();
|
||||
for _ in 0..count {
|
||||
let result = sess.get(&Field::None);
|
||||
let id = result.id().clone();
|
||||
@@ -649,11 +649,11 @@ mod sessions {
|
||||
|
||||
#[test]
|
||||
fn are_valid_uuids_returned() {
|
||||
let mut sess = Session::new();
|
||||
let mut sess = SessionStorage::new();
|
||||
let data = sess.get(&Field::None);
|
||||
let start = Utc::now() + SessionEntry::EXPIRE_IN;
|
||||
let start = Utc::now() + Session::EXPIRE_IN;
|
||||
let result = sess.get(data.id());
|
||||
let end = Utc::now() + SessionEntry::EXPIRE_IN;
|
||||
let end = Utc::now() + Session::EXPIRE_IN;
|
||||
assert_eq!(result.id(), data.id());
|
||||
let entry = sess.entries.get(data.id()).unwrap();
|
||||
assert!(
|
||||
@@ -672,7 +672,7 @@ mod sessions {
|
||||
|
||||
#[test]
|
||||
fn do_bad_ids_generate_new_ids() {
|
||||
let mut sess = Session::new();
|
||||
let mut sess = SessionStorage::new();
|
||||
let data: Field = Uuid::nil().into();
|
||||
let result = sess.get(&data);
|
||||
assert_ne!(result.id(), &data);
|
||||
@@ -680,7 +680,7 @@ mod sessions {
|
||||
|
||||
#[test]
|
||||
fn can_string_ids_be_accepted() {
|
||||
let mut sess = Session::new();
|
||||
let mut sess = SessionStorage::new();
|
||||
let data = sess.get(&Field::None);
|
||||
let id = sess.get(&Field::None).id().clone();
|
||||
let text: Field = match id {
|
||||
@@ -693,7 +693,7 @@ mod sessions {
|
||||
|
||||
#[test]
|
||||
fn does_mismatched_string_produce_new_id() {
|
||||
let mut sess = Session::new();
|
||||
let mut sess = SessionStorage::new();
|
||||
let input = Uuid::nil();
|
||||
let id: Field = input.to_string().into();
|
||||
let test_data: Field = input.into();
|
||||
@@ -703,7 +703,7 @@ mod sessions {
|
||||
|
||||
#[test]
|
||||
fn does_bad_string_produce_id() {
|
||||
let mut sess = Session::new();
|
||||
let mut sess = SessionStorage::new();
|
||||
let id: Field = "not a uuid".into();
|
||||
let result = sess.get(&id);
|
||||
match result.id() {
|
||||
@@ -714,7 +714,7 @@ mod sessions {
|
||||
|
||||
#[test]
|
||||
fn do_other_fields_return_uuid() {
|
||||
let mut sess = Session::new();
|
||||
let mut sess = SessionStorage::new();
|
||||
let id: Field = 2.into();
|
||||
let result = sess.get(&id);
|
||||
match result.id() {
|
||||
@@ -725,9 +725,9 @@ mod sessions {
|
||||
|
||||
#[test]
|
||||
fn are_expired_sessions_removed() {
|
||||
let mut sess = Session::new();
|
||||
let mut sess = SessionStorage::new();
|
||||
let lose = sess.get(&Field::None);
|
||||
sess.entries.get_mut(&lose.id).unwrap().expire_time = Utc::now() - SessionEntry::EXPIRE_IN;
|
||||
sess.entries.get_mut(&lose.id).unwrap().expire_time = Utc::now() - Session::EXPIRE_IN;
|
||||
let keep = sess.get(&Field::None);
|
||||
sess.expire();
|
||||
assert_eq!(
|
||||
@@ -750,6 +750,7 @@ pub struct DocRegistry {
|
||||
queue: Queue,
|
||||
receiver: Receiver<Message>,
|
||||
routes: RouteStorage,
|
||||
sessions: SessionStorage,
|
||||
}
|
||||
|
||||
impl DocRegistry {
|
||||
@@ -759,6 +760,7 @@ impl DocRegistry {
|
||||
queue: queue,
|
||||
receiver: rx,
|
||||
routes: RouteStorage::new(),
|
||||
sessions: SessionStorage::new(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -780,6 +782,8 @@ impl DocRegistry {
|
||||
}
|
||||
_ => match self.path_to_route(&msg.get_path()) {
|
||||
Ok(route) => {
|
||||
let session = self.sessions.get(msg.session_id());
|
||||
msg.override_session(session.clone());
|
||||
msg.set_route(route.clone());
|
||||
for sender_id in self.routes.get(route).iter() {
|
||||
self.queue.forward(sender_id, msg.clone());
|
||||
|
||||
Reference in New Issue
Block a user