Sha256: b7def077a8e898f6b2d687f5cbdd2724dffd449bb3a2ce8c643a2f15e86d35b6

Contents?: true

Size: 1.68 KB

Versions: 22

Compression:

Stored size: 1.68 KB

Contents

module BBK
  module App
    module Middlewares
      class SelfKiller


        SELF_KILLER_LOG_INTERVAL = 300

        attr_reader :dispatcher, :count, :threshold, :stop_time

        def initialize(dispatcher, delay: 10 * 60, threshold: 10_000, logger: ::Logger.new(STDOUT))
          @dispatcher = dispatcher
          @threshold = threshold
          @count = 0
          @stop_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) + delay
          @stopping = false
          @logger = logger
          @logger.info "[SelfKiller] Initializing: #{@count}/#{@threshold}"
          reset_log_timer
        end

        def build(app)
          @app = app
          self
        end

        def call(msg)
          @count += 1
          if time_exceed?(@log_timer)
            @logger.info "[SelfKiller] Threshold status: #{@count}/#{@threshold}, delayed: #{!time_exceed?}"
            reset_log_timer
          end
          close_dispatcher if stop?

          @app.call(msg)
        end

        protected

          def reset_log_timer
            @log_timer = Process.clock_gettime(Process::CLOCK_MONOTONIC) + SELF_KILLER_LOG_INTERVAL
          end

          def stop?
            !@stopping && threshold_exceed? && time_exceed?
          end

          def threshold_exceed?
            @count > @threshold
          end

          def time_exceed?(time = @stop_time)
            Process.clock_gettime(Process::CLOCK_MONOTONIC) > time
          end

          def close_dispatcher
            @stopping = true
            @logger.warn '[SelfKiller] Threshold exceeded, closing dispatcher...'
            Thread.new { @dispatcher.close } # Don't block current call
          end

      end
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
bbk-app-1.1.1.273627 lib/bbk/app/middlewares/self_killer.rb
bbk-app-1.1.1.273338 lib/bbk/app/middlewares/self_killer.rb
bbk-app-1.1.1.273312 lib/bbk/app/middlewares/self_killer.rb
bbk-app-1.1.1.273294 lib/bbk/app/middlewares/self_killer.rb
bbk-app-1.1.0.219406 lib/bbk/app/middlewares/self_killer.rb
bbk-app-1.1.0.216998 lib/bbk/app/middlewares/self_killer.rb
bbk-app-1.1.0.204569 lib/bbk/app/middlewares/self_killer.rb
bbk-app-1.1.0.200751 lib/bbk/app/middlewares/self_killer.rb
bbk-app-1.1.0.200186 lib/bbk/app/middlewares/self_killer.rb
bbk-app-1.1.0.199675 lib/bbk/app/middlewares/self_killer.rb
bbk-app-1.1.0.199604 lib/bbk/app/middlewares/self_killer.rb
bbk-app-1.1.0.199389 lib/bbk/app/middlewares/self_killer.rb
bbk-app-1.1.0.199383 lib/bbk/app/middlewares/self_killer.rb
bbk-app-1.0.0.152254 lib/bbk/app/middlewares/self_killer.rb
bbk-app-1.0.0.141716 lib/bbk/app/middlewares/self_killer.rb
bbk-app-1.0.0.80957 lib/bbk/app/middlewares/self_killer.rb
bbk-app-1.0.0.79514 lib/bbk/app/middlewares/self_killer.rb
bbk-app-1.0.0.79241 lib/bbk/app/middlewares/self_killer.rb
bbk-app-1.0.0.78020 lib/bbk/app/middlewares/self_killer.rb
bbk-app-1.0.0.72920 lib/bbk/app/middlewares/self_killer.rb