restify 1.4.1 → 1.4.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0ead12f1e1ea5dc63535ff85782767f164bc8092fc2fad879c6efab1852b18fd
4
- data.tar.gz: 37d9f08cf939f668d9317f5ec4a8fa564fa908b23686c2f42904b1af911b012d
3
+ metadata.gz: 218f1484655662a863e6d65ae81f8f1cfe5e3127f9c795748b150236fe137225
4
+ data.tar.gz: 440710396a4f3879ad78766ef074edc85a51c5e40b7b8a3ac685e41b5060abf3
5
5
  SHA512:
6
- metadata.gz: 04a147ae63f21761d3d86c4a5b57ed0006ce1b54c72ea9b89f4cc69fbb9fc560d2a84c605137cc1b4e8ac8d409d5f7e8707db08bb726c350fd050fcaecc93a68
7
- data.tar.gz: b1382be6253e083bda252c8975ec2abe45b86c5af300fc878f6ba6ca0533cb34cf07be6553432c2167dba74aba558e561b2404e1d0639f07b33b997a9c413114
6
+ metadata.gz: 1d065b2b0b1080d937e311dcc7f820c41fad880262112887d57e5051888a069244809b3dd3e14307ce553277c82b8d56f602f5b891178f3df78b82aa45c13655
7
+ data.tar.gz: a4fd01db3ece23055dc2b3d4c297f5caf06bb93294cc4e9cd79a72fcca87340014d5f03b90603be32a2dff8006df6fb3a6375056258cae2e669643dca4024ef1
@@ -1,5 +1,10 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.4.3
4
+
5
+ * Add advanced logging capabilities using logging gem
6
+ * Improve compatibility with webmocks returning `nil` as headers
7
+
3
8
  ## 1.4.1
4
9
 
5
10
  * Fix possible deadlock issues
@@ -12,6 +12,8 @@ require 'addressable/template'
12
12
  #
13
13
  module Restify
14
14
  require 'restify/error'
15
+ require 'restify/logging'
16
+
15
17
  require 'restify/promise'
16
18
  require 'restify/registry'
17
19
  require 'restify/global'
@@ -27,6 +29,7 @@ module Restify
27
29
 
28
30
  module Adapter
29
31
  require 'restify/adapter/base'
32
+ require 'restify/adapter/typhoeus'
30
33
  end
31
34
 
32
35
  module Processors
@@ -34,8 +37,6 @@ module Restify
34
37
  require 'restify/processors/json'
35
38
  end
36
39
 
37
- require 'restify/railtie' if defined?(Rails::Railtie)
38
-
39
40
  PROCESSORS = [Processors::Json].freeze
40
41
 
41
42
  extend Global
@@ -6,8 +6,7 @@ require 'em-http-request'
6
6
  module Restify
7
7
  module Adapter
8
8
  class PooledEM < Base
9
- # rubocop:disable RedundantFreeze
10
- LOG_PROGNAME = 'restify.adapter.pooled-em'.freeze
9
+ include Logging
11
10
 
12
11
  # This class maintains a pool of connection objects, grouped by origin,
13
12
  # and ensures limits for total parallel requests and per-origin requests.
@@ -16,6 +15,8 @@ module Restify
16
15
  # When any of them are checked out for usage, it counts the usages to
17
16
  # prevent constraints being broken.
18
17
  class Pool
18
+ include Logging
19
+
19
20
  def initialize(size: 32, per_host: 6, connect_timeout: 2, inactivity_timeout: 10)
20
21
  @size = size
21
22
  @per_host = per_host
@@ -64,7 +65,7 @@ module Restify
64
65
  @available.unshift(conn) if @available.size < @size
65
66
  @used -= 1 if @used > 0
66
67
 
67
- Restify.logger.debug(LOG_PROGNAME) do
68
+ logger.debug do
68
69
  "[#{conn.uri}] Released to pool (#{@available.size}/#{@used}/#{size})"
69
70
  end
70
71
 
@@ -76,7 +77,7 @@ module Restify
76
77
  def remove(conn)
77
78
  close(conn)
78
79
 
79
- Restify.logger.debug(LOG_PROGNAME) do
80
+ logger.debug do
80
81
  "[#{conn.uri}] Removed from pool (#{@available.size}/#{@used}/#{size})"
81
82
  end
82
83
 
@@ -121,7 +122,7 @@ module Restify
121
122
  def reuse_connection(index, origin)
122
123
  @used += 1
123
124
  @available.delete_at(index).tap do
124
- Restify.logger.debug(LOG_PROGNAME) do
125
+ logger.debug do
125
126
  "[#{origin}] Take connection from pool " \
126
127
  "(#{@available.size}/#{@used}/#{size})"
127
128
  end
@@ -135,7 +136,7 @@ module Restify
135
136
 
136
137
  @used += 1
137
138
  new(origin).tap do
138
- Restify.logger.debug(LOG_PROGNAME) do
139
+ logger.debug do
139
140
  "[#{origin}] Add new connection to pool " \
140
141
  "(#{@available.size}/#{@used}/#{size})"
141
142
  end
@@ -145,14 +146,14 @@ module Restify
145
146
  def close_oldest
146
147
  close(@available.pop)
147
148
 
148
- Restify.logger.debug(LOG_PROGNAME) do
149
+ logger.debug do
149
150
  "[#{origin}] Closed oldest connection in pool " \
150
151
  "(#{@available.size}/#{@used}/#{size})"
151
152
  end
152
153
  end
153
154
 
154
155
  def queue(defer)
155
- Restify.logger.debug(LOG_PROGNAME) do
156
+ logger.debug do
156
157
  "[#{origin}] Wait for free slot " \
157
158
  "(#{@available.size}/#{@used}/#{size})"
158
159
  end
@@ -161,7 +162,7 @@ module Restify
161
162
  end
162
163
 
163
164
  def new(origin)
164
- Restify.logger.debug(LOG_PROGNAME) do
165
+ logger.debug do
165
166
  "Connect to '#{origin}' " \
166
167
  "(#{@connect_timeout}/#{@inactivity_timeout})..."
167
168
  end
@@ -263,7 +264,7 @@ module Restify
263
264
  begin
264
265
  EventMachine.run {}
265
266
  rescue => e
266
- puts "#{self.class} -> #{e}\n#{e.backtrace.join("\n")}"
267
+ logger.error(e)
267
268
  raise e
268
269
  end
269
270
  end
@@ -5,8 +5,7 @@ require 'typhoeus'
5
5
  module Restify
6
6
  module Adapter
7
7
  class Typhoeus < Base
8
- # rubocop:disable RedundantFreeze
9
- LOG_PROGNAME = 'restify.adapter.typhoeus'.freeze
8
+ include Logging
10
9
 
11
10
  attr_reader :sync
12
11
 
@@ -27,11 +26,12 @@ module Restify
27
26
 
28
27
  def call_native(request, writer)
29
28
  @mutex.synchronize do
29
+ logger.debug { "[#{request.object_id}] Queue request #{request}" }
30
30
  @hydra.queue convert(request, writer)
31
31
  @hydra.dequeue_many
32
32
  end
33
33
 
34
- sync? ? @hydra.run : start
34
+ sync? ? @hydra.run : thread.run
35
35
  end
36
36
 
37
37
  def queued?
@@ -53,6 +53,8 @@ module Restify
53
53
  connecttimeout: request.timeout
54
54
 
55
55
  req.on_complete do |response|
56
+ logger.debug { "[#{request.object_id}] Completed: #{response.code}" }
57
+
56
58
  if response.timed_out?
57
59
  writer.reject Restify::Timeout.new request
58
60
  elsif response.code == 0
@@ -75,26 +77,37 @@ module Restify
75
77
  end
76
78
 
77
79
  def convert_headers(headers)
78
- headers.each_with_object({}) do |header, memo|
80
+ return {} unless headers.respond_to?(:each_pair)
81
+
82
+ headers.each_pair.each_with_object({}) do |header, memo|
79
83
  memo[header[0].upcase.tr('-', '_')] = header[1]
80
84
  end
81
85
  end
82
86
 
83
- def start
84
- thread.run
87
+ def thread
88
+ if @thread.nil? || !@thread.status
89
+ # Recreate thread if nil or dead
90
+ @thread = Thread.new { _loop }
91
+ end
92
+
93
+ @thread
85
94
  end
86
95
 
87
- def thread
88
- @thread ||= Thread.new do
89
- loop do
90
- begin
91
- @hydra.run
92
- Thread.stop unless queued?
93
- rescue StandardError => e
94
- puts "#{self.class}: #{e.message}"
95
- end
96
- end
96
+ def _loop
97
+ loop { _run }
98
+ end
99
+
100
+ def _run
101
+ if queued?
102
+ logger.debug { 'Run hydra' }
103
+ @hydra.run
104
+ logger.debug { 'Hydra finished' }
105
+ else
106
+ logger.debug { 'Pause hydra thread' }
107
+ Thread.stop
97
108
  end
109
+ rescue StandardError => e
110
+ logger.error(e)
98
111
  end
99
112
  end
100
113
  end
@@ -10,7 +10,6 @@ module Restify
10
10
 
11
11
  def adapter
12
12
  @adapter ||= begin
13
- require 'restify/adapter/typhoeus'
14
13
  Restify::Adapter::Typhoeus.new
15
14
  end
16
15
  end
@@ -31,13 +30,11 @@ module Restify
31
30
  end
32
31
 
33
32
  def logger
34
- @logger ||= ::Logger.new(STDOUT).tap do |logger|
35
- logger.level = :info
36
- end
33
+ ::Logging.logger[Restify]
37
34
  end
38
35
 
39
36
  def logger=(logger)
40
- @logger = logger
37
+
41
38
  end
42
39
 
43
40
  private
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'logging'
4
+
5
+ module Restify
6
+ module Logging
7
+ def logger
8
+ @logger ||= ::Logging.logger[self]
9
+ end
10
+ end
11
+ end
@@ -44,5 +44,9 @@ module Restify
44
44
  JSON.dump(data) unless data.nil?
45
45
  end
46
46
  end
47
+
48
+ def to_s
49
+ "#<#{self.class} #{method.upcase} #{uri}>"
50
+ end
47
51
  end
48
52
  end
@@ -4,7 +4,7 @@ module Restify
4
4
  module VERSION
5
5
  MAJOR = 1
6
6
  MINOR = 4
7
- PATCH = 1
7
+ PATCH = 3
8
8
  STAGE = nil
9
9
  STRING = [MAJOR, MINOR, PATCH, STAGE].reject(&:nil?).join('.').freeze
10
10
 
@@ -41,7 +41,10 @@ RSpec.configure do |config|
41
41
  config.order = 'random'
42
42
 
43
43
  config.before(:each) do
44
- Restify.logger.level = Logger::DEBUG
44
+ Ethon.logger = ::Logging.logger[Ethon] if defined?(Ethon)
45
+
46
+ ::Logging.logger.root.level = :debug
47
+ ::Logging.logger.root.add_appenders ::Logging.appenders.stdout
45
48
  end
46
49
 
47
50
  config.after(:each) do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: restify
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.1
4
+ version: 1.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Graichen
@@ -94,6 +94,20 @@ dependencies:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
96
  version: '1.3'
97
+ - !ruby/object:Gem::Dependency
98
+ name: logging
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '2.0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '2.0'
97
111
  - !ruby/object:Gem::Dependency
98
112
  name: bundler
99
113
  requirement: !ruby/object:Gem::Requirement
@@ -129,10 +143,10 @@ files:
129
143
  - lib/restify/error.rb
130
144
  - lib/restify/global.rb
131
145
  - lib/restify/link.rb
146
+ - lib/restify/logging.rb
132
147
  - lib/restify/processors/base.rb
133
148
  - lib/restify/processors/json.rb
134
149
  - lib/restify/promise.rb
135
- - lib/restify/railtie.rb
136
150
  - lib/restify/registry.rb
137
151
  - lib/restify/relation.rb
138
152
  - lib/restify/request.rb
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Restify
4
- class Railtie < Rails::Railtie
5
- initializer 'restify.logger' do
6
- config.after_initialize do
7
- Restify.logger = Rails.logger
8
- end
9
- end
10
- end
11
- end