Sha256: 26f4395d403baa47f771c1ee33c339e12a1e0c1924072a85e822649250a5aa69

Contents?: true

Size: 1.69 KB

Versions: 10

Compression:

Stored size: 1.69 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 { "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

      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
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
sqreen-1.25.1 lib/sqreen/js/context_pool.rb
sqreen-1.25.0 lib/sqreen/js/context_pool.rb
sqreen-1.24.3 lib/sqreen/js/context_pool.rb
sqreen-1.24.2 lib/sqreen/js/context_pool.rb
sqreen-1.24.1 lib/sqreen/js/context_pool.rb
sqreen-1.24.0 lib/sqreen/js/context_pool.rb
sqreen-1.23.2 lib/sqreen/js/context_pool.rb
sqreen-1.23.1 lib/sqreen/js/context_pool.rb
sqreen-1.23.0 lib/sqreen/js/context_pool.rb
sqreen-1.22.1 lib/sqreen/js/context_pool.rb