From 957a1b17c9178ac8b838dec0e8384a405572c07b Mon Sep 17 00:00:00 2001 From: Tanmay Deep Sharma <32020192+tds-1@users.noreply.github.com> Date: Mon, 23 Feb 2026 15:08:11 +0530 Subject: [PATCH] perf: add default configs for assignment V2 (#13577) ## Description AutoAssignment::RateLimiter#within_limit? returned true early for inboxes without an AssignmentPolicy, bypassing fair distribution entirely and allowing unlimited conversation assignment. ## Type of change - [ ] Bug fix (non-breaking change which fixes an issue) ## Script - Script to enable users with V2 assignment: https://www.notion.so/chatwoot/Script-to-migrate-account-to-assignment-V2-30ca5f274c9280f5b8ecfd15e28eeb9c?source=copy_link ## Checklist: - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my code - [ ] I have commented on my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published in downstream modules --------- Co-authored-by: Shivam Mishra --- app/services/auto_assignment/rate_limiter.rb | 12 ++---------- spec/services/auto_assignment/rate_limiter_spec.rb | 6 +++--- 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/app/services/auto_assignment/rate_limiter.rb b/app/services/auto_assignment/rate_limiter.rb index da8daac9c..b46148526 100644 --- a/app/services/auto_assignment/rate_limiter.rb +++ b/app/services/auto_assignment/rate_limiter.rb @@ -2,8 +2,6 @@ class AutoAssignment::RateLimiter pattr_initialize [:inbox!, :agent!] def within_limit? - return true unless enabled? - current_count < limit end @@ -13,24 +11,18 @@ class AutoAssignment::RateLimiter end def current_count - return 0 unless enabled? - pattern = assignment_key_pattern Redis::Alfred.keys_count(pattern) end private - def enabled? - config.present? && limit.positive? - end - def limit - config&.fair_distribution_limit.present? ? config.fair_distribution_limit.to_i : Float::INFINITY + config&.fair_distribution_limit.present? ? config.fair_distribution_limit.to_i : 5 end def window - config&.fair_distribution_window&.to_i || 24.hours.to_i + config&.fair_distribution_window&.to_i || 5.minutes.to_i end def config diff --git a/spec/services/auto_assignment/rate_limiter_spec.rb b/spec/services/auto_assignment/rate_limiter_spec.rb index d5c3de047..59b145c87 100644 --- a/spec/services/auto_assignment/rate_limiter_spec.rb +++ b/spec/services/auto_assignment/rate_limiter_spec.rb @@ -61,7 +61,7 @@ RSpec.describe AutoAssignment::RateLimiter do it 'still tracks the assignment with default window' do expected_key = format(Redis::RedisKeys::ASSIGNMENT_KEY, inbox_id: inbox.id, agent_id: agent.id, conversation_id: conversation.id) - expect(Redis::Alfred).to receive(:set).with(expected_key, conversation.id.to_s, ex: 24.hours.to_i) + expect(Redis::Alfred).to receive(:set).with(expected_key, conversation.id.to_s, ex: 5.minutes.to_i) rate_limiter.track_assignment(conversation) end end @@ -154,12 +154,12 @@ RSpec.describe AutoAssignment::RateLimiter do allow(inbox).to receive(:assignment_policy).and_return(assignment_policy) end - it 'uses the default window value of 24 hours' do + it 'uses the default window value of 5 minutes' do expected_key = format(Redis::RedisKeys::ASSIGNMENT_KEY, inbox_id: inbox.id, agent_id: agent.id, conversation_id: conversation.id) expect(Redis::Alfred).to receive(:set).with( expected_key, conversation.id.to_s, - ex: 86_400 + ex: 5.minutes.to_i ) rate_limiter.track_assignment(conversation) end