Skip to content

Commit

Permalink
Merge pull request #33 from Tout/PLAT-3000
Browse files Browse the repository at this point in the history
PLAT-3000: Remove un-threadsafe typhoeus gem
  • Loading branch information
will3216 committed May 2, 2018
2 parents 502fef4 + b955a2d commit abd9437
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 58 deletions.
26 changes: 10 additions & 16 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
trubl (2.0.1)
trubl (2.0.2)
activesupport
faraday (~> 0.12.2)
hashie (~> 3.5.6)
Expand All @@ -10,13 +10,12 @@ PATH
json (~> 1.8.6)
oauth2
rake
typhoeus (= 0.6.8)
yajl-ruby

GEM
remote: https://rubygems.org/
specs:
activesupport (5.1.4)
activesupport (5.1.5)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (~> 0.7)
minitest (~> 5.1)
Expand All @@ -29,27 +28,24 @@ GEM
safe_yaml (~> 1.0.0)
diff-lcs (1.3)
docile (1.1.5)
ethon (0.11.0)
ffi (>= 1.3.0)
faraday (0.12.2)
multipart-post (>= 1.2, < 3)
ffi (1.9.18)
hashdiff (0.3.6)
hashie (3.5.7)
httmultiparty (0.3.16)
httparty (>= 0.7.3)
mimemagic
multipart-post
httparty (0.15.6)
httparty (0.15.7)
multi_xml (>= 0.5.2)
i18n (0.9.1)
i18n (0.9.5)
concurrent-ruby (~> 1.0)
json (1.8.6)
jwt (1.5.6)
method_source (0.9.0)
mimemagic (0.3.2)
minitest (5.10.3)
multi_json (1.12.2)
minitest (5.11.3)
multi_json (1.13.1)
multi_xml (0.6.0)
multipart-post (2.0.0)
oauth2 (1.4.0)
Expand All @@ -62,8 +58,8 @@ GEM
coderay (~> 1.1.0)
method_source (~> 0.9.0)
public_suffix (3.0.0)
rack (2.0.3)
rake (12.3.0)
rack (2.0.4)
rake (12.3.1)
rspec (3.6.0)
rspec-core (~> 3.6.0)
rspec-expectations (~> 3.6.0)
Expand All @@ -89,9 +85,7 @@ GEM
simplecov-html (0.10.2)
thread_safe (0.3.6)
timecop (0.9.1)
typhoeus (0.6.8)
ethon (>= 0.7.0)
tzinfo (1.2.4)
tzinfo (1.2.5)
thread_safe (~> 0.1)
webmock (1.24.6)
addressable (>= 2.3.6)
Expand All @@ -113,4 +107,4 @@ DEPENDENCIES
webmock (~> 1.24)

BUNDLED WITH
1.16.0
1.16.1
34 changes: 2 additions & 32 deletions lib/trubl/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,7 @@ def multi_request(method, requests=[], opts={})

Trubl.logger.info("Trubl::Client multi-#{method}-ing #{requests.join(', ')} with headers #{headers}")

action = RUBY_ENGINE == 'ruby' ? :multi_request_typhoeus : :multi_request_threaded

self.send(action, method, requests, opts).collect do |response|
self.send(:multi_request_threaded, method, requests, opts).collect do |response|
response.body.force_encoding("utf-8") if response.body and response.body.respond_to?(:force_encoding)
response
end
Expand All @@ -231,7 +229,7 @@ def multi_request_threaded(method, requests=[], opts={})
opts[:max_concurrency].times.map do
Thread.new(requests, responses) do |requests, responses|
while request = mutex.synchronize { requests.pop }
response = HTTParty.send(method, full_url(request[:path]), {headers: headers}.merge(request[:params] || {} ))
response = HTTParty.send(method, full_url(request[:path]), {headers: headers}.merge(query: request[:query] || {} ))
mutex.synchronize { responses << response }
end
end
Expand All @@ -240,34 +238,6 @@ def multi_request_threaded(method, requests=[], opts={})
responses
end

def multi_request_typhoeus(method, requests=[], opts={})
# https://github.com/lostisland/faraday/wiki/Parallel-requests
# https://github.com/typhoeus/typhoeus/issues/226
hydra = Typhoeus::Hydra.new(max_concurrency: opts[:max_concurrency])

conn = Faraday.new(url: api_uri_root, parallel_manager: hydra) do |builder|
builder.request :url_encoded
builder.adapter :typhoeus
end

requests = requests.collect do |request|
if request.is_a?(String)
{path: request, params: {}}
else
request.reverse_merge params: {}
end
end

[].tap do |responses|
conn.in_parallel do
requests.each do |request|
path = [request[:path], request[:query].try(:to_query)].compact.join('?')
responses << conn.send(method, path, request[:params], headers)
end
end
end
end

def set_logger(level)
Trubl.logger(level)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/trubl/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Trubl
class Version
MAJOR = 2 unless defined? Trubl::Version::MAJOR
MINOR = 0 unless defined? Trubl::Version::MINOR
PATCH = 2 unless defined? Trubl::Version::PATCH
PATCH = 3 unless defined? Trubl::Version::PATCH

class << self

Expand Down
10 changes: 2 additions & 8 deletions spec/trubl/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,27 +113,21 @@
end

describe 'response status codes' do
subject do
RUBY_ENGINE == 'ruby' ? multi_request.map(&:status).sort : multi_request.map(&:code).sort
subject do
multi_request.map(&:code).sort
end
it { should == uids.size.times.collect { |i| 200+i } }
end

context 'with a blank request list' do
let(:requests) { nil }
before do
Typhoeus::Hydra.should_not_receive(:new) if RUBY_ENGINE == 'ruby'
end
it { should == [] }
end

end

context 'with an invalid request method' do
let(:method) { :unsupported }
before do
Typhoeus::Hydra.should_not_receive(:new) if RUBY_ENGINE == 'ruby'
end
it { should == [] }
end
end
Expand Down
1 change: 0 additions & 1 deletion trubl.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ Gem::Specification.new do |spec|
spec.add_dependency 'rake'

if RUBY_ENGINE == 'ruby'
spec.add_dependency 'typhoeus', '0.6.8'
spec.add_dependency 'yajl-ruby'
end

Expand Down

0 comments on commit abd9437

Please sign in to comment.