net_tcp_client 2.2.0 → 2.3.0
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/README.md +30 -20
- data/Rakefile +7 -7
- data/lib/net/tcp_client/address.rb +49 -14
- data/lib/net/tcp_client/exceptions.rb +3 -3
- data/lib/net/tcp_client/policy/base.rb +0 -1
- data/lib/net/tcp_client/policy/custom.rb +5 -3
- data/lib/net/tcp_client/policy/ordered.rb +1 -2
- data/lib/net/tcp_client/policy/random.rb +1 -2
- data/lib/net/tcp_client/tcp_client.rb +156 -117
- data/lib/net/tcp_client/version.rb +2 -2
- data/lib/net/tcp_client.rb +10 -10
- data/lib/net_tcp_client.rb +1 -1
- metadata +8 -38
- data/test/address_test.rb +0 -91
- data/test/policy/custom_policy_test.rb +0 -42
- data/test/policy/ordered_policy_test.rb +0 -36
- data/test/policy/random_policy_test.rb +0 -45
- data/test/simple_tcp_server.rb +0 -160
- data/test/ssl_files/ca.key +0 -27
- data/test/ssl_files/ca.pem +0 -19
- data/test/ssl_files/localhost-server-key.pem +0 -27
- data/test/ssl_files/localhost-server.pem +0 -19
- data/test/ssl_files/no-sni-key.pem +0 -27
- data/test/ssl_files/no-sni.pem +0 -20
- data/test/tcp_client_test.rb +0 -257
- data/test/test_helper.rb +0 -18
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: c8ff3b14fc47e63f2d733bd63abeda2177187bcefc8af9b8cc550ea456675897
|
|
4
|
+
data.tar.gz: f1ff58592eca02a9559fcb90a24c956bf3d8788550f4cb437ec9713473259727
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 978ce566c440d8622b204fa9d171a02e65f320c6e81bd5db28b6a8445cc80e1ad3127a454163737ead74db939120f94feb007941f54cf0895c4e6385d3b41694
|
|
7
|
+
data.tar.gz: 4243b4d6ea2e9344c0c5f1ed1b622d54b8ee73bae0f0b5d754b8455b677fc945ddf1649f61361eaf818d71573c8278587f0eba2a3ec6ffaae895b3d059f92c89
|
data/README.md
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
# net_tcp_client
|
|
2
|
-
[](https://rubygems.org/gems/net_tcp_client) [](https://rubygems.org/gems/net_tcp_client) [](https://github.com/reidmorrison/net_tcp_client/actions?query=workflow%3Abuild) [](https://rubygems.org/gems/net_tcp_client) [](http://opensource.org/licenses/Apache-2.0) 
|
|
3
3
|
|
|
4
4
|
Net::TCPClient is a TCP Socket Client with automated failover, load balancing, retries and built-in timeouts.
|
|
5
5
|
|
|
6
|
-
*
|
|
6
|
+
* https://github.com/reidmorrison/net_tcp_client
|
|
7
7
|
|
|
8
8
|
## Introduction
|
|
9
9
|
|
|
@@ -22,6 +22,7 @@ prevent a network issue from "hanging" the client program.
|
|
|
22
22
|
* Read Timeout.
|
|
23
23
|
* Write Timeout.
|
|
24
24
|
* Fails over / load balances across all servers under a single DNS entry.
|
|
25
|
+
* IPv4 and IPv6.
|
|
25
26
|
* Logging.
|
|
26
27
|
* Optional trace level logging of all data sent or received.
|
|
27
28
|
* Uses non blocking timeouts, instead of using threads such as used by the Timeout class.
|
|
@@ -36,7 +37,7 @@ prevent a network issue from "hanging" the client program.
|
|
|
36
37
|
require 'net/tcp_client'
|
|
37
38
|
|
|
38
39
|
Net::TCPClient.connect(server: 'mydomain:3300') do |client|
|
|
39
|
-
client.
|
|
40
|
+
client.write('Update the database')
|
|
40
41
|
response = client.read(20)
|
|
41
42
|
puts "Received: #{response}"
|
|
42
43
|
end
|
|
@@ -48,7 +49,7 @@ Enable SSL encryption:
|
|
|
48
49
|
require 'net/tcp_client'
|
|
49
50
|
|
|
50
51
|
Net::TCPClient.connect(server: 'mydomain:3300', ssl: true) do |client|
|
|
51
|
-
client.
|
|
52
|
+
client.write('Update the database')
|
|
52
53
|
response = client.read(20)
|
|
53
54
|
puts "Received: #{response}"
|
|
54
55
|
end
|
|
@@ -62,6 +63,15 @@ if the connection is lost the next server is tried immediately.
|
|
|
62
63
|
Net::TCPClient detects DNS entries that have multiple IP Addresses associated with them and
|
|
63
64
|
adds each of the ip addresses for the single DNS name to the list of servers to try to connect to.
|
|
64
65
|
|
|
66
|
+
Both IPv4 and IPv6 addresses are used. A DNS name's IPv4 addresses are tried before its IPv6 addresses.
|
|
67
|
+
An IPv6 address in a server name must be enclosed in square brackets:
|
|
68
|
+
|
|
69
|
+
~~~ruby
|
|
70
|
+
Net::TCPClient.connect(server: '[2001:db8::1]:3300') do |client|
|
|
71
|
+
client.write('Update the database')
|
|
72
|
+
end
|
|
73
|
+
~~~
|
|
74
|
+
|
|
65
75
|
If a server is unavailable, cannot connect, or the connection is lost, the next server is immediately
|
|
66
76
|
tried. Once all servers have been exhausted, it will keep trying to connect, starting with the
|
|
67
77
|
first server again.
|
|
@@ -104,7 +114,7 @@ Servers are tried in a Random order.
|
|
|
104
114
|
~~~ruby
|
|
105
115
|
tcp_client = Net::TCPClient.new(
|
|
106
116
|
servers: ['server1:3300', 'server2:3300', 'server3:3600'],
|
|
107
|
-
policy: :
|
|
117
|
+
policy: :random
|
|
108
118
|
)
|
|
109
119
|
~~~
|
|
110
120
|
|
|
@@ -145,7 +155,7 @@ Example run, the servers could be tried in the following order:
|
|
|
145
155
|
If a connection cannot be established to any servers in the list Net::TCPClient will retry from the
|
|
146
156
|
first server. This retry behavior can be controlled using the following options:
|
|
147
157
|
|
|
148
|
-
* `connect_retry_count` [
|
|
158
|
+
* `connect_retry_count` [Integer]
|
|
149
159
|
* Number of times to retry connecting when a connection fails
|
|
150
160
|
* Default: 10
|
|
151
161
|
|
|
@@ -153,7 +163,7 @@ first server. This retry behavior can be controlled using the following options:
|
|
|
153
163
|
* Number of seconds between connection retry attempts after the first failed attempt
|
|
154
164
|
* Default: 0.5
|
|
155
165
|
|
|
156
|
-
* `retry_count` [
|
|
166
|
+
* `retry_count` [Integer]
|
|
157
167
|
* Number of times to retry when calling #retry_on_connection_failure
|
|
158
168
|
* This is independent of :connect_retry_count which still applies with
|
|
159
169
|
* connection failures. This retry controls upto how many times to retry the
|
|
@@ -184,16 +194,16 @@ Net::TCPClient.connect(
|
|
|
184
194
|
connect_retry_interval: 0.1,
|
|
185
195
|
connect_retry_count: 5
|
|
186
196
|
) do |client|
|
|
187
|
-
# If the connection is lost, create a new one and retry the
|
|
197
|
+
# If the connection is lost, create a new one and retry the write
|
|
188
198
|
client.retry_on_connection_failure do
|
|
189
|
-
client.
|
|
199
|
+
client.write('How many users available?')
|
|
190
200
|
response = client.read(20)
|
|
191
201
|
puts "Received: #{response}"
|
|
192
202
|
end
|
|
193
203
|
end
|
|
194
204
|
~~~
|
|
195
205
|
|
|
196
|
-
If the connection is lost during either the `
|
|
206
|
+
If the connection is lost during either the `write` or the `read` above the
|
|
197
207
|
entire block will be re-tried once the connection has been re-stablished.
|
|
198
208
|
|
|
199
209
|
## Callbacks
|
|
@@ -210,7 +220,7 @@ Any time a connection has been established a callback can be called to handle ac
|
|
|
210
220
|
tcp_client = Net::TCPClient.new(
|
|
211
221
|
servers: ['server1:3300', 'server2:3300', 'server3:3600'],
|
|
212
222
|
on_connect: -> do |client|
|
|
213
|
-
client.
|
|
223
|
+
client.write('My username and password')
|
|
214
224
|
result = client.read(2)
|
|
215
225
|
raise "Authentication failed" if result != 'OK'
|
|
216
226
|
end
|
|
@@ -224,13 +234,13 @@ tcp_client = Net::TCPClient.new(
|
|
|
224
234
|
servers: ['server1:3300', 'server2:3300', 'server3:3600'],
|
|
225
235
|
on_connect: -> do |client|
|
|
226
236
|
# Set the sequence number to 0
|
|
227
|
-
user_data = 0
|
|
237
|
+
client.user_data = 0
|
|
228
238
|
end
|
|
229
239
|
)
|
|
230
240
|
|
|
231
241
|
tcp_client.retry_on_connection_failure do
|
|
232
|
-
#
|
|
233
|
-
tcp_client.
|
|
242
|
+
# Write with the sequence number
|
|
243
|
+
tcp_client.write("#{tcp_client.user_data} hello")
|
|
234
244
|
result = tcp_client.receive(30)
|
|
235
245
|
|
|
236
246
|
# Increment sequence number after every call to the server
|
|
@@ -250,7 +260,7 @@ test on a daily basis, including connections over the internet between remote da
|
|
|
250
260
|
|
|
251
261
|
gem install net_tcp_client
|
|
252
262
|
|
|
253
|
-
To enable logging add [Semantic Logger](
|
|
263
|
+
To enable logging add [Semantic Logger](https://logger.rocketjob.io/):
|
|
254
264
|
|
|
255
265
|
gem install semantic_logger
|
|
256
266
|
|
|
@@ -273,13 +283,13 @@ SemanticLogger.default_level = :trace
|
|
|
273
283
|
SemanticLogger.add_appender(file_name: 'development.log', formatter: :color)
|
|
274
284
|
~~~
|
|
275
285
|
|
|
276
|
-
If running Rails, see: [Semantic Logger Rails](
|
|
286
|
+
If running Rails, see: [Semantic Logger Rails](https://logger.rocketjob.io/rails.html)
|
|
277
287
|
|
|
278
288
|
### Support
|
|
279
289
|
|
|
280
290
|
Join the [Gitter chat session](https://gitter.im/rocketjob/support) if you have any questions.
|
|
281
291
|
|
|
282
|
-
Issues / bugs can be reported via [Github issues](https://github.com/
|
|
292
|
+
Issues / bugs can be reported via [Github issues](https://github.com/reidmorrison/net_tcp_client/issues).
|
|
283
293
|
|
|
284
294
|
### Upgrading to V2
|
|
285
295
|
|
|
@@ -303,7 +313,7 @@ Tested and supported on the following Ruby platforms:
|
|
|
303
313
|
- JRuby 1.7.23, 9.0 and above
|
|
304
314
|
- Rubinius 2.5 and above
|
|
305
315
|
|
|
306
|
-
There is a soft dependency on [Semantic Logger](
|
|
316
|
+
There is a soft dependency on [Semantic Logger](https://github.com/reidmorrison/semantic_logger). It will use SemanticLogger only if
|
|
307
317
|
it is already available, otherwise any other standard Ruby logger can be used.
|
|
308
318
|
|
|
309
319
|
### Note
|
|
@@ -314,8 +324,8 @@ Be sure to place the `semantic_logger` gem dependency before `net_tcp_client` in
|
|
|
314
324
|
|
|
315
325
|
[Reid Morrison](https://github.com/reidmorrison)
|
|
316
326
|
|
|
317
|
-
[Contributors](https://github.com/
|
|
327
|
+
[Contributors](https://github.com/reidmorrison/net_tcp_client/graphs/contributors)
|
|
318
328
|
|
|
319
329
|
## Versioning
|
|
320
330
|
|
|
321
|
-
This project uses [Semantic Versioning](
|
|
331
|
+
This project uses [Semantic Versioning](https://semver.org/).
|
data/Rakefile
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
require
|
|
2
|
-
require_relative
|
|
1
|
+
require "rake/testtask"
|
|
2
|
+
require_relative "lib/net/tcp_client/version"
|
|
3
3
|
|
|
4
4
|
task :gem do
|
|
5
|
-
system
|
|
5
|
+
system "gem build net_tcp_client.gemspec"
|
|
6
6
|
end
|
|
7
7
|
|
|
8
|
-
task :
|
|
8
|
+
task publish: :gem do
|
|
9
9
|
system "git tag -a v#{Net::TCPClient::VERSION} -m 'Tagging #{Net::TCPClient::VERSION}'"
|
|
10
|
-
system
|
|
10
|
+
system "git push --tags"
|
|
11
11
|
system "gem push net_tcp_client-#{Net::TCPClient::VERSION}.gem"
|
|
12
12
|
system "rm net_tcp_client-#{Net::TCPClient::VERSION}.gem"
|
|
13
13
|
end
|
|
14
14
|
|
|
15
15
|
Rake::TestTask.new(:test) do |t|
|
|
16
|
-
t.pattern =
|
|
16
|
+
t.pattern = "test/**/*_test.rb"
|
|
17
17
|
t.verbose = true
|
|
18
18
|
t.warning = true
|
|
19
19
|
end
|
|
20
20
|
|
|
21
|
-
task :
|
|
21
|
+
task default: :test
|
|
@@ -1,19 +1,20 @@
|
|
|
1
|
-
require
|
|
2
|
-
require
|
|
1
|
+
require "socket"
|
|
2
|
+
require "ipaddr"
|
|
3
3
|
module Net
|
|
4
4
|
class TCPClient
|
|
5
5
|
# Host name, ip address and port to connect to
|
|
6
6
|
class Address
|
|
7
7
|
attr_accessor :host_name, :ip_address, :port
|
|
8
8
|
|
|
9
|
-
# Returns [Array<String>]
|
|
10
|
-
# Returns dns_name if it is already an IP Address
|
|
9
|
+
# Returns [Array<String>] the IPv4 and IPv6 addresses for the supplied DNS entry.
|
|
10
|
+
# Returns dns_name if it is already an IP Address.
|
|
11
|
+
#
|
|
12
|
+
# IPv4 addresses are listed before IPv6 addresses, each in the order the resolver returns them,
|
|
13
|
+
# so a host that has both still connects over IPv4 first, as it always has.
|
|
11
14
|
def self.ip_addresses(dns_name)
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
end
|
|
16
|
-
ips.uniq
|
|
15
|
+
infos = Addrinfo.getaddrinfo(dns_name, nil, Socket::AF_UNSPEC, Socket::SOCK_STREAM)
|
|
16
|
+
ipv4, ipv6 = infos.select { |info| info.ipv4? || info.ipv6? }.partition(&:ipv4?)
|
|
17
|
+
(ipv4 + ipv6).map(&:ip_address).uniq
|
|
17
18
|
end
|
|
18
19
|
|
|
19
20
|
# Returns [Array<Net::TCPClient::Address>] addresses for a given DNS / host name.
|
|
@@ -28,26 +29,60 @@ module Net
|
|
|
28
29
|
# Returns [Array<Net::TCPClient::Address>] addresses for a list of DNS / host name's
|
|
29
30
|
# that are paired with their numbers
|
|
30
31
|
#
|
|
31
|
-
# server_name should be either a host_name, or ip address combined with a port
|
|
32
|
+
# server_name should be either a host_name, or ip address combined with a port.
|
|
33
|
+
# An IPv6 address must be enclosed in square brackets:
|
|
32
34
|
# "host_name:1234"
|
|
33
35
|
# "192.168.1.10:80"
|
|
36
|
+
# "[2001:db8::1]:80"
|
|
34
37
|
def self.addresses_for_server_name(server_name)
|
|
35
|
-
dns_name, port = server_name
|
|
36
|
-
|
|
37
|
-
|
|
38
|
+
dns_name, port = parse_server_name(server_name)
|
|
39
|
+
unless dns_name && port&.positive?
|
|
40
|
+
raise(
|
|
41
|
+
ArgumentError,
|
|
42
|
+
"Invalid host_name: #{server_name.inspect}. " \
|
|
43
|
+
"Must be formatted as 'host_name:1234', '192.168.1.10:80' or '[2001:db8::1]:80'"
|
|
44
|
+
)
|
|
45
|
+
end
|
|
46
|
+
|
|
38
47
|
addresses(dns_name, port)
|
|
39
48
|
end
|
|
40
49
|
|
|
50
|
+
# Returns [String, Integer] the host name and port in the supplied server name,
|
|
51
|
+
# or nil for either one it does not have.
|
|
52
|
+
def self.parse_server_name(server_name)
|
|
53
|
+
match = server_name.to_s.match(/\A(?:\[(?<ipv6>[^\]]+)\]|(?<host>[^:\[\]]+)):(?<port>\d+)\z/)
|
|
54
|
+
return unless match
|
|
55
|
+
|
|
56
|
+
[match[:ipv6] || match[:host], match[:port].to_i]
|
|
57
|
+
end
|
|
58
|
+
private_class_method :parse_server_name
|
|
59
|
+
|
|
41
60
|
def initialize(host_name, ip_address, port)
|
|
42
61
|
@host_name = host_name
|
|
43
62
|
@ip_address = ip_address
|
|
44
63
|
@port = port.to_i
|
|
45
64
|
end
|
|
46
65
|
|
|
66
|
+
# Returns [Integer] the address family of this address: Socket::AF_INET or Socket::AF_INET6
|
|
67
|
+
def family
|
|
68
|
+
addrinfo.afamily
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# Returns [String] the packed socket address to connect to
|
|
72
|
+
def sockaddr
|
|
73
|
+
addrinfo.to_sockaddr
|
|
74
|
+
end
|
|
75
|
+
|
|
47
76
|
def to_s
|
|
48
77
|
"#{host_name}[#{ip_address}]:#{port}"
|
|
49
78
|
end
|
|
50
|
-
end
|
|
51
79
|
|
|
80
|
+
private
|
|
81
|
+
|
|
82
|
+
# The ip address is numeric, so this does not look up DNS.
|
|
83
|
+
def addrinfo
|
|
84
|
+
Addrinfo.tcp(ip_address, port)
|
|
85
|
+
end
|
|
86
|
+
end
|
|
52
87
|
end
|
|
53
88
|
end
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
module Net
|
|
2
2
|
class TCPClient
|
|
3
|
-
|
|
4
3
|
class ConnectionTimeout < ::SocketError
|
|
5
4
|
end
|
|
5
|
+
|
|
6
6
|
class ReadTimeout < ::SocketError
|
|
7
7
|
end
|
|
8
|
+
|
|
8
9
|
class WriteTimeout < ::SocketError
|
|
9
10
|
end
|
|
10
11
|
|
|
@@ -27,12 +28,11 @@ module Net
|
|
|
27
28
|
#
|
|
28
29
|
# cause [Exception]
|
|
29
30
|
# Original Exception if any, otherwise nil
|
|
30
|
-
def initialize(message, server, cause=nil)
|
|
31
|
+
def initialize(message, server, cause = nil)
|
|
31
32
|
@server = server
|
|
32
33
|
@cause = cause
|
|
33
34
|
super(message)
|
|
34
35
|
end
|
|
35
36
|
end
|
|
36
|
-
|
|
37
37
|
end
|
|
38
38
|
end
|
|
@@ -26,13 +26,15 @@ module Net
|
|
|
26
26
|
# end
|
|
27
27
|
def each(&block)
|
|
28
28
|
count = 1
|
|
29
|
-
while address = @block.call(addresses, count)
|
|
30
|
-
|
|
29
|
+
while (address = @block.call(addresses, count))
|
|
30
|
+
unless address.is_a?(Net::TCPClient::Address) || address.nil?
|
|
31
|
+
raise(ArgumentError, "Proc must return Net::TCPClient::Address, or nil")
|
|
32
|
+
end
|
|
33
|
+
|
|
31
34
|
block.call(address)
|
|
32
35
|
count += 1
|
|
33
36
|
end
|
|
34
37
|
end
|
|
35
|
-
|
|
36
38
|
end
|
|
37
39
|
end
|
|
38
40
|
end
|
|
@@ -5,9 +5,8 @@ module Net
|
|
|
5
5
|
class Random < Base
|
|
6
6
|
# Calls the block once for each server, with the addresses in random order
|
|
7
7
|
def each(&block)
|
|
8
|
-
addresses.shuffle.each {|address| block.call(address)}
|
|
8
|
+
addresses.shuffle.each { |address| block.call(address) }
|
|
9
9
|
end
|
|
10
|
-
|
|
11
10
|
end
|
|
12
11
|
end
|
|
13
12
|
end
|