Skip to content

Commit

Permalink
Ensure retention is smaller than rate limit
Browse files Browse the repository at this point in the history
  • Loading branch information
dcadenas committed Sep 6, 2024
1 parent 6606a52 commit 3bd0880
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 87 deletions.
6 changes: 4 additions & 2 deletions config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ followers:
google_project_id: "pub-verse-app"
google_topic: "follow-changes"
seconds_threshold: 60
max_messages_per_hour: 2
max_retention_minutes: 60
max_retention_minutes: 60
max_messages_per_rate_period: 2
# Must be bigger than max_retention_minutes
rate_period_minutes: 61
5 changes: 3 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ pub struct Settings {
pub google_project_id: String,
pub google_topic: String,
pub seconds_threshold: u64,
pub max_messages_per_hour: usize,
pub max_retention_minutes: i64,
pub max_retention_minutes: usize,
pub max_messages_per_rate_period: usize,
pub rate_period_minutes: usize,
}

impl Configurable for Settings {
Expand Down
31 changes: 19 additions & 12 deletions src/domain/followee_notification_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ use std::time::Duration;
type Follower = PublicKey;
type Followee = PublicKey;

pub const ONE_HOUR: Duration = Duration::from_secs(60 * 60);

pub enum SendableFollowChange<T: Clock> {
Single(RetainedFollowChange<T>),
Batchable(RetainedFollowChange<T>),
Expand Down Expand Up @@ -65,8 +63,17 @@ pub struct FolloweeNotificationFactory<T: Clock> {
}

impl<T: Clock> FolloweeNotificationFactory<T> {
pub fn new(max_messages_per_hour: usize, max_retention: Duration, clock: T) -> Self {
let rate_counter = RateCounter::new(max_messages_per_hour, ONE_HOUR, clock.clone());
pub fn new(
max_messages_per_rate_period: usize,
rate_period_minutes: Duration,
max_retention: Duration,
clock: T,
) -> Self {
let rate_counter = RateCounter::new(
max_messages_per_rate_period,
rate_period_minutes,
clock.clone(),
);

Self {
rate_counter,
Expand Down Expand Up @@ -167,14 +174,6 @@ impl<T: Clock> FolloweeNotificationFactory<T> {
}
}

impl<T: Clock> Debug for FolloweeNotificationFactory<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("FolloweeNotificationFactory")
.field("follow_changes", &self.follow_changes)
.finish()
}
}

fn collect_sendables<T: Clock>(
max_retention: &Duration,
follow_changes_to_publish: &mut Vec<SendableFollowChange<T>>,
Expand Down Expand Up @@ -220,3 +219,11 @@ fn send<T: Clock>(
follow_changes_to_publish.push(collected_follow_change);
rate_counter.bump();
}

impl<T: Clock> Debug for FolloweeNotificationFactory<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("FolloweeNotificationFactory")
.field("follow_changes", &self.follow_changes)
.finish()
}
}
Loading

0 comments on commit 3bd0880

Please sign in to comment.