Sha256: 088b1e959cd0fd53891827cf73ea8492e640b3a6f7bc4429178421eb27e129aa

Contents?: true

Size: 1.53 KB

Versions: 9

Compression:

Stored size: 1.53 KB

Contents

# Copyright (c) 2015 Sqreen. All Rights Reserved.
# Please refer to our terms for more information: https://www.sqreen.com/terms.html

# TODO: => Sqreen::JS:MiniRacer ?

module Sqreen
  module Js
    class ContextPool
      def initialize
        @mutex = Mutex.new
        @total_ctxs = 0
        @contexts = []
      end

      def with_context(&block)
        isolate = context
        begin
          block[isolate]
        ensure
          give_back_context isolate
        end
      end

      private

      def context
        @mutex.synchronize do
          if @contexts.empty?
            @total_ctxs += 1
            Sqreen.log.debug "Creating new V8 context (#{@total_ctxs})"
            SqreenContext.new
          else
            @contexts.pop
          end
        end
      end

      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("Context #{context} had too many close garbage " \
                            'collections; doubling the threshold to ' \
                            "#{context.gc_threshold_in_bytes} bytes")
            context.gc_load = 0
          else
            Sqreen.log.warn("Context #{context} had too many close garbage " \
                            'collections; discarding it')
            context.dispose
            return
          end
        end

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

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
sqreen-1.18.6-java lib/sqreen/js/context_pool.rb
sqreen-1.18.6 lib/sqreen/js/context_pool.rb
sqreen-1.18.5-java lib/sqreen/js/context_pool.rb
sqreen-1.18.5 lib/sqreen/js/context_pool.rb
sqreen-1.18.4-java lib/sqreen/js/context_pool.rb
sqreen-1.18.4 lib/sqreen/js/context_pool.rb
sqreen-1.18.3-java lib/sqreen/js/context_pool.rb
sqreen-1.18.3 lib/sqreen/js/context_pool.rb
sqreen-1.18.3.beta2 lib/sqreen/js/context_pool.rb