active_utils 1.0.5 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/active_utils/common/connection.rb +22 -3
- data/lib/active_utils/common/network_connection_retries.rb +15 -4
- data/lib/active_utils/common/posts_data.rb +8 -0
- data/lib/active_utils/version.rb +1 -1
- data/lib/certs/cacert.pem +3693 -7588
- data/test/unit/connection_test.rb +35 -3
- data/test/unit/network_connection_retries_test.rb +14 -2
- data/test/unit/posts_data_test.rb +23 -0
- metadata +41 -21
@@ -11,6 +11,8 @@ module ActiveMerchant
|
|
11
11
|
OPEN_TIMEOUT = 60
|
12
12
|
READ_TIMEOUT = 60
|
13
13
|
VERIFY_PEER = true
|
14
|
+
CA_FILE = (File.dirname(__FILE__) + '/../../certs/cacert.pem')
|
15
|
+
CA_PATH = nil
|
14
16
|
RETRY_SAFE = false
|
15
17
|
RUBY_184_POST_HEADERS = { "Content-Type" => "application/x-www-form-urlencoded" }
|
16
18
|
|
@@ -18,12 +20,17 @@ module ActiveMerchant
|
|
18
20
|
attr_accessor :open_timeout
|
19
21
|
attr_accessor :read_timeout
|
20
22
|
attr_accessor :verify_peer
|
23
|
+
attr_accessor :ssl_version
|
24
|
+
attr_accessor :ca_file
|
25
|
+
attr_accessor :ca_path
|
26
|
+
attr_accessor :retry_safe
|
21
27
|
attr_accessor :pem
|
22
28
|
attr_accessor :pem_password
|
23
29
|
attr_accessor :wiredump_device
|
24
30
|
attr_accessor :logger
|
25
31
|
attr_accessor :tag
|
26
32
|
attr_accessor :ignore_http_status
|
33
|
+
attr_accessor :max_retries
|
27
34
|
|
28
35
|
def initialize(endpoint)
|
29
36
|
@endpoint = endpoint.is_a?(URI) ? endpoint : URI.parse(endpoint)
|
@@ -31,13 +38,19 @@ module ActiveMerchant
|
|
31
38
|
@read_timeout = READ_TIMEOUT
|
32
39
|
@retry_safe = RETRY_SAFE
|
33
40
|
@verify_peer = VERIFY_PEER
|
41
|
+
@ca_file = CA_FILE
|
42
|
+
@ca_path = CA_PATH
|
43
|
+
@max_retries = MAX_RETRIES
|
34
44
|
@ignore_http_status = false
|
45
|
+
@ssl_version = nil
|
35
46
|
end
|
36
47
|
|
37
48
|
def request(method, body, headers = {})
|
38
|
-
|
49
|
+
request_start = Time.now.to_f
|
50
|
+
|
51
|
+
retry_exceptions(:max_retries => max_retries, :logger => logger, :tag => tag) do
|
39
52
|
begin
|
40
|
-
info "
|
53
|
+
info "connection_http_method=#{method.to_s.upcase} connection_uri=#{endpoint}", tag
|
41
54
|
|
42
55
|
result = nil
|
43
56
|
|
@@ -68,6 +81,9 @@ module ActiveMerchant
|
|
68
81
|
result
|
69
82
|
end
|
70
83
|
end
|
84
|
+
|
85
|
+
ensure
|
86
|
+
info "connection_request_total_time=%.4fs" % [Time.now.to_f - request_start], tag
|
71
87
|
end
|
72
88
|
|
73
89
|
private
|
@@ -93,13 +109,16 @@ module ActiveMerchant
|
|
93
109
|
return unless endpoint.scheme == "https"
|
94
110
|
|
95
111
|
http.use_ssl = true
|
112
|
+
http.ssl_version = ssl_version if ssl_version
|
96
113
|
|
97
114
|
if verify_peer
|
98
115
|
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
99
|
-
http.ca_file =
|
116
|
+
http.ca_file = ca_file
|
117
|
+
http.ca_path = ca_path
|
100
118
|
else
|
101
119
|
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
102
120
|
end
|
121
|
+
|
103
122
|
end
|
104
123
|
|
105
124
|
def configure_cert(http)
|
@@ -32,19 +32,25 @@ module ActiveMerchant
|
|
32
32
|
private
|
33
33
|
|
34
34
|
def retry_network_exceptions(options = {})
|
35
|
-
|
35
|
+
initial_retries = options[:max_retries] || DEFAULT_RETRIES
|
36
|
+
retries = initial_retries
|
37
|
+
request_start = nil
|
36
38
|
|
37
39
|
begin
|
38
|
-
|
40
|
+
request_start = Time.now.to_f
|
41
|
+
result = yield
|
42
|
+
log_with_retry_details(options[:logger], initial_retries-retries + 1, Time.now.to_f - request_start, "success", options[:tag])
|
43
|
+
result
|
39
44
|
rescue ActiveMerchant::RetriableConnectionError => e
|
40
45
|
retries -= 1
|
46
|
+
|
47
|
+
log_with_retry_details(options[:logger], initial_retries-retries, Time.now.to_f - request_start, e.message, options[:tag])
|
41
48
|
retry unless retries.zero?
|
42
|
-
NetworkConnectionRetries.log(options[:logger], :error, e.message, options[:tag])
|
43
49
|
raise ActiveMerchant::ConnectionError, e.message
|
44
50
|
rescue ActiveMerchant::ConnectionError => e
|
45
51
|
retries -= 1
|
52
|
+
log_with_retry_details(options[:logger], initial_retries-retries, Time.now.to_f - request_start, e.message, options[:tag])
|
46
53
|
retry if (options[:retry_safe] || retry_safe) && !retries.zero?
|
47
|
-
NetworkConnectionRetries.log(options[:logger], :error, e.message, options[:tag])
|
48
54
|
raise
|
49
55
|
end
|
50
56
|
end
|
@@ -54,5 +60,10 @@ module ActiveMerchant
|
|
54
60
|
message = "[#{tag}] #{message}"
|
55
61
|
logger.send(level, message) if logger
|
56
62
|
end
|
63
|
+
|
64
|
+
private
|
65
|
+
def log_with_retry_details(logger, attempts, time, message, tag)
|
66
|
+
NetworkConnectionRetries.log(logger, :info, "connection_attempt=%d connection_request_time=%.4fs connection_msg=\"%s\"" % [attempts, time, message], tag)
|
67
|
+
end
|
57
68
|
end
|
58
69
|
end
|
@@ -5,6 +5,9 @@ module ActiveMerchant #:nodoc:
|
|
5
5
|
base.superclass_delegating_accessor :ssl_strict
|
6
6
|
base.ssl_strict = true
|
7
7
|
|
8
|
+
base.superclass_delegating_accessor :ssl_version
|
9
|
+
base.ssl_version = nil
|
10
|
+
|
8
11
|
base.class_attribute :retry_safe
|
9
12
|
base.retry_safe = false
|
10
13
|
|
@@ -14,6 +17,9 @@ module ActiveMerchant #:nodoc:
|
|
14
17
|
base.superclass_delegating_accessor :read_timeout
|
15
18
|
base.read_timeout = 60
|
16
19
|
|
20
|
+
base.superclass_delegating_accessor :max_retries
|
21
|
+
base.max_retries = Connection::MAX_RETRIES
|
22
|
+
|
17
23
|
base.superclass_delegating_accessor :logger
|
18
24
|
base.superclass_delegating_accessor :wiredump_device
|
19
25
|
end
|
@@ -38,7 +44,9 @@ module ActiveMerchant #:nodoc:
|
|
38
44
|
connection.read_timeout = read_timeout
|
39
45
|
connection.retry_safe = retry_safe
|
40
46
|
connection.verify_peer = ssl_strict
|
47
|
+
connection.ssl_version = ssl_version
|
41
48
|
connection.logger = logger
|
49
|
+
connection.max_retries = max_retries
|
42
50
|
connection.tag = self.class.name
|
43
51
|
connection.wiredump_device = wiredump_device
|
44
52
|
|
data/lib/active_utils/version.rb
CHANGED