-
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.
feat: copy over more so downstream can compile
- Loading branch information
Showing
9 changed files
with
104 additions
and
21 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use rand::Rng as _; | ||
use std::{hash::Hash, sync::Arc}; | ||
use wykies_shared::session::UserSessionInfo; | ||
|
||
/// Distinguishes different types of Websocket services supported | ||
#[derive(Debug, PartialEq, Eq, Clone, Copy, serde::Serialize, serde::Deserialize)] | ||
pub struct WsId(u8); | ||
|
||
impl WsId { | ||
#[cfg(test)] | ||
pub(crate) const TEST1: Self = Self::new(1); | ||
|
||
pub const fn new(value: u8) -> Self { | ||
Self(value) | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
/// Websocket Connection ID | ||
/// Includes user session info as this is not available in the websocket context | ||
/// Only the id is used for hashing and equality checks | ||
pub struct WsConnId { | ||
id: usize, | ||
pub user_info: Arc<UserSessionInfo>, | ||
} | ||
|
||
impl WsConnId { | ||
pub fn new(user_info: Arc<UserSessionInfo>) -> Self { | ||
Self { | ||
id: rand::thread_rng().gen::<usize>(), | ||
user_info, | ||
} | ||
} | ||
} | ||
|
||
impl PartialEq for WsConnId { | ||
fn eq(&self, other: &Self) -> bool { | ||
self.id == other.id | ||
} | ||
} | ||
|
||
impl Eq for WsConnId {} | ||
|
||
impl Hash for WsConnId { | ||
fn hash<H: std::hash::Hasher>(&self, state: &mut H) { | ||
self.id.hash(state); | ||
} | ||
} |
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,9 @@ | ||
//! Provides authenticated access to web socket handlers | ||
//! Provides authentication for web socket handlers | ||
mod errors; | ||
mod id; | ||
mod manager; | ||
|
||
pub use errors::WebSocketError; | ||
pub use errors::WebSocketAuthError; | ||
pub use id::{WsConnId, WsId}; | ||
pub use manager::{validate_ws_connection, AuthTokenManager}; | ||
|
||
/// Distinguishes different types of Websocket services supported | ||
#[derive(Debug, PartialEq, Eq, Clone, Copy, serde::Serialize, serde::Deserialize)] | ||
pub struct WsId(u8); | ||
|
||
impl WsId { | ||
#[cfg(test)] | ||
const TEST1: Self = Self::new(1); | ||
|
||
pub const fn new(value: u8) -> Self { | ||
Self(value) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
mod heartbeat; | ||
|
||
pub use heartbeat::HeartbeatConfig; |
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,37 @@ | ||
use std::{fmt::Display, time::Duration}; | ||
|
||
use crate::WebSocketSettings; | ||
use tracing::instrument; | ||
use wykies_time::Seconds; | ||
|
||
#[derive(Debug, Clone, Copy)] | ||
pub struct HeartbeatConfig { | ||
interval_time: Seconds, | ||
client_timeout: Seconds, | ||
} | ||
|
||
impl HeartbeatConfig { | ||
#[instrument(ret)] | ||
pub fn new(interval_time: Seconds, ws_config: &WebSocketSettings) -> Self { | ||
let times_missed_allowance = ws_config.heartbeat_times_missed_allowance.into(); | ||
let additional_buffer_time = ws_config.heartbeat_additional_buffer_time_secs; | ||
let client_timeout = interval_time * times_missed_allowance + additional_buffer_time; | ||
|
||
Self { | ||
interval_time, | ||
client_timeout, | ||
} | ||
} | ||
|
||
pub fn interval(&self) -> tokio::time::Interval { | ||
tokio::time::interval(self.interval_time.into()) | ||
} | ||
|
||
pub fn client_timeout(&self) -> Duration { | ||
self.client_timeout.into() | ||
} | ||
|
||
pub fn client_timeout_display(&self) -> impl Display { | ||
format!("{} sec", self.client_timeout) | ||
} | ||
} |