Class: BBK::App::Middlewares::SelfKiller
- Inherits:
-
Object
- Object
- BBK::App::Middlewares::SelfKiller
- Defined in:
- lib/bbk/app/middlewares/self_killer.rb,
sig/bbk/app/middlewares/self_killer.rbs
Constant Summary collapse
- SELF_KILLER_LOG_INTERVAL =
300
Instance Attribute Summary collapse
-
#count ⇒ Integer
readonly
Returns the value of attribute count.
-
#dispatcher ⇒ Object
readonly
Returns the value of attribute dispatcher.
-
#stop_time ⇒ Integer, Float
readonly
Returns the value of attribute stop_time.
-
#threshold ⇒ Integer
readonly
Returns the value of attribute threshold.
Instance Method Summary collapse
- #build(app) ⇒ void
- #call(msg) ⇒ void
- #close_dispatcher ⇒ void
-
#initialize(dispatcher, delay: 10 * 60, threshold: 10_000, logger: ::Logger.new(STDOUT)) ⇒ SelfKiller
constructor
A new instance of SelfKiller.
- #reset_log_time ⇒ void
- #stop? ⇒ Boolean
- #threshold_exceed? ⇒ Boolean
- #time_exceed?(time = @stop_time) ⇒ Boolean
Constructor Details
#initialize(dispatcher, delay: 10 * 60, threshold: 10_000, logger: ::Logger.new(STDOUT)) ⇒ SelfKiller
Returns a new instance of SelfKiller.
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/bbk/app/middlewares/self_killer.rb', line 11 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 |
Instance Attribute Details
#count ⇒ Integer (readonly)
Returns the value of attribute count.
9 10 11 |
# File 'lib/bbk/app/middlewares/self_killer.rb', line 9 def count @count end |
#dispatcher ⇒ Object (readonly)
Returns the value of attribute dispatcher.
9 10 11 |
# File 'lib/bbk/app/middlewares/self_killer.rb', line 9 def dispatcher @dispatcher end |
#stop_time ⇒ Integer, Float (readonly)
Returns the value of attribute stop_time.
9 10 11 |
# File 'lib/bbk/app/middlewares/self_killer.rb', line 9 def stop_time @stop_time end |
#threshold ⇒ Integer (readonly)
Returns the value of attribute threshold.
9 10 11 |
# File 'lib/bbk/app/middlewares/self_killer.rb', line 9 def threshold @threshold end |
Instance Method Details
#build(app) ⇒ void
This method returns an undefined value.
22 23 24 25 |
# File 'lib/bbk/app/middlewares/self_killer.rb', line 22 def build(app) @app = app self end |
#call(msg) ⇒ void
This method returns an undefined value.
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/bbk/app/middlewares/self_killer.rb', line 27 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 |
#close_dispatcher ⇒ void
This method returns an undefined value.
56 57 58 59 60 |
# File 'lib/bbk/app/middlewares/self_killer.rb', line 56 def close_dispatcher @stopping = true @logger.warn '[SelfKiller] Threshold exceeded, closing dispatcher...' Thread.new { @dispatcher.close } # Don't block current call end |
#reset_log_time ⇒ void
This method returns an undefined value.
17 |
# File 'sig/bbk/app/middlewares/self_killer.rbs', line 17
def reset_log_time: () -> void
|
#stop? ⇒ Boolean
44 45 46 |
# File 'lib/bbk/app/middlewares/self_killer.rb', line 44 def stop? !@stopping && threshold_exceed? && time_exceed? end |
#threshold_exceed? ⇒ Boolean
48 49 50 |
# File 'lib/bbk/app/middlewares/self_killer.rb', line 48 def threshold_exceed? @count > @threshold end |
#time_exceed?(time = @stop_time) ⇒ Boolean
52 53 54 |
# File 'lib/bbk/app/middlewares/self_killer.rb', line 52 def time_exceed?(time = @stop_time) Process.clock_gettime(Process::CLOCK_MONOTONIC) > time end |