drb 2.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,97 @@
1
+ # frozen_string_literal: false
2
+ require_relative 'drb'
3
+ require 'monitor'
4
+
5
+ module DRb
6
+
7
+ # Timer id conversion keeps objects alive for a certain amount of time after
8
+ # their last access. The default time period is 600 seconds and can be
9
+ # changed upon initialization.
10
+ #
11
+ # To use TimerIdConv:
12
+ #
13
+ # DRb.install_id_conv TimerIdConv.new 60 # one minute
14
+
15
+ class TimerIdConv < DRbIdConv
16
+ class TimerHolder2 # :nodoc:
17
+ include MonitorMixin
18
+
19
+ class InvalidIndexError < RuntimeError; end
20
+
21
+ def initialize(keeping=600)
22
+ super()
23
+ @sentinel = Object.new
24
+ @gc = {}
25
+ @renew = {}
26
+ @keeping = keeping
27
+ @expires = nil
28
+ end
29
+
30
+ def add(obj)
31
+ synchronize do
32
+ rotate
33
+ key = obj.__id__
34
+ @renew[key] = obj
35
+ invoke_keeper
36
+ return key
37
+ end
38
+ end
39
+
40
+ def fetch(key)
41
+ synchronize do
42
+ rotate
43
+ obj = peek(key)
44
+ raise InvalidIndexError if obj == @sentinel
45
+ @renew[key] = obj # KeepIt
46
+ return obj
47
+ end
48
+ end
49
+
50
+ private
51
+ def peek(key)
52
+ return @renew.fetch(key) { @gc.fetch(key, @sentinel) }
53
+ end
54
+
55
+ def invoke_keeper
56
+ return if @expires
57
+ @expires = Time.now + @keeping
58
+ on_gc
59
+ end
60
+
61
+ def on_gc
62
+ return unless Thread.main.alive?
63
+ return if @expires.nil?
64
+ Thread.new { rotate } if @expires < Time.now
65
+ ObjectSpace.define_finalizer(Object.new) {on_gc}
66
+ end
67
+
68
+ def rotate
69
+ synchronize do
70
+ if @expires &.< Time.now
71
+ @gc = @renew # GCed
72
+ @renew = {}
73
+ @expires = @gc.empty? ? nil : Time.now + @keeping
74
+ end
75
+ end
76
+ end
77
+ end
78
+
79
+ # Creates a new TimerIdConv which will hold objects for +keeping+ seconds.
80
+ def initialize(keeping=600)
81
+ @holder = TimerHolder2.new(keeping)
82
+ end
83
+
84
+ def to_obj(ref) # :nodoc:
85
+ return super if ref.nil?
86
+ @holder.fetch(ref)
87
+ rescue TimerHolder2::InvalidIndexError
88
+ raise "invalid reference"
89
+ end
90
+
91
+ def to_id(obj) # :nodoc:
92
+ return @holder.add(obj)
93
+ end
94
+ end
95
+ end
96
+
97
+ # DRb.install_id_conv(TimerIdConv.new)
@@ -0,0 +1,118 @@
1
+ # frozen_string_literal: false
2
+ require 'socket'
3
+ require_relative 'drb'
4
+ require 'tmpdir'
5
+
6
+ raise(LoadError, "UNIXServer is required") unless defined?(UNIXServer)
7
+
8
+ module DRb
9
+
10
+ # Implements DRb over a UNIX socket
11
+ #
12
+ # DRb UNIX socket URIs look like <code>drbunix:<path>?<option></code>. The
13
+ # option is optional.
14
+
15
+ class DRbUNIXSocket < DRbTCPSocket
16
+ # :stopdoc:
17
+ def self.parse_uri(uri)
18
+ if /\Adrbunix:(.*?)(\?(.*))?\z/ =~ uri
19
+ filename = $1
20
+ option = $3
21
+ [filename, option]
22
+ else
23
+ raise(DRbBadScheme, uri) unless uri.start_with?('drbunix:')
24
+ raise(DRbBadURI, 'can\'t parse uri:' + uri)
25
+ end
26
+ end
27
+
28
+ def self.open(uri, config)
29
+ filename, = parse_uri(uri)
30
+ soc = UNIXSocket.open(filename)
31
+ self.new(uri, soc, config)
32
+ end
33
+
34
+ def self.open_server(uri, config)
35
+ filename, = parse_uri(uri)
36
+ if filename.size == 0
37
+ soc = temp_server
38
+ filename = soc.path
39
+ uri = 'drbunix:' + soc.path
40
+ else
41
+ soc = UNIXServer.open(filename)
42
+ end
43
+ owner = config[:UNIXFileOwner]
44
+ group = config[:UNIXFileGroup]
45
+ if owner || group
46
+ require 'etc'
47
+ owner = Etc.getpwnam( owner ).uid if owner
48
+ group = Etc.getgrnam( group ).gid if group
49
+ File.chown owner, group, filename
50
+ end
51
+ mode = config[:UNIXFileMode]
52
+ File.chmod(mode, filename) if mode
53
+
54
+ self.new(uri, soc, config, true)
55
+ end
56
+
57
+ def self.uri_option(uri, config)
58
+ filename, option = parse_uri(uri)
59
+ return "drbunix:#{filename}", option
60
+ end
61
+
62
+ def initialize(uri, soc, config={}, server_mode = false)
63
+ super(uri, soc, config)
64
+ set_sockopt(@socket)
65
+ @server_mode = server_mode
66
+ @acl = nil
67
+ end
68
+
69
+ # import from tempfile.rb
70
+ Max_try = 10
71
+ private
72
+ def self.temp_server
73
+ tmpdir = Dir::tmpdir
74
+ n = 0
75
+ while true
76
+ begin
77
+ tmpname = sprintf('%s/druby%d.%d', tmpdir, $$, n)
78
+ lock = tmpname + '.lock'
79
+ unless File.exist?(tmpname) or File.exist?(lock)
80
+ Dir.mkdir(lock)
81
+ break
82
+ end
83
+ rescue
84
+ raise "cannot generate tempfile `%s'" % tmpname if n >= Max_try
85
+ #sleep(1)
86
+ end
87
+ n += 1
88
+ end
89
+ soc = UNIXServer.new(tmpname)
90
+ Dir.rmdir(lock)
91
+ soc
92
+ end
93
+
94
+ public
95
+ def close
96
+ return unless @socket
97
+ shutdown # DRbProtocol#shutdown
98
+ path = @socket.path if @server_mode
99
+ @socket.close
100
+ File.unlink(path) if @server_mode
101
+ @socket = nil
102
+ close_shutdown_pipe
103
+ end
104
+
105
+ def accept
106
+ s = accept_or_shutdown
107
+ return nil unless s
108
+ self.class.new(nil, s, @config)
109
+ end
110
+
111
+ def set_sockopt(soc)
112
+ # no-op for now
113
+ end
114
+ end
115
+
116
+ DRbProtocol.add_protocol(DRbUNIXSocket)
117
+ # :startdoc:
118
+ end
@@ -0,0 +1,3 @@
1
+ module DRb
2
+ VERSION = "2.0.4"
3
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: false
2
+ require_relative 'drb'
3
+ require 'monitor'
4
+
5
+ module DRb
6
+
7
+ # To use WeakIdConv:
8
+ #
9
+ # DRb.start_service(nil, nil, {:idconv => DRb::WeakIdConv.new})
10
+
11
+ class WeakIdConv < DRbIdConv
12
+ class WeakSet
13
+ include MonitorMixin
14
+ def initialize
15
+ super()
16
+ @immutable = {}
17
+ @map = ObjectSpace::WeakMap.new
18
+ end
19
+
20
+ def add(obj)
21
+ synchronize do
22
+ begin
23
+ @map[obj] = self
24
+ rescue ArgumentError
25
+ @immutable[obj.__id__] = obj
26
+ end
27
+ return obj.__id__
28
+ end
29
+ end
30
+
31
+ def fetch(ref)
32
+ synchronize do
33
+ @immutable.fetch(ref) {
34
+ @map.each { |key, _|
35
+ return key if key.__id__ == ref
36
+ }
37
+ raise RangeError.new("invalid reference")
38
+ }
39
+ end
40
+ end
41
+ end
42
+
43
+ def initialize()
44
+ super()
45
+ @weak_set = WeakSet.new
46
+ end
47
+
48
+ def to_obj(ref) # :nodoc:
49
+ return super if ref.nil?
50
+ @weak_set.fetch(ref)
51
+ end
52
+
53
+ def to_id(obj) # :nodoc:
54
+ return @weak_set.add(obj)
55
+ end
56
+ end
57
+ end
58
+
59
+ # DRb.install_id_conv(WeakIdConv.new)
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: drb
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.4
5
+ platform: ruby
6
+ authors:
7
+ - Masatoshi SEKI
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-12-03 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Distributed object system for Ruby
14
+ email:
15
+ - seki@ruby-lang.org
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - ".travis.yml"
22
+ - Gemfile
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - bin/console
27
+ - bin/setup
28
+ - drb.gemspec
29
+ - lib/drb.rb
30
+ - lib/drb/acl.rb
31
+ - lib/drb/drb.rb
32
+ - lib/drb/eq.rb
33
+ - lib/drb/extserv.rb
34
+ - lib/drb/extservm.rb
35
+ - lib/drb/gw.rb
36
+ - lib/drb/invokemethod.rb
37
+ - lib/drb/observer.rb
38
+ - lib/drb/ssl.rb
39
+ - lib/drb/timeridconv.rb
40
+ - lib/drb/unix.rb
41
+ - lib/drb/version.rb
42
+ - lib/drb/weakidconv.rb
43
+ homepage: https://github.com/ruby/drb
44
+ licenses:
45
+ - Ruby
46
+ - BSD-2-Clause
47
+ metadata:
48
+ homepage_uri: https://github.com/ruby/drb
49
+ source_code_uri: https://github.com/ruby/drb
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: 2.3.0
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubygems_version: 3.2.0.rc.2
66
+ signing_key:
67
+ specification_version: 4
68
+ summary: Distributed object system for Ruby
69
+ test_files: []