Sha256: 31b49271c3b90e33ada891f6cbc7939ae53e3896810539e9a2916638399b6b70

Contents?: true

Size: 1.08 KB

Versions: 70

Compression:

Stored size: 1.08 KB

Contents

require 'thread'

module Celluloid
  class Actor
    # Maintain a thread pool of actors FOR SPEED!!
    class Pool
      @pool = []
      @lock = Mutex.new
      @max_idle = 16

      class << self
        attr_accessor :max_idle

        def get(&block)
          @lock.synchronize do
            if @pool.empty?
              thread = create
            else
              thread = @pool.shift
            end

            thread[:queue] << block
            thread
          end
        end

        def put(thread)
          @lock.synchronize do
            if @pool.size >= @max_idle
              thread[:queue] << nil
            else
              @pool << thread
            end
          end
        end

        def create
          queue = Queue.new
          thread = Thread.new do
            begin
              while func = queue.pop
                func.call
              end
            rescue Exception => ex
              Logger.crash("#{self} internal failure", ex)
            end
          end
          thread[:queue] = queue
          thread
        end
      end
    end
  end
end

Version data entries

70 entries across 70 versions & 2 rubygems

Version Path
engineyard-serverside-2.0.4 lib/vendor/celluloid/lib/celluloid/actor_pool.rb
engineyard-serverside-1.6.5 lib/vendor/celluloid/lib/celluloid/actor_pool.rb
engineyard-serverside-2.0.3 lib/vendor/celluloid/lib/celluloid/actor_pool.rb
engineyard-serverside-2.0.2 lib/vendor/celluloid/lib/celluloid/actor_pool.rb
engineyard-serverside-2.0.1 lib/vendor/celluloid/lib/celluloid/actor_pool.rb
engineyard-serverside-2.0.0 lib/vendor/celluloid/lib/celluloid/actor_pool.rb
engineyard-serverside-2.0.0.rc1 lib/vendor/celluloid/lib/celluloid/actor_pool.rb
engineyard-serverside-2.0.0.pre5 lib/vendor/celluloid/lib/celluloid/actor_pool.rb
engineyard-serverside-2.0.0.pre4 lib/vendor/celluloid/lib/celluloid/actor_pool.rb
engineyard-serverside-2.0.0.pre3 lib/vendor/celluloid/lib/celluloid/actor_pool.rb
engineyard-serverside-2.0.0.pre2 lib/vendor/celluloid/lib/celluloid/actor_pool.rb
engineyard-serverside-2.0.0.pre1 lib/vendor/celluloid/lib/celluloid/actor_pool.rb
engineyard-serverside-1.7.0.pre2 lib/vendor/celluloid/lib/celluloid/actor_pool.rb
engineyard-serverside-1.6.4 lib/vendor/celluloid/lib/celluloid/actor_pool.rb
engineyard-serverside-1.6.4.pre6 lib/vendor/celluloid/lib/celluloid/actor_pool.rb
engineyard-serverside-1.6.4.pre5 lib/vendor/celluloid/lib/celluloid/actor_pool.rb
engineyard-serverside-1.6.4.pre4 lib/vendor/celluloid/lib/celluloid/actor_pool.rb
engineyard-serverside-1.6.4.pre3 lib/vendor/celluloid/lib/celluloid/actor_pool.rb
engineyard-serverside-1.6.4.pre2 lib/vendor/celluloid/lib/celluloid/actor_pool.rb
engineyard-serverside-1.6.4.pre lib/vendor/celluloid/lib/celluloid/actor_pool.rb