PERF: Reduce memory footprint of Chat::AutoRemove::HandleCategoryUpdated (#28332)

This commit seeks to reduce the memory footprint of `Chat::AutoRemove::HandleCategoryUpdated.call`
by optimizing the
`Chat::AutoRemove::HandleCategoryUpdated#remove_users_without_channel_permission` method which was
loading all the ActiveRecord users objects into memory at once. This
change updates the method call to load the ActiveRecord user objects in
batches instead.
This commit is contained in:
Alan Guo Xiang Tan 2024-08-14 09:30:36 +08:00 committed by GitHub
parent 106406b8a4
commit ed11ee9d05
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -59,11 +59,16 @@ module Chat
end
def remove_users_without_channel_permission(users:, category_channel_ids:)
memberships_to_remove =
Chat::Action::CalculateMembershipsForRemoval.call(
scoped_users: users,
channel_ids: category_channel_ids,
memberships_to_remove = []
users.find_in_batches do |batch_users|
memberships_to_remove.concat(
Chat::Action::CalculateMembershipsForRemoval.call(
scoped_users: batch_users,
channel_ids: category_channel_ids,
),
)
end
return if memberships_to_remove.blank?