PERF: remove image optimization throttling from Sidekiq

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.

diff --git a/app/models/optimized_image.rb b/app/models/optimized_image.rb
index 5cc2484..2b5f986 100644
--- a/app/models/optimized_image.rb
+++ b/app/models/optimized_image.rb
@@ -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

GitHub sha: 9c91e683