Sha256: 974281db2f69eab1e79faf5f6bf62e2f154c7802d02a6d69601639ba1902f70f

Contents?: true

Size: 769 Bytes

Versions: 6

Compression:

Stored size: 769 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 = @min + WorkerKiller.randomize(@max - @min + 1)
      @left = @limit
      @verbose = verbose
    end

    def check
      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

6 entries across 6 versions & 1 rubygems

Version Path
worker_killer-1.0.5.213977 lib/worker_killer/count_limiter.rb
worker_killer-1.0.5.213889 lib/worker_killer/count_limiter.rb
worker_killer-1.0.4.189871 lib/worker_killer/count_limiter.rb
worker_killer-1.0.3.189564 lib/worker_killer/count_limiter.rb
worker_killer-1.0.3.189098 lib/worker_killer/count_limiter.rb
worker_killer-1.0.2.187852 lib/worker_killer/count_limiter.rb