Sha256: b382e44a77ce5ab34394ad0383b81f75856f7bf4df745ee3b090e1530e11b967

Contents?: true

Size: 1.24 KB

Versions: 59

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

module Isomorfeus
  module Puppetmaster
    class Server
      class Middleware
        class Counter
          attr_reader :value

          def initialize
            @value = 0
            @mutex = Mutex.new
          end

          def increment
            @mutex.synchronize { @value += 1 }
          end

          def decrement
            @mutex.synchronize { @value -= 1 }
          end
        end

        attr_reader :error

        def initialize(app, extra_middleware = [])
          @app = app
          @extended_app = extra_middleware.inject(@app) do |ex_app, klass|
            klass.new(ex_app)
          end
          @counter = Counter.new
        end

        def pending_requests?
          @counter.value.positive?
        end

        def clear_error
          @error = nil
        end

        def call(env)
          if env['PATH_INFO'] == '/__identify__'
            [200, {}, [@app.object_id.to_s]]
          else
            @counter.increment
            begin
              @extended_app.call(env)
            rescue Exception => err
              @error ||= err
              raise err
            ensure
              @counter.decrement
            end
          end
        end
      end
    end
  end
end

Version data entries

59 entries across 59 versions & 1 rubygems

Version Path
isomorfeus-puppetmaster-0.6.11 lib/isomorfeus/puppetmaster/server/middleware.rb
isomorfeus-puppetmaster-0.6.10 lib/isomorfeus/puppetmaster/server/middleware.rb
isomorfeus-puppetmaster-0.6.9 lib/isomorfeus/puppetmaster/server/middleware.rb
isomorfeus-puppetmaster-0.6.8 lib/isomorfeus/puppetmaster/server/middleware.rb
isomorfeus-puppetmaster-0.6.7 lib/isomorfeus/puppetmaster/server/middleware.rb
isomorfeus-puppetmaster-0.6.6 lib/isomorfeus/puppetmaster/server/middleware.rb
isomorfeus-puppetmaster-0.6.5 lib/isomorfeus/puppetmaster/server/middleware.rb
isomorfeus-puppetmaster-0.6.4 lib/isomorfeus/puppetmaster/server/middleware.rb
isomorfeus-puppetmaster-0.6.3 lib/isomorfeus/puppetmaster/server/middleware.rb
isomorfeus-puppetmaster-0.6.2 lib/isomorfeus/puppetmaster/server/middleware.rb
isomorfeus-puppetmaster-0.6.1 lib/isomorfeus/puppetmaster/server/middleware.rb
isomorfeus-puppetmaster-0.6.0 lib/isomorfeus/puppetmaster/server/middleware.rb
isomorfeus-puppetmaster-0.5.7 lib/isomorfeus/puppetmaster/server/middleware.rb
isomorfeus-puppetmaster-0.5.6 lib/isomorfeus/puppetmaster/server/middleware.rb
isomorfeus-puppetmaster-0.5.5 lib/isomorfeus/puppetmaster/server/middleware.rb
isomorfeus-puppetmaster-0.5.4 lib/isomorfeus/puppetmaster/server/middleware.rb
isomorfeus-puppetmaster-0.5.3 lib/isomorfeus/puppetmaster/server/middleware.rb
isomorfeus-puppetmaster-0.5.2 lib/isomorfeus/puppetmaster/server/middleware.rb
isomorfeus-puppetmaster-0.5.1 lib/isomorfeus/puppetmaster/server/middleware.rb
isomorfeus-puppetmaster-0.5.0 lib/isomorfeus/puppetmaster/server/middleware.rb