PERF: remove image optimization throttling from Sidekiq

Previously we only allowed one image optimization per machine, this meant there
was cross talk between avatar resizing and Sidekiq. This could lead to large
amounts of starvation when optimized image version changed which in turn could
block the Sidekiq queue.

This increases amount of allowed load on machines but this is preferable to
having crosstalk between avatar resizing and Sidekiq.
This commit is contained in:
Sam 2019-01-04 18:43:53 +11:00
parent 940a61037c
commit 9c91e68351

View File

@ -11,12 +11,20 @@ class OptimizedImage < ActiveRecord::Base
def self.lock(upload_id, width, height)
@hostname ||= `hostname`.strip rescue "unknown"
# note, the extra lock here ensures we only optimize one image per machine
# this can very easily lead to runaway CPU so slowing it down is beneficial
DistributedMutex.synchronize("optimized_image_host_#{@hostname}") do
# note, the extra lock here ensures we only optimize one image per machine on webs
# this can very easily lead to runaway CPU so slowing it down is beneficial and it is hijacked
#
# we can not afford this blocking in Sidekiq cause it can lead to starvation
if Sidekiq.server?
DistributedMutex.synchronize("optimized_image_#{upload_id}_#{width}_#{height}") do
yield
end
else
DistributedMutex.synchronize("optimized_image_host_#{@hostname}") do
DistributedMutex.synchronize("optimized_image_#{upload_id}_#{width}_#{height}") do
yield
end
end
end
end