net_tcp_client 1.0.2 → 2.0.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 +237 -41
- data/lib/net/tcp_client.rb +18 -2
- data/lib/net/tcp_client/address.rb +53 -0
- data/lib/net/tcp_client/exceptions.rb +5 -3
- data/lib/net/tcp_client/policy/base.rb +36 -0
- data/lib/net/tcp_client/policy/custom.rb +39 -0
- data/lib/net/tcp_client/policy/ordered.rb +14 -0
- data/lib/net/tcp_client/policy/random.rb +14 -0
- data/lib/net/tcp_client/tcp_client.rb +332 -209
- data/lib/net/tcp_client/version.rb +1 -1
- data/test/address_test.rb +91 -0
- data/test/policy/custom_policy_test.rb +42 -0
- data/test/policy/ordered_policy_test.rb +36 -0
- data/test/policy/random_policy_test.rb +46 -0
- data/test/simple_tcp_server.rb +36 -9
- data/test/ssl_files/ca.pem +19 -0
- data/test/ssl_files/localhost-server-key.pem +27 -0
- data/test/ssl_files/localhost-server.pem +18 -0
- data/test/tcp_client_test.rb +207 -143
- data/test/test_helper.rb +1 -2
- metadata +23 -5
- data/lib/net/tcp_client/logging.rb +0 -193
data/test/test_helper.rb
CHANGED
@@ -6,10 +6,9 @@ ENV['RAILS_ENV'] = 'test'
|
|
6
6
|
|
7
7
|
require 'minitest/autorun'
|
8
8
|
require 'minitest/reporters'
|
9
|
-
require 'semantic_logger'
|
10
9
|
require 'net/tcp_client'
|
11
10
|
|
12
11
|
Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new
|
13
12
|
|
14
13
|
SemanticLogger.default_level = :trace
|
15
|
-
SemanticLogger.add_appender('test.log',
|
14
|
+
SemanticLogger.add_appender(file_name: 'test.log', formatter: :color)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: net_tcp_client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Reid Morrison
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-04-13 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Net::TCPClient implements resilience features that many developers wish
|
14
14
|
was already included in the standard Ruby libraries.
|
@@ -22,17 +22,28 @@ files:
|
|
22
22
|
- README.md
|
23
23
|
- Rakefile
|
24
24
|
- lib/net/tcp_client.rb
|
25
|
+
- lib/net/tcp_client/address.rb
|
25
26
|
- lib/net/tcp_client/exceptions.rb
|
26
|
-
- lib/net/tcp_client/
|
27
|
+
- lib/net/tcp_client/policy/base.rb
|
28
|
+
- lib/net/tcp_client/policy/custom.rb
|
29
|
+
- lib/net/tcp_client/policy/ordered.rb
|
30
|
+
- lib/net/tcp_client/policy/random.rb
|
27
31
|
- lib/net/tcp_client/tcp_client.rb
|
28
32
|
- lib/net/tcp_client/version.rb
|
29
33
|
- lib/net_tcp_client.rb
|
34
|
+
- test/address_test.rb
|
35
|
+
- test/policy/custom_policy_test.rb
|
36
|
+
- test/policy/ordered_policy_test.rb
|
37
|
+
- test/policy/random_policy_test.rb
|
30
38
|
- test/simple_tcp_server.rb
|
39
|
+
- test/ssl_files/ca.pem
|
40
|
+
- test/ssl_files/localhost-server-key.pem
|
41
|
+
- test/ssl_files/localhost-server.pem
|
31
42
|
- test/tcp_client_test.rb
|
32
43
|
- test/test_helper.rb
|
33
44
|
homepage: https://github.com/rocketjob/net_tcp_client
|
34
45
|
licenses:
|
35
|
-
- Apache
|
46
|
+
- Apache-2.0
|
36
47
|
metadata: {}
|
37
48
|
post_install_message:
|
38
49
|
rdoc_options: []
|
@@ -50,12 +61,19 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
50
61
|
version: '0'
|
51
62
|
requirements: []
|
52
63
|
rubyforge_project:
|
53
|
-
rubygems_version: 2.
|
64
|
+
rubygems_version: 2.5.1
|
54
65
|
signing_key:
|
55
66
|
specification_version: 4
|
56
67
|
summary: Net::TCPClient is a TCP Socket Client with built-in timeouts, retries, and
|
57
68
|
logging
|
58
69
|
test_files:
|
70
|
+
- test/address_test.rb
|
71
|
+
- test/policy/custom_policy_test.rb
|
72
|
+
- test/policy/ordered_policy_test.rb
|
73
|
+
- test/policy/random_policy_test.rb
|
59
74
|
- test/simple_tcp_server.rb
|
75
|
+
- test/ssl_files/ca.pem
|
76
|
+
- test/ssl_files/localhost-server-key.pem
|
77
|
+
- test/ssl_files/localhost-server.pem
|
60
78
|
- test/tcp_client_test.rb
|
61
79
|
- test/test_helper.rb
|
@@ -1,193 +0,0 @@
|
|
1
|
-
module Net
|
2
|
-
class TCPClient
|
3
|
-
#
|
4
|
-
# Purpose: Support SemanticLogger API without forcing a dependency on SemanticLogger
|
5
|
-
#
|
6
|
-
# This Mix-in has been copied from semantic_logger/compatible
|
7
|
-
#
|
8
|
-
# Do not modify this file, grab the latest version from Semantic Logger
|
9
|
-
#
|
10
|
-
module Logging
|
11
|
-
def self.new_logger(logger=nil, name=nil, log_level=nil)
|
12
|
-
# SemanticLogger is a soft dependency, use it if already loaded
|
13
|
-
if defined?(SemanticLogger)
|
14
|
-
SemanticLogger::Logger.new(name, log_level)
|
15
|
-
elsif logger && !logger.respond_to?(:benchmark_trace)
|
16
|
-
logger.extend(InstanceMethods)
|
17
|
-
logger
|
18
|
-
else
|
19
|
-
# Return a nil logger
|
20
|
-
require 'logger'
|
21
|
-
logger = Logger.new($null)
|
22
|
-
logger.level = Logger::FATAL
|
23
|
-
logger.extend(InstanceMethods)
|
24
|
-
logger
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
module InstanceMethods
|
29
|
-
# See SemanticLogger for details on these logging extensions
|
30
|
-
[:debug, :info, :warn, :error, :fatal].each_with_index do |level|
|
31
|
-
class_eval <<-EOT, __FILE__, __LINE__ + 1
|
32
|
-
def #{level}(message=nil, payload=nil, exception=nil, &block)
|
33
|
-
if #{level}?
|
34
|
-
super(format_log_message('#{level}', message, payload, exception, &block))
|
35
|
-
true
|
36
|
-
else
|
37
|
-
false
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
def benchmark_#{level}(message, params = {}, &block)
|
42
|
-
if #{level}?
|
43
|
-
benchmark(:#{level}, message, params, &block)
|
44
|
-
else
|
45
|
-
block.call(params) if block
|
46
|
-
end
|
47
|
-
end
|
48
|
-
EOT
|
49
|
-
end
|
50
|
-
|
51
|
-
# Log trace level messages as debug
|
52
|
-
def trace(*args)
|
53
|
-
debug(*args)
|
54
|
-
end
|
55
|
-
|
56
|
-
def trace?
|
57
|
-
debug?
|
58
|
-
end
|
59
|
-
|
60
|
-
def trace_benchmark(*args)
|
61
|
-
debug_benchmark(*args)
|
62
|
-
end
|
63
|
-
|
64
|
-
def format_log_message(level, message=nil, payload=nil, exception=nil, duration=nil, &block)
|
65
|
-
if exception.nil? && payload && payload.is_a?(Exception)
|
66
|
-
exception = payload
|
67
|
-
payload = nil
|
68
|
-
end
|
69
|
-
|
70
|
-
if block && (result = block.call)
|
71
|
-
if result.is_a?(String)
|
72
|
-
message = message.nil? ? result : "\#{message} -- \#{result}"
|
73
|
-
elsif payload && payload.respond_to?(:merge)
|
74
|
-
payload.merge(result)
|
75
|
-
else
|
76
|
-
payload = result
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
# Add scoped payload
|
81
|
-
if self.payload
|
82
|
-
payload = payload.nil? ? self.payload : self.payload.merge(payload)
|
83
|
-
end
|
84
|
-
|
85
|
-
tags_str = tags.collect { |tag| "[#{tag}]" }.join(" ") + " " if tags && (tags.size > 0)
|
86
|
-
|
87
|
-
message = message.to_s.dup
|
88
|
-
message << ' -- ' << payload.inspect if payload
|
89
|
-
message << ' -- Exception: ' << "#{exception.class}: #{exception.message}\n#{(exception.backtrace || []).join("\n")}" if exception
|
90
|
-
|
91
|
-
duration_str = duration ? "(#{'%.1f' % duration}ms) " : ''
|
92
|
-
|
93
|
-
"#{tags_str}#{duration_str} #{message}"
|
94
|
-
end
|
95
|
-
|
96
|
-
# Measure the supplied block and log the message
|
97
|
-
def benchmark(level, message, params, &block)
|
98
|
-
start = Time.now
|
99
|
-
begin
|
100
|
-
rc = block.call(params) if block
|
101
|
-
exception = params[:exception]
|
102
|
-
rc
|
103
|
-
rescue Exception => exc
|
104
|
-
exception = exc
|
105
|
-
ensure
|
106
|
-
end_time = Time.now
|
107
|
-
# Extract options after block completes so that block can modify any of the options
|
108
|
-
log_exception = params[:log_exception] || :partial
|
109
|
-
on_exception_level = params[:on_exception_level]
|
110
|
-
min_duration = params[:min_duration] || 0.0
|
111
|
-
payload = params[:payload]
|
112
|
-
metric = params[:metric]
|
113
|
-
duration =
|
114
|
-
if block_given?
|
115
|
-
1000.0 * (end_time - start)
|
116
|
-
else
|
117
|
-
params[:duration] || raise('Mandatory block missing when :duration option is not supplied')
|
118
|
-
end
|
119
|
-
|
120
|
-
# Add scoped payload
|
121
|
-
if self.payload
|
122
|
-
payload = payload.nil? ? self.payload : self.payload.merge(payload)
|
123
|
-
end
|
124
|
-
if exception
|
125
|
-
logged_exception = exception
|
126
|
-
case log_exception
|
127
|
-
when :full
|
128
|
-
# On exception change the log level
|
129
|
-
level = on_exception_level if on_exception_level
|
130
|
-
when :partial
|
131
|
-
# On exception change the log level
|
132
|
-
level = on_exception_level if on_exception_level
|
133
|
-
message = "#{message} -- Exception: #{exception.class}: #{exception.message}"
|
134
|
-
logged_exception = nil
|
135
|
-
else
|
136
|
-
logged_exception = nil
|
137
|
-
end
|
138
|
-
send(level, format_log_message(level, message, payload, logged_exception, duration))
|
139
|
-
raise exception
|
140
|
-
elsif duration >= min_duration
|
141
|
-
# Only log if the block took longer than 'min_duration' to complete
|
142
|
-
send(level, format_log_message(level, message, payload, logged_exception, duration))
|
143
|
-
end
|
144
|
-
end
|
145
|
-
end
|
146
|
-
|
147
|
-
def tagged(*tags)
|
148
|
-
new_tags = push_tags(*tags)
|
149
|
-
yield self
|
150
|
-
ensure
|
151
|
-
pop_tags(new_tags.size)
|
152
|
-
end
|
153
|
-
|
154
|
-
# Previous method for supplying tags
|
155
|
-
alias_method :with_tags, :tagged
|
156
|
-
|
157
|
-
def tags
|
158
|
-
# Since tags are stored on a per thread basis this list is thread-safe
|
159
|
-
t = Thread.current[:semantic_logger_tags]
|
160
|
-
t.nil? ? [] : t.clone
|
161
|
-
end
|
162
|
-
|
163
|
-
def push_tags *tags
|
164
|
-
# Need to flatten and reject empties to support calls from Rails 4
|
165
|
-
new_tags = tags.flatten.collect(&:to_s).reject(&:empty?)
|
166
|
-
t = Thread.current[:semantic_logger_tags]
|
167
|
-
|
168
|
-
Thread.current[:semantic_logger_tags] = t.nil? ? new_tags : t.concat(new_tags)
|
169
|
-
new_tags
|
170
|
-
end
|
171
|
-
|
172
|
-
def pop_tags(quantity=1)
|
173
|
-
t = Thread.current[:semantic_logger_tags]
|
174
|
-
t.pop(quantity) unless t.nil?
|
175
|
-
end
|
176
|
-
|
177
|
-
def with_payload(payload)
|
178
|
-
current_payload = self.payload
|
179
|
-
Thread.current[:semantic_logger_payload] = current_payload ? current_payload.merge(payload) : payload
|
180
|
-
yield
|
181
|
-
ensure
|
182
|
-
Thread.current[:semantic_logger_payload] = current_payload
|
183
|
-
end
|
184
|
-
|
185
|
-
def payload
|
186
|
-
Thread.current[:semantic_logger_payload]
|
187
|
-
end
|
188
|
-
|
189
|
-
end
|
190
|
-
end
|
191
|
-
end
|
192
|
-
|
193
|
-
end
|