Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a connection pool for faraday connections #63

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,32 @@ Spyke::Base.connection = Faraday.new(url: 'http://api.com') do |c|
end
```

You can also assign a connection pool instead of a plain connection with
something like:

```ruby
# config/initializers/spyke.rb

class JSONParser < Faraday::Response::Middleware
def parse(body)
json = MultiJson.load(body, symbolize_keys: true)
{
data: json[:result],
metadata: json[:extra],
errors: json[:errors]
}
end
end

Spyke::Base.connection_pool = ConnectionPool.new(size: 5, timeout: 5) do
Faraday.new(url: 'http://api.com') do |c|
c.request :json
c.use JSONParser
c.adapter Faraday.default_adapter
end
end
```

## Usage

Adding a class and inheriting from `Spyke::Base` will allow you to interact with the remote service:
Expand Down
25 changes: 16 additions & 9 deletions lib/spyke/http.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'connection_pool'
require 'faraday'
require 'faraday_middleware'
require 'spyke/config'
Expand All @@ -10,7 +11,7 @@ module Http
METHODS = %i{ get post put patch delete }

included do
class_attribute :connection, instance_accessor: false
class_attribute :connection_pool, instance_accessor: false
end

module ClassMethods
Expand All @@ -20,18 +21,24 @@ module ClassMethods
end
end

def connection=(connection)
self.connection_pool = ConnectionPool.new(size: 1) { connection }
end

def request(method, path, params = {})
ActiveSupport::Notifications.instrument('request.spyke', method: method) do |payload|
response = connection.send(method) do |request|
if method == :get
request.url path.to_s, params
else
request.url path.to_s
request.body = params
connection_pool.with do |connection|
response = connection.send(method) do |request|
if method == :get
request.url path.to_s, params
else
request.url path.to_s
request.body = params
end
end
payload[:url], payload[:status] = response.env.url, response.status
Result.new_from_response(response)
end
payload[:url], payload[:status] = response.env.url, response.status
Result.new_from_response(response)
end
end

Expand Down
1 change: 1 addition & 0 deletions spyke.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Gem::Specification.new do |spec|
spec.add_dependency 'faraday', '>= 0.9.0', '< 2.0'
spec.add_dependency 'faraday_middleware', '>= 0.9.1', '< 2.0'
spec.add_dependency 'uri_template', '>= 0.7.0', '< 2.0'
spec.add_dependency 'connection_pool', '>= 2.2.0', '< 2.3.0'

spec.add_development_dependency 'bundler', '~> 1.6'
spec.add_development_dependency 'coveralls', '~> 0.7'
Expand Down
6 changes: 3 additions & 3 deletions test/orm_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,14 @@ def test_scoped_destroy_class_method_without_param
end

def test_relative_uris
previous = Spyke::Base.connection.url_prefix
Spyke::Base.connection.url_prefix = 'http://sushi.com/api/v2/'
previous = Spyke::TestConnection.url_prefix
Spyke::TestConnection.url_prefix = 'http://sushi.com/api/v2/'

endpoint = stub_request(:get, 'http://sushi.com/api/v2/recipes')
Recipe.all.to_a
assert_requested endpoint

Spyke::Base.connection.url_prefix = previous
Spyke::TestConnection.url_prefix = previous
end
end
end
4 changes: 3 additions & 1 deletion test/support/fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ def parse(body)
end
end

Spyke::Base.connection = Faraday.new(url: 'http://sushi.com') do |faraday|
Spyke::TestConnection = Faraday.new(url: 'http://sushi.com') do |faraday|
faraday.request :json
faraday.use JSONParser
faraday.adapter Faraday.default_adapter
end

Spyke::Base.connection = Spyke::TestConnection

# Test classes
class Recipe < Spyke::Base
has_many :groups
Expand Down