random-port 0.7.2 → 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/codecov.yml +1 -1
- data/.github/workflows/copyrights.yml +4 -0
- data/.github/workflows/rake.yml +2 -2
- data/.github/workflows/xcop.yml +5 -1
- data/lib/random-port/pool.rb +30 -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/rake.yml
CHANGED
data/.github/workflows/xcop.yml
CHANGED
data/lib/random-port/pool.rb
CHANGED
@@ -92,28 +92,18 @@ class RandomPort::Pool
|
|
92
92
|
raise \
|
93
93
|
Timeout,
|
94
94
|
"Can't find a place in the pool of #{@limit} ports " \
|
95
|
+
"(#{@ports.size} already occupied) " \
|
95
96
|
"for #{total} port(s), after #{attempt} attempts in #{start.ago}"
|
96
97
|
end
|
97
98
|
attempt += 1
|
98
|
-
opts = safe
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
opts[i] = take(i.zero? ? @next : opts[i - 1] + 1)
|
104
|
-
end
|
105
|
-
rescue Errno::EADDRINUSE, SocketError
|
106
|
-
next
|
107
|
-
end
|
108
|
-
next if opts.any? { |p| @ports.include?(p) }
|
109
|
-
d = total * (total - 1) / 2
|
110
|
-
next unless opts.inject(&:+) - (total * opts.min) == d
|
111
|
-
@ports += opts
|
112
|
-
opts
|
99
|
+
opts = safe { group(total) }
|
100
|
+
if opts.nil?
|
101
|
+
@next += 1
|
102
|
+
else
|
103
|
+
@next = opts.max + 1
|
113
104
|
end
|
114
|
-
next if opts.nil?
|
115
|
-
@next = opts.max + 1
|
116
105
|
@next = 0 if @next > 65_535
|
106
|
+
next if opts.nil?
|
117
107
|
opts = opts[0] if total == 1
|
118
108
|
return opts unless block_given?
|
119
109
|
begin
|
@@ -125,6 +115,8 @@ class RandomPort::Pool
|
|
125
115
|
end
|
126
116
|
|
127
117
|
# Return it/them back to the pool.
|
118
|
+
# @param [Integer] port TCP port number to release
|
119
|
+
# @return nil
|
128
120
|
def release(port)
|
129
121
|
safe do
|
130
122
|
if port.is_a?(Array)
|
@@ -137,6 +129,27 @@ class RandomPort::Pool
|
|
137
129
|
|
138
130
|
private
|
139
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
|
+
|
140
153
|
# Find one possible TCP port.
|
141
154
|
# @param [Integer] opt Suggested port number
|
142
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'
|