random-port 0.7.3 → 0.7.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/copyrights.yml +4 -0
- data/.github/workflows/xcop.yml +5 -1
- data/lib/random-port/pool.rb +29 -17
- data/random-port.gemspec +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 97daaf8ddcb40b28c9702c20272747d85619de8b19301f05a34918c6dd275cca
|
4
|
+
data.tar.gz: 48a06545628a4bfb4c3ee8b59e1880d83cd22cacc5fa22a893717b70225f08a5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: da44606055d80695de6baea9ef514b98d5b5d0cf82bac7276981edef778f2aabb7d2532d722b576eabffd2d4a58e7d21a8ee8effbb48f7d2c86e1b02dbd9ffed
|
7
|
+
data.tar.gz: 49c67ec4a38bdcf60cf12d7d53078ddbf70370899cd5da8c03cd7ce82a46750ba79bf35242d2b6cf32ab31742c4ebea0bec473ee95448e6b3692c21ec6965393
|
data/.github/workflows/xcop.yml
CHANGED
data/lib/random-port/pool.rb
CHANGED
@@ -96,25 +96,14 @@ class RandomPort::Pool
|
|
96
96
|
"for #{total} port(s), after #{attempt} attempts in #{start.ago}"
|
97
97
|
end
|
98
98
|
attempt += 1
|
99
|
-
opts = safe
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
opts[i] = take(i.zero? ? @next : opts[i - 1] + 1)
|
105
|
-
end
|
106
|
-
rescue Errno::EADDRINUSE, SocketError
|
107
|
-
next
|
108
|
-
end
|
109
|
-
next if opts.any? { |p| @ports.include?(p) }
|
110
|
-
d = total * (total - 1) / 2
|
111
|
-
next unless opts.inject(&:+) - (total * opts.min) == d
|
112
|
-
@ports += opts
|
113
|
-
opts
|
99
|
+
opts = safe { group(total) }
|
100
|
+
if opts.nil?
|
101
|
+
@next += 1
|
102
|
+
else
|
103
|
+
@next = opts.max + 1
|
114
104
|
end
|
115
|
-
next if opts.nil?
|
116
|
-
@next = opts.max + 1
|
117
105
|
@next = 0 if @next > 65_535
|
106
|
+
next if opts.nil?
|
118
107
|
opts = opts[0] if total == 1
|
119
108
|
return opts unless block_given?
|
120
109
|
begin
|
@@ -126,6 +115,8 @@ class RandomPort::Pool
|
|
126
115
|
end
|
127
116
|
|
128
117
|
# Return it/them back to the pool.
|
118
|
+
# @param [Integer] port TCP port number to release
|
119
|
+
# @return nil
|
129
120
|
def release(port)
|
130
121
|
safe do
|
131
122
|
if port.is_a?(Array)
|
@@ -138,6 +129,27 @@ class RandomPort::Pool
|
|
138
129
|
|
139
130
|
private
|
140
131
|
|
132
|
+
# Take a group of ports, if possible.
|
133
|
+
# @param [Integer] total How many ports to take
|
134
|
+
# @return [Array<Integer>|nil] Ports found or NIL if impossible now
|
135
|
+
def group(total)
|
136
|
+
return if @ports.count + total > @limit
|
137
|
+
opts = Array.new(0, total)
|
138
|
+
begin
|
139
|
+
(0..(total - 1)).each do |i|
|
140
|
+
port = i.zero? ? @next : opts[i - 1] + 1
|
141
|
+
opts[i] = take(port)
|
142
|
+
end
|
143
|
+
rescue Errno::EADDRINUSE, SocketError
|
144
|
+
return
|
145
|
+
end
|
146
|
+
return if opts.any? { |p| @ports.include?(p) }
|
147
|
+
d = total * (total - 1) / 2
|
148
|
+
return unless opts.inject(&:+) - (total * opts.min) == d
|
149
|
+
@ports += opts
|
150
|
+
opts
|
151
|
+
end
|
152
|
+
|
141
153
|
# Find one possible TCP port.
|
142
154
|
# @param [Integer] opt Suggested port number
|
143
155
|
# @return [Integer] Port found
|
data/random-port.gemspec
CHANGED
@@ -27,7 +27,7 @@ Gem::Specification.new do |s|
|
|
27
27
|
s.required_rubygems_version = Gem::Requirement.new('>= 0') if s.respond_to? :required_rubygems_version=
|
28
28
|
s.required_ruby_version = '>=2.3'
|
29
29
|
s.name = 'random-port'
|
30
|
-
s.version = '0.7.
|
30
|
+
s.version = '0.7.4'
|
31
31
|
s.license = 'MIT'
|
32
32
|
s.summary = 'Random TCP port'
|
33
33
|
s.description = 'Reserves a random TCP port'
|