Sha256: 2f8715d2789d5d84e3568ecd049db52c93ae1d293190f245df79034cdf57cefe

Contents?: true

Size: 1.55 KB

Versions: 29

Compression:

Stored size: 1.55 KB

Contents

# typed: ignore

# 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

29 entries across 29 versions & 1 rubygems

Version Path
sqreen-1.22.0-java lib/sqreen/js/context_pool.rb
sqreen-1.22.0 lib/sqreen/js/context_pool.rb
sqreen-1.21.1-java lib/sqreen/js/context_pool.rb
sqreen-1.21.1 lib/sqreen/js/context_pool.rb
sqreen-1.21.0-java lib/sqreen/js/context_pool.rb
sqreen-1.21.0 lib/sqreen/js/context_pool.rb
sqreen-1.20.4-java lib/sqreen/js/context_pool.rb
sqreen-1.20.4 lib/sqreen/js/context_pool.rb
sqreen-1.21.0.beta3-java lib/sqreen/js/context_pool.rb
sqreen-1.21.0.beta3 lib/sqreen/js/context_pool.rb
sqreen-1.20.4.beta1 lib/sqreen/js/context_pool.rb
sqreen-1.19.4 lib/sqreen/js/context_pool.rb
sqreen-1.20.3 lib/sqreen/js/context_pool.rb
sqreen-1.20.2 lib/sqreen/js/context_pool.rb
sqreen-1.21.0.beta2 lib/sqreen/js/context_pool.rb
sqreen-1.21.0.beta1 lib/sqreen/js/context_pool.rb
sqreen-1.20.1-java lib/sqreen/js/context_pool.rb
sqreen-1.20.1 lib/sqreen/js/context_pool.rb
sqreen-1.20.0-java lib/sqreen/js/context_pool.rb
sqreen-1.20.0 lib/sqreen/js/context_pool.rb