Sha256: 1a2617fcb675a3784a842bbcd02ab444721c8b2cfc51ebc7c9229f072476f0ef

Contents?: true

Size: 1.12 KB

Versions: 15

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: false
require_relative 'drb'
require 'monitor'

module DRb

  # To use WeakIdConv:
  #
  #  DRb.start_service(nil, nil, {:idconv => DRb::WeakIdConv.new})

  class WeakIdConv < DRbIdConv
    class WeakSet
      include MonitorMixin
      def initialize
        super()
        @immutable = {}
        @map = ObjectSpace::WeakMap.new
      end

      def add(obj)
        synchronize do
          begin
            @map[obj] = self
          rescue ArgumentError
            @immutable[obj.__id__] = obj
          end
          return obj.__id__
        end
      end

      def fetch(ref)
        synchronize do
          @immutable.fetch(ref) {
            @map.each { |key, _|
              return key if key.__id__ == ref
            }
            raise RangeError.new("invalid reference")
          }
        end
      end
    end

    def initialize()
      super()
      @weak_set = WeakSet.new
    end

    def to_obj(ref) # :nodoc:
      return super if ref.nil?
      @weak_set.fetch(ref)
    end

    def to_id(obj) # :nodoc:
      return @weak_set.add(obj)
    end
  end
end

# DRb.install_id_conv(WeakIdConv.new)

Version data entries

15 entries across 15 versions & 8 rubygems

Version Path
fluent-plugin-nuopenlineage-light-0.1.0 vendor/bundle/ruby/3.3.0/gems/drb-2.2.1/lib/drb/weakidconv.rb
fluent-plugin-openlineage-light-0.1.4 vendor/bundle/ruby/3.3.0/gems/drb-2.2.1/lib/drb/weakidconv.rb
fluent-plugin-openlineage-light-0.1.3 vendor/bundle/ruby/3.3.0/gems/drb-2.2.1/lib/drb/weakidconv.rb
fluent-plugin-openlineage-0.1.0 vendor/bundle/ruby/3.3.0/gems/drb-2.2.1/lib/drb/weakidconv.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/drb-2.2.1/lib/drb/weakidconv.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/drb-2.2.1/lib/drb/weakidconv.rb
tinymce-rails-7.1.2 vendor/bundle/ruby/3.3.0/gems/drb-2.2.1/lib/drb/weakidconv.rb
drb-2.2.1 lib/drb/weakidconv.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/drb-2.2.0/lib/drb/weakidconv.rb
drb-2.0.6 lib/drb/weakidconv.rb
drb-2.2.0 lib/drb/weakidconv.rb
drb-2.1.1 lib/drb/weakidconv.rb
drb-2.1.0 lib/drb/weakidconv.rb
drb-2.0.5 lib/drb/weakidconv.rb
drb-2.0.4 lib/drb/weakidconv.rb