Sha256: 026a0bd122037a180d66472494d1a578658e059bcf24417584592f0b80f245aa

Contents?: true

Size: 1.4 KB

Versions: 5

Compression:

Stored size: 1.4 KB

Contents

module FaaStRuby
  class ConcurrencyController
    def self.store
      @@store ||= {} 
    end
    attr_accessor :params, :name, :max, :type
    def initialize(name, max: 1, type:)
      @type = type
      @name = name
      @max = max
      @running = 0
      # @mutex = Mutex.new
      self.class.store[name] = self
      puts "[ConcurrencyController] Started controller for '#{name}' with max_concurrency = #{@max}".yellow
    end
  
    def running
      # puts "[ConcurrencyController] [#{name}] Reading runners".red
      # wait
      # puts "[ConcurrencyController] [#{name}] Locking mutex".red
      # @mutex.lock
      @running
    # ensure
      # puts "[ConcurrencyController] [#{name}] Unlocking mutex".red
      # @mutex.unlock
    end

    def decr(amount = 1)
      incr(0 - amount)
    end
  
    def incr(amount = 1)
      # puts "[ConcurrencyController] [#{name}] Incr #{amount}".red
      # wait
      # puts "[ConcurrencyController] [#{name}] Locking mutex".red
      # @mutex.lock
      current = @running + amount
      return nil if max < current
      @running += amount
    # ensure
    #   puts "[ConcurrencyController] [#{name}] Unlocking mutex".red
    #   @mutex.unlock
    end
  
    # def wait
    #   puts "[ConcurrencyController] [#{name}] Waiting for mutex lock to release".red
    #   while @mutex.locked? do;end
    #   puts "[ConcurrencyController] [#{name}] Mutex released".red
    # end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
faastruby-0.4.18 lib/faastruby/server/concurrency_controller.rb
faastruby-0.4.17 lib/faastruby/server/concurrency_controller.rb
faastruby-0.4.16 lib/faastruby/server/concurrency_controller.rb
faastruby-0.4.15 lib/faastruby/server/concurrency_controller.rb
faastruby-0.4.14 lib/faastruby/server/concurrency_controller.rb