random-port 0.7.0 → 0.7.2
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/actionlint.yml +1 -1
- data/.github/workflows/codecov.yml +2 -2
- data/.github/workflows/copyrights.yml +2 -2
- data/.github/workflows/markdown-lint.yml +1 -1
- data/.github/workflows/pdd.yml +1 -1
- data/.github/workflows/rake.yml +2 -2
- data/.github/workflows/xcop.yml +1 -1
- data/.rultor.yml +1 -1
- data/Gemfile +5 -5
- data/lib/random-port/pool.rb +17 -6
- data/random-port.gemspec +2 -1
- data/test/test_pool.rb +10 -0
- metadata +20 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7955c6c026633ceb489c05357e4d0383aca879252680a87053339682e014fe0f
|
4
|
+
data.tar.gz: ab010bcab3569cce98f608c38dfe23929ff5d8c94bba8a15e46c1e486aef71d0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5861bf9d24fbb7c4cdc9ca9393736c0d65ee3e30f207a715ac0f7366f8b13ebaf442e5ebff10ae1787515bceab7f257aea44b9592a09c7f9f27d57015805ac2b
|
7
|
+
data.tar.gz: de319e8a9c70bc72a98ea456297079c732a41a3deac2b9c943065a601446ff2f828cfc7bb1a0a25f24327ed72fd6605129021aa06e23defe89095a7257bfe0a8
|
@@ -25,7 +25,7 @@ on:
|
|
25
25
|
- master
|
26
26
|
jobs:
|
27
27
|
codecov:
|
28
|
-
runs-on: ubuntu-
|
28
|
+
runs-on: ubuntu-24.04
|
29
29
|
steps:
|
30
30
|
- uses: actions/checkout@v4
|
31
31
|
- uses: ruby/setup-ruby@v1
|
@@ -33,6 +33,6 @@ jobs:
|
|
33
33
|
ruby-version: 2.7
|
34
34
|
- run: bundle update
|
35
35
|
- run: bundle exec rake
|
36
|
-
- uses: codecov/codecov-action@
|
36
|
+
- uses: codecov/codecov-action@v5
|
37
37
|
with:
|
38
38
|
token: ${{ secrets.CODECOV_TOKEN }}
|
data/.github/workflows/pdd.yml
CHANGED
data/.github/workflows/rake.yml
CHANGED
data/.github/workflows/xcop.yml
CHANGED
data/.rultor.yml
CHANGED
data/Gemfile
CHANGED
@@ -23,10 +23,10 @@
|
|
23
23
|
source 'https://rubygems.org'
|
24
24
|
gemspec
|
25
25
|
|
26
|
-
gem 'minitest', '5.
|
26
|
+
gem 'minitest', '5.25.4', require: false
|
27
27
|
gem 'rake', '13.2.1', require: false
|
28
|
-
gem 'rubocop', '1.
|
29
|
-
gem 'rubocop-rspec', '3.0
|
28
|
+
gem 'rubocop', '1.69.2', require: false
|
29
|
+
gem 'rubocop-rspec', '3.3.0', require: false
|
30
30
|
gem 'simplecov', '0.22.0', require: false
|
31
|
-
gem 'threads', '0.
|
32
|
-
gem 'yard', '0.9.
|
31
|
+
gem 'threads', '0.4.1', require: false
|
32
|
+
gem 'yard', '0.9.37', require: false
|
data/lib/random-port/pool.rb
CHANGED
@@ -22,8 +22,9 @@
|
|
22
22
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
23
23
|
# SOFTWARE.
|
24
24
|
|
25
|
-
require 'socket'
|
26
25
|
require 'monitor'
|
26
|
+
require 'socket'
|
27
|
+
require 'tago'
|
27
28
|
require_relative 'module'
|
28
29
|
|
29
30
|
# Pool of TPC ports.
|
@@ -52,13 +53,14 @@ class RandomPort::Pool
|
|
52
53
|
attr_reader :limit
|
53
54
|
|
54
55
|
# Ctor.
|
55
|
-
# @param [
|
56
|
-
# @param [
|
56
|
+
# @param [Boolean] sync Set it to FALSE if you want this pool to be NOT thread-safe
|
57
|
+
# @param [Integer] limit Set the maximum number of ports in the pool
|
57
58
|
def initialize(sync: true, limit: 65_536)
|
58
59
|
@ports = []
|
59
60
|
@sync = sync
|
60
61
|
@monitor = Monitor.new
|
61
62
|
@limit = limit
|
63
|
+
@next = 1024
|
62
64
|
end
|
63
65
|
|
64
66
|
# Application wide pool of ports
|
@@ -84,17 +86,21 @@ class RandomPort::Pool
|
|
84
86
|
# is available.
|
85
87
|
def acquire(total = 1, timeout: 4)
|
86
88
|
start = Time.now
|
89
|
+
attempt = 0
|
87
90
|
loop do
|
88
91
|
if Time.now > start + timeout
|
89
|
-
raise
|
90
|
-
|
92
|
+
raise \
|
93
|
+
Timeout,
|
94
|
+
"Can't find a place in the pool of #{@limit} ports " \
|
95
|
+
"for #{total} port(s), after #{attempt} attempts in #{start.ago}"
|
91
96
|
end
|
97
|
+
attempt += 1
|
92
98
|
opts = safe do
|
93
99
|
next if @ports.count + total > @limit
|
94
100
|
opts = Array.new(0, total)
|
95
101
|
begin
|
96
102
|
(0..(total - 1)).each do |i|
|
97
|
-
opts[i] = i.zero? ?
|
103
|
+
opts[i] = take(i.zero? ? @next : opts[i - 1] + 1)
|
98
104
|
end
|
99
105
|
rescue Errno::EADDRINUSE, SocketError
|
100
106
|
next
|
@@ -106,6 +112,8 @@ for #{total} port(s), in #{format('%.02f', Time.now - start)}s"
|
|
106
112
|
opts
|
107
113
|
end
|
108
114
|
next if opts.nil?
|
115
|
+
@next = opts.max + 1
|
116
|
+
@next = 0 if @next > 65_535
|
109
117
|
opts = opts[0] if total == 1
|
110
118
|
return opts unless block_given?
|
111
119
|
begin
|
@@ -129,6 +137,9 @@ for #{total} port(s), in #{format('%.02f', Time.now - start)}s"
|
|
129
137
|
|
130
138
|
private
|
131
139
|
|
140
|
+
# Find one possible TCP port.
|
141
|
+
# @param [Integer] opt Suggested port number
|
142
|
+
# @return [Integer] Port found
|
132
143
|
def take(opt = 0)
|
133
144
|
server = TCPServer.new('127.0.0.1', opt)
|
134
145
|
p = server.addr[1]
|
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.2'
|
31
31
|
s.license = 'MIT'
|
32
32
|
s.summary = 'Random TCP port'
|
33
33
|
s.description = 'Reserves a random TCP port'
|
@@ -37,5 +37,6 @@ Gem::Specification.new do |s|
|
|
37
37
|
s.files = `git ls-files`.split($RS)
|
38
38
|
s.rdoc_options = ['--charset=UTF-8']
|
39
39
|
s.extra_rdoc_files = ['README.md']
|
40
|
+
s.add_dependency 'tago', '>0'
|
40
41
|
s.metadata['rubygems_mfa_required'] = 'true'
|
41
42
|
end
|
data/test/test_pool.rb
CHANGED
@@ -115,6 +115,16 @@ class RandomPort::TestPool < Minitest::Test
|
|
115
115
|
assert_equal(total, numbers.uniq.count)
|
116
116
|
end
|
117
117
|
|
118
|
+
def test_acquires_unique_numbers_in_block
|
119
|
+
total = 25
|
120
|
+
numbers = (0..total - 1).map do
|
121
|
+
RandomPort::Pool::SINGLETON.acquire do |port|
|
122
|
+
port
|
123
|
+
end
|
124
|
+
end
|
125
|
+
assert_equal(total, numbers.uniq.count)
|
126
|
+
end
|
127
|
+
|
118
128
|
def test_raises_when_too_many
|
119
129
|
pool = RandomPort::Pool.new(limit: 1)
|
120
130
|
pool.acquire
|
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: random-port
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.7.
|
4
|
+
version: 0.7.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yegor Bugayenko
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
12
|
-
dependencies:
|
11
|
+
date: 2024-12-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: tago
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
13
27
|
description: Reserves a random TCP port
|
14
28
|
email: yegor256@gmail.com
|
15
29
|
executables: []
|
@@ -45,7 +59,7 @@ licenses:
|
|
45
59
|
- MIT
|
46
60
|
metadata:
|
47
61
|
rubygems_mfa_required: 'true'
|
48
|
-
post_install_message:
|
62
|
+
post_install_message:
|
49
63
|
rdoc_options:
|
50
64
|
- "--charset=UTF-8"
|
51
65
|
require_paths:
|
@@ -62,7 +76,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
62
76
|
version: '0'
|
63
77
|
requirements: []
|
64
78
|
rubygems_version: 3.4.10
|
65
|
-
signing_key:
|
79
|
+
signing_key:
|
66
80
|
specification_version: 4
|
67
81
|
summary: Random TCP port
|
68
82
|
test_files: []
|