Sha256: c3c4b4deff827203df4e2e5256bcf1ae22c275b8077367d25c64a9a00a24b3cd

Contents?: true

Size: 879 Bytes

Versions: 3

Compression:

Stored size: 879 Bytes

Contents

module WorkerKiller
  class CountLimiter

    attr_reader :min, :max, :left, :limit, :started_at

    def initialize(min: 3072, max: 4096, verbose: false)
      @min = min
      @max = max
      @limit = nil
      @left = nil
      @verbose = verbose
    end

    def check
      # initialize limits on first check
      if @limit.nil?
        @limit = min + WorkerKiller.randomize(max - min + 1)
        @left = @limit
      end

      return nil if @limit < 1

      @started_at ||= Time.now

      if @verbose
        logger.info "#{self.class}: worker (pid: #{Process.pid}) has #{@left} left before being limited"
      end

      return false if (@left -= 1) > 0

      logger.warn "#{self.class}: worker (pid: #{Process.pid}) exceeds max number of requests (limit: #{@limit})"

      true
    end

    def logger
      WorkerKiller.configuration.logger
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
worker_killer-1.1.0.223443 lib/worker_killer/count_limiter.rb
worker_killer-1.1.0.214159 lib/worker_killer/count_limiter.rb
worker_killer-1.1.0.214146 lib/worker_killer/count_limiter.rb