Sha256: 900fa61d758dbe61181f7debadf96f7ff2bfa1684c2fe90a11a1010add79fa3d

Contents?: true

Size: 864 Bytes

Versions: 3

Compression:

Stored size: 864 Bytes

Contents

require 'worker_killer/memory_limiter'
require 'worker_killer/count_limiter'

module WorkerKiller
  class Middleware

    attr_reader :limiter, :killer, :reaction

    def initialize(app, killer:, klass:, reaction: nil, **opts)
      @app = app
      @killer = killer

      @reaction = reaction || proc do |l, k|
        k.kill(l.started_at)
      end

      @limiter = klass.new(opts)
    end

    def call(env)
      @app.call(env)
    ensure
      reaction.call(limiter, killer) if limiter.check
    end

    class RequestsLimiter < ::WorkerKiller::Middleware

      def initialize(app, **opts)
        super(app, klass: ::WorkerKiller::CountLimiter, **opts)
      end

    end

    class OOMLimiter < ::WorkerKiller::Middleware

      def initialize(app, **opts)
        super(app, klass: ::WorkerKiller::MemoryLimiter, **opts)
      end

    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
worker_killer-1.0.1.39842 lib/worker_killer/middleware.rb
worker_killer-1.0.0.39841 lib/worker_killer/middleware.rb
worker_killer-1.0.0.39839 lib/worker_killer/middleware.rb