class Sqreen::Js::ContextPool

Public Class Methods

new() click to toggle source
# File lib/sqreen/js/context_pool.rb, line 11
def initialize
  @mutex = Mutex.new
  @total_ctxs = 0
  @contexts = []
end

Public Instance Methods

with_context(&block) click to toggle source
# File lib/sqreen/js/context_pool.rb, line 17
def with_context(&block)
  isolate = context
  begin
    block[isolate]
  ensure
    give_back_context isolate
  end
end

Private Instance Methods

context() click to toggle source
# File lib/sqreen/js/context_pool.rb, line 28
def context
  @mutex.synchronize do
    if @contexts.empty?
      @total_ctxs += 1
      Sqreen.log.debug { "js:context_pool action:spawn count:#{@total_ctxs}" }
      SqreenContext.new
    else
      Sqreen.log.debug { "js:context_pool action:pop count:#{@total_ctxs}" }
      @contexts.pop
    end
  end
end
give_back_context(context) click to toggle source
# File lib/sqreen/js/context_pool.rb, line 41
def give_back_context(context)
  context.possibly_gc

  if context.gc_load > 30
    if context.gc_threshold_in_bytes == DEFAULT_GC_THRESHOLD
      context.gc_threshold_in_bytes *= 2
      Sqreen.log.warn { "js:context action:increase reason:gc threshold:#{context.gc_threshold_in_bytes}" }
      context.gc_load = 0
    else
      Sqreen.log.warn { "js:context action:discard reason:gc threshold:#{context.gc_threshold_in_bytes}" }

      Sqreen.log.debug { "js:context_pool action:drop reason:gc count:#{@total_ctxs}" }
      context.dispose
      return
    end
  end

  Sqreen.log.debug { "js:context_pool action:push count:#{@total_ctxs}" }

  @mutex.synchronize { @contexts.push(context); }
end