Skip to content

Commit

Permalink
feat: move limits to config
Browse files Browse the repository at this point in the history
  • Loading branch information
YoussefAWasfy committed Oct 10, 2024
1 parent d0d5e36 commit 133eecb
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 7 deletions.
9 changes: 9 additions & 0 deletions affinidi-messaging-mediator/conf/mediator.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ max_queued_messages = "${MAX_QUEUED_MESSAGES:100}"
### Default: 10080 (7 days)
message_expiry_minutes = "${MESSAGE_EXPIRY_MINUTES:10080}"

### max_message_list_limit: Max messages retrieved
### when listing messages from db per request
### Default: 100
max_messages_list = "${MAX_MESSAGES_LIST:100}"

### max_messages_to_delete: Max number of messages to be deleted per request
### Default: 100
max_messages_to_delete = "{MAX_MESSAGES_TO_DELETE:100}"

[security]
### If true (default) SSL is enabled.
### default: true
Expand Down
12 changes: 11 additions & 1 deletion affinidi-messaging-mediator/src/common/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ pub struct DatabaseConfig {
pub max_message_size: String,
pub max_queued_messages: String,
pub message_expiry_minutes: String,
pub max_messages_list: String,
pub max_messages_to_delete: String,
}

/// SecurityConfig Struct contains security related configuration details
Expand Down Expand Up @@ -122,6 +124,8 @@ pub struct Config {
pub max_message_size: u32,
pub max_queued_messages: u32,
pub message_expiry_minutes: u32,
pub max_messages_list: u32,
pub max_messages_to_delete: u32,
pub use_ssl: bool,
pub ssl_certificate_file: String,
pub ssl_key_file: String,
Expand Down Expand Up @@ -153,8 +157,10 @@ impl fmt::Debug for Config {
.field("database_pool_size", &self.database_pool_size)
.field("database_timeout", &self.database_timeout)
.field("max_message_size", &self.max_message_size)
.field("max_queued_messages", &self.max_queued_messages)
.field("max_messages_list", &self.max_messages_list)
.field("message_expiry_minutes", &self.message_expiry_minutes)
.field("max_queued_messages", &self.max_queued_messages)
.field("max_messages_to_delete", &self.max_messages_to_delete)
.field("ssl_certificate_file", &self.ssl_certificate_file)
.field("ssl_key_file", &self.ssl_key_file)
.field("jwt_encoding_key?", &self.jwt_encoding_key.is_some())
Expand Down Expand Up @@ -198,6 +204,8 @@ impl Default for Config {
max_message_size: 1048576,
max_queued_messages: 100,
message_expiry_minutes: 10080,
max_messages_list: 100,
max_messages_to_delete: 100,
use_ssl: true,
ssl_certificate_file: "".into(),
ssl_key_file: "".into(),
Expand Down Expand Up @@ -248,6 +256,8 @@ impl TryFrom<ConfigRaw> for Config {
database_timeout: raw.database.database_timeout.parse().unwrap_or(2),
max_message_size: raw.database.max_message_size.parse().unwrap_or(1048576),
max_queued_messages: raw.database.max_queued_messages.parse().unwrap_or(100),
max_messages_list: raw.database.max_messages_list.parse().unwrap_or(100),
max_messages_to_delete: raw.database.max_messages_to_delete.parse().unwrap_or(100),
message_expiry_minutes: raw.database.message_expiry_minutes.parse().unwrap_or(10080),
use_ssl: raw.security.use_ssl.parse().unwrap_or(true),
ssl_certificate_file: raw.security.ssl_certificate_file,
Expand Down
5 changes: 2 additions & 3 deletions affinidi-messaging-mediator/src/database/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ use crate::common::errors::MediatorError;

use super::DatabaseHandler;

const MAX_MESSAGES_LIMIT: usize = 100;

impl DatabaseHandler {
/// Retrieves list of messages for the specified DID and folder
/// The folder can be either Inbox or Outbox
Expand All @@ -19,6 +17,7 @@ impl DatabaseHandler {
did_hash: &str,
folder: Folder,
range: Option<(&str, &str)>,
limit: u32,
) -> Result<MessageList, MediatorError> {
let _span = span!(
Level::DEBUG,
Expand Down Expand Up @@ -46,7 +45,7 @@ impl DatabaseHandler {
.arg(start)
.arg(end)
.arg("COUNT")
.arg(MAX_MESSAGES_LIMIT)
.arg(limit)
.query_async(&mut conn)
.await
.map_err(|err| {
Expand Down
4 changes: 2 additions & 2 deletions affinidi-messaging-mediator/src/handlers/message_delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
common::errors::{AppError, MediatorError, Session, SuccessResponse},
SharedData,
};
const MAX_MESSAGES_TO_DELETE_LIMIT: usize = 100;

#[derive(Serialize, Deserialize, Debug, Default, Clone)]
pub struct ResponseData {
pub body: String,
Expand All @@ -34,7 +34,7 @@ pub async fn message_delete_handler(
);
async move {
debug!("Deleting ({}) messages", body.message_ids.len());
if body.message_ids.len() > MAX_MESSAGES_TO_DELETE_LIMIT {
if body.message_ids.len() > state.config.max_messages_to_delete.try_into().unwrap() {
return Err(MediatorError::RequestDataError(
session.session_id.clone(),
format!(
Expand Down
2 changes: 1 addition & 1 deletion affinidi-messaging-mediator/src/handlers/message_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub async fn message_list_handler(

let messages = state
.database
.list_messages(&did_hash, folder, None)
.list_messages(&did_hash, folder, None, state.config.max_messages_list)
.await?;

debug!("List contains ({}) messages", messages.len());
Expand Down

0 comments on commit 133eecb

Please sign in to comment.