Added new session information to the queue.
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Has been cancelled

This commit is contained in:
2026-05-03 11:55:43 -04:00
parent db841643e4
commit 6d53ddd0be
2 changed files with 40 additions and 43 deletions

View File

@@ -1,7 +1,7 @@
use crate::{ use crate::{
action::{Field, MsgAction}, action::{Field, MsgAction},
name::NameType, name::NameType,
queue::data_director::{Include, Path, Route, SessionEntry}, queue::data_director::{Include, Path, Route, Session},
}; };
use uuid::Uuid; use uuid::Uuid;
@@ -41,7 +41,7 @@ pub struct Message {
msg_id: MessageID, msg_id: MessageID,
action: MsgAction, action: MsgAction,
route: Route, route: Route,
session: SessionEntry, session: Session,
} }
impl Message { impl Message {
@@ -62,23 +62,16 @@ impl Message {
self.session.id() self.session.id()
} }
pub fn override_session(&mut self, session: Session) {
self.session = session;
}
pub fn set_session<F>(&self, session: F) -> Self pub fn set_session<F>(&self, session: F) -> Self
where where
F: Into<Field>, 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(); let mut output = self.clone();
// output.session = sess_id; output.session = Session::new(session);
output.session = SessionEntry::new(session);
output output
} }
@@ -122,7 +115,7 @@ impl Default for Message {
msg_id: MessageID::new(), msg_id: MessageID::new(),
action: MsgAction::None, action: MsgAction::None,
route: Route::default(), route: Route::default(),
session: SessionEntry::new(Field::None), session: Session::new(Field::None),
} }
} }
} }

View File

@@ -487,12 +487,12 @@ mod route_storeage {
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct SessionEntry { pub struct Session {
id: Field, id: Field,
expire_time: DateTime<Utc>, expire_time: DateTime<Utc>,
} }
impl SessionEntry { impl Session {
const EXPIRE_IN: Duration = Duration::from_hours(1); const EXPIRE_IN: Duration = Duration::from_hours(1);
pub fn new<F>(id: F) -> Self pub fn new<F>(id: F) -> Self
@@ -501,7 +501,7 @@ impl SessionEntry {
{ {
Self { Self {
id: id.into(), 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) { 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 { fn is_expired(&self) -> bool {
@@ -527,9 +527,9 @@ mod session_entries {
#[test] #[test]
fn does_entry_return_id() { fn does_entry_return_id() {
let id: Field = Uuid::new_v4().into(); let id: Field = Uuid::new_v4().into();
let start = Utc::now() + SessionEntry::EXPIRE_IN; let start = Utc::now() + Session::EXPIRE_IN;
let entry = SessionEntry::new(id.clone()); let entry = Session::new(id.clone());
let end = Utc::now() + SessionEntry::EXPIRE_IN; let end = Utc::now() + Session::EXPIRE_IN;
assert_eq!(entry.id(), &id); assert_eq!(entry.id(), &id);
assert!( assert!(
start < entry.expire_time, start < entry.expire_time,
@@ -547,7 +547,7 @@ mod session_entries {
#[test] #[test]
fn can_determine_if_entry_expired() { fn can_determine_if_entry_expired() {
let mut entry = SessionEntry::new(Uuid::nil()); let mut entry = Session::new(Uuid::nil());
let data = Utc::now(); let data = Utc::now();
entry.expire_time = data + Duration::from_secs(1); entry.expire_time = data + Duration::from_secs(1);
assert!(!entry.is_expired(), "entry should not be expired"); assert!(!entry.is_expired(), "entry should not be expired");
@@ -557,11 +557,11 @@ mod session_entries {
#[test] #[test]
fn can_expiration_be_reset() { fn can_expiration_be_reset() {
let mut entry = SessionEntry::new(Uuid::nil()); let mut entry = Session::new(Uuid::nil());
entry.expire_time = Utc::now(); entry.expire_time = Utc::now();
let start = Utc::now() + SessionEntry::EXPIRE_IN; let start = Utc::now() + Session::EXPIRE_IN;
entry.extend(); entry.extend();
let end = Utc::now() + SessionEntry::EXPIRE_IN; let end = Utc::now() + Session::EXPIRE_IN;
assert!( assert!(
start < entry.expire_time, start < entry.expire_time,
"{:?} should have been after {:?}", "{:?} should have been after {:?}",
@@ -577,18 +577,18 @@ mod session_entries {
} }
} }
struct Session { struct SessionStorage {
entries: HashMap<Field, SessionEntry>, entries: HashMap<Field, Session>,
} }
impl Session { impl SessionStorage {
fn new() -> Self { fn new() -> Self {
Self { Self {
entries: HashMap::new(), entries: HashMap::new(),
} }
} }
fn get(&mut self, id: &Field) -> SessionEntry { fn get(&mut self, id: &Field) -> Session {
let converted = match id { let converted = match id {
Field::Uuid(data) => Field::Uuid(data.clone()), Field::Uuid(data) => Field::Uuid(data.clone()),
Field::StaticString(data) => match Uuid::try_from(data.clone()) { Field::StaticString(data) => match Uuid::try_from(data.clone()) {
@@ -607,7 +607,7 @@ impl Session {
while self.entries.contains_key(&new_id) { while self.entries.contains_key(&new_id) {
new_id = Uuid::new_v4().into(); 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()); self.entries.insert(new_id, output.clone());
output output
} }
@@ -628,7 +628,7 @@ impl Session {
} }
#[cfg(test)] #[cfg(test)]
mod sessions { mod session_storage {
use super::*; use super::*;
use crate::FieldType; use crate::FieldType;
@@ -636,7 +636,7 @@ mod sessions {
fn are_session_ids_unique() { fn are_session_ids_unique() {
let count = 10; let count = 10;
let mut ids: Vec<Field> = Vec::new(); let mut ids: Vec<Field> = Vec::new();
let mut sess = Session::new(); let mut sess = SessionStorage::new();
for _ in 0..count { for _ in 0..count {
let result = sess.get(&Field::None); let result = sess.get(&Field::None);
let id = result.id().clone(); let id = result.id().clone();
@@ -649,11 +649,11 @@ mod sessions {
#[test] #[test]
fn are_valid_uuids_returned() { fn are_valid_uuids_returned() {
let mut sess = Session::new(); let mut sess = SessionStorage::new();
let data = sess.get(&Field::None); 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 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()); assert_eq!(result.id(), data.id());
let entry = sess.entries.get(data.id()).unwrap(); let entry = sess.entries.get(data.id()).unwrap();
assert!( assert!(
@@ -672,7 +672,7 @@ mod sessions {
#[test] #[test]
fn do_bad_ids_generate_new_ids() { fn do_bad_ids_generate_new_ids() {
let mut sess = Session::new(); let mut sess = SessionStorage::new();
let data: Field = Uuid::nil().into(); let data: Field = Uuid::nil().into();
let result = sess.get(&data); let result = sess.get(&data);
assert_ne!(result.id(), &data); assert_ne!(result.id(), &data);
@@ -680,7 +680,7 @@ mod sessions {
#[test] #[test]
fn can_string_ids_be_accepted() { fn can_string_ids_be_accepted() {
let mut sess = Session::new(); let mut sess = SessionStorage::new();
let data = sess.get(&Field::None); let data = sess.get(&Field::None);
let id = sess.get(&Field::None).id().clone(); let id = sess.get(&Field::None).id().clone();
let text: Field = match id { let text: Field = match id {
@@ -693,7 +693,7 @@ mod sessions {
#[test] #[test]
fn does_mismatched_string_produce_new_id() { fn does_mismatched_string_produce_new_id() {
let mut sess = Session::new(); let mut sess = SessionStorage::new();
let input = Uuid::nil(); let input = Uuid::nil();
let id: Field = input.to_string().into(); let id: Field = input.to_string().into();
let test_data: Field = input.into(); let test_data: Field = input.into();
@@ -703,7 +703,7 @@ mod sessions {
#[test] #[test]
fn does_bad_string_produce_id() { fn does_bad_string_produce_id() {
let mut sess = Session::new(); let mut sess = SessionStorage::new();
let id: Field = "not a uuid".into(); let id: Field = "not a uuid".into();
let result = sess.get(&id); let result = sess.get(&id);
match result.id() { match result.id() {
@@ -714,7 +714,7 @@ mod sessions {
#[test] #[test]
fn do_other_fields_return_uuid() { fn do_other_fields_return_uuid() {
let mut sess = Session::new(); let mut sess = SessionStorage::new();
let id: Field = 2.into(); let id: Field = 2.into();
let result = sess.get(&id); let result = sess.get(&id);
match result.id() { match result.id() {
@@ -725,9 +725,9 @@ mod sessions {
#[test] #[test]
fn are_expired_sessions_removed() { fn are_expired_sessions_removed() {
let mut sess = Session::new(); let mut sess = SessionStorage::new();
let lose = sess.get(&Field::None); 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); let keep = sess.get(&Field::None);
sess.expire(); sess.expire();
assert_eq!( assert_eq!(
@@ -750,6 +750,7 @@ pub struct DocRegistry {
queue: Queue, queue: Queue,
receiver: Receiver<Message>, receiver: Receiver<Message>,
routes: RouteStorage, routes: RouteStorage,
sessions: SessionStorage,
} }
impl DocRegistry { impl DocRegistry {
@@ -759,6 +760,7 @@ impl DocRegistry {
queue: queue, queue: queue,
receiver: rx, receiver: rx,
routes: RouteStorage::new(), routes: RouteStorage::new(),
sessions: SessionStorage::new(),
} }
} }
@@ -780,6 +782,8 @@ impl DocRegistry {
} }
_ => match self.path_to_route(&msg.get_path()) { _ => match self.path_to_route(&msg.get_path()) {
Ok(route) => { Ok(route) => {
let session = self.sessions.get(msg.session_id());
msg.override_session(session.clone());
msg.set_route(route.clone()); msg.set_route(route.clone());
for sender_id in self.routes.get(route).iter() { for sender_id in self.routes.get(route).iter() {
self.queue.forward(sender_id, msg.clone()); self.queue.forward(sender_id, msg.clone());