Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix(notif): only incr rate limit on success (#1995)
Sean Aye5489b3555f12
diff --git a/rust/cloud-storage/notification/src/domain/ports.rs b/rust/cloud-storage/notification/src/domain/ports.rsindex dae4d034bc..43997bda8b 100644--- a/rust/cloud-storage/notification/src/domain/ports.rs+++ b/rust/cloud-storage/notification/src/domain/ports.rs@@ -50,15 +50,24 @@ pub trait NotificationSender: Send + Sync + 'static {/// Port for rate limiting operations.pub trait RateLimitPort: Send + Sync + 'static {- /// Check if the action is allowed and increment the counter.+ /// Check if the action is allowed without incrementing the counter.////// The `RateLimitKey` is a hashed value - callers control what gets rate/// limited by constructing the key from relevant data.- fn check_and_increment(+ fn check(&self,key: &RateLimitKey,config: &RateLimitConfig,) -> impl Future<Output = Result<RateLimitResult, Report>> + Send;++ /// Increment the rate limit counter for a key.+ ///+ /// Should only be called after a successful action.+ fn increment(+ &self,+ key: &RateLimitKey,+ config: &RateLimitConfig,+ ) -> impl Future<Output = Result<u64, Report>> + Send;}/// Port for notification persistence operations.diff --git a/rust/cloud-storage/notification/src/domain/service/egress.rs b/rust/cloud-storage/notification/src/domain/service/egress.rsindex 342da52297..1344804796 100644--- a/rust/cloud-storage/notification/src/domain/service/egress.rs+++ b/rust/cloud-storage/notification/src/domain/service/egress.rs@@ -204,7 +204,7 @@ where) -> Result<DeliverySuccess, Report<DeliveryFailure>> {let (config, key) = email.rate_limit();- match self.rate_limiter.check_and_increment(key, config).await {+ match self.rate_limiter.check(key, config).await {Ok(RateLimitResult::Exceeded(exceeded)) => {return Err(report!(exceeded).context(DeliveryFailure::RateLimit));}@@ -214,10 +214,25 @@ whereErr(e) => return Err(e.context(DeliveryFailure::Other)),}+ let recipient = email.to();self.email- .send_email(email.to().clone(), &email.content)+ .send_email(recipient.clone(), &email.content).await+ .inspect_err(|e| {+ tracing::error!(+ error = ?e,+ recipient = %recipient,+ subject = %email.content.subject,+ "Email delivery failed"+ );+ }).context(DeliveryFailure::Other)?;++ self.rate_limiter+ .increment(key, config)+ .await+ .context(DeliveryFailure::Other)?;+Ok(DeliverySuccess::Email)}}diff --git a/rust/cloud-storage/notification/src/domain/service/test.rs b/rust/cloud-storage/notification/src/domain/service/test.rsindex 9fdcb8145a..ba9076e4b0 100644--- a/rust/cloud-storage/notification/src/domain/service/test.rs+++ b/rust/cloud-storage/notification/src/domain/service/test.rs@@ -1053,7 +1053,7 @@ impl MockRateLimiter {}impl RateLimitPort for MockRateLimiter {- async fn check_and_increment(+ async fn check(&self,_key: &RateLimitKey,config: &RateLimitConfig,@@ -1068,6 +1068,14 @@ impl RateLimitPort for MockRateLimiter {Ok(RateLimitResult::Allowed { current_count: 1 })}}++ async fn increment(+ &self,+ _key: &RateLimitKey,+ _config: &RateLimitConfig,+ ) -> Result<u64, Report> {+ Ok(1)+ }}/// Mock egress state machine that forwards sends without recording message IDs or batching.diff --git a/rust/cloud-storage/notification/src/outbound/rate_limit.rs b/rust/cloud-storage/notification/src/outbound/rate_limit.rsindex 9e259a9e07..8af08a11e4 100644--- a/rust/cloud-storage/notification/src/outbound/rate_limit.rs+++ b/rust/cloud-storage/notification/src/outbound/rate_limit.rs@@ -64,43 +64,31 @@ impl RedisRateLimitOps for redis::Client {}impl<R: RedisRateLimitOps + Send + Sync + 'static> RateLimitPort for RedisRateLimitAdapter<R> {- async fn check_and_increment(+ async fn check(&self,key: &RateLimitKey,config: &RateLimitConfig,) -> Result<RateLimitResult, Report> {let key_str = format!("rtl:{}", key.to_hex_string());- let expiry_seconds = config.window.as_secs();- // Get current countlet current_count = self.redis.get_count(&key_str).await?.unwrap_or(0);- // Check if already exceededif current_count >= config.max_count {- return Ok(RateLimitResult::Exceeded(RateLimitExceeded {- key: key_str,- current_count,- max_count: config.max_count,- }));- }-- // Increment and set expiry- let new_count = self- .redis- .increment_with_expiry(&key_str, expiry_seconds)- .await?;-- // Check again after increment (in case of race condition)- if new_count > config.max_count {Ok(RateLimitResult::Exceeded(RateLimitExceeded {key: key_str,- current_count: new_count,+ current_count,max_count: config.max_count,}))} else {- Ok(RateLimitResult::Allowed {- current_count: new_count,- })+ Ok(RateLimitResult::Allowed { current_count })}}++ async fn increment(&self, key: &RateLimitKey, config: &RateLimitConfig) -> Result<u64, Report> {+ let key_str = format!("rtl:{}", key.to_hex_string());+ let expiry_seconds = config.window.as_secs();+ self.redis+ .increment_with_expiry(&key_str, expiry_seconds)+ .await+ }}diff --git a/rust/cloud-storage/notification_sandbox/src/adapters/noop_rate_limiter.rs b/rust/cloud-storage/notification_sandbox/src/adapters/noop_rate_limiter.rsindex 481544bc44..93c93ee478 100644--- a/rust/cloud-storage/notification_sandbox/src/adapters/noop_rate_limiter.rs+++ b/rust/cloud-storage/notification_sandbox/src/adapters/noop_rate_limiter.rs@@ -6,11 +6,19 @@ use rootcause::Report;pub struct NoOpRateLimiter;impl RateLimitPort for NoOpRateLimiter {- async fn check_and_increment(+ async fn check(&self,_key: &RateLimitKey,_config: &RateLimitConfig,) -> Result<RateLimitResult, Report> {Ok(RateLimitResult::Allowed { current_count: 0 })}++ async fn increment(+ &self,+ _key: &RateLimitKey,+ _config: &RateLimitConfig,+ ) -> Result<u64, Report> {+ Ok(0)+ }}