faraday_persistent_excon 0.2.2 → 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.dockerignore +9 -0
- data/Dockerfile +34 -0
- data/README.md +11 -5
- data/docker-compose.yml +24 -0
- data/docker-entrypoint.sh +20 -0
- data/faraday_persistent_excon.gemspec +3 -3
- data/lib/faraday_persistent_excon.rb +3 -2
- data/lib/faraday_persistent_excon/adapter.rb +7 -1
- data/lib/faraday_persistent_excon/connection.rb +26 -0
- data/lib/faraday_persistent_excon/connection_pools.rb +18 -2
- data/lib/faraday_persistent_excon/perform_request.rb +7 -6
- data/lib/faraday_persistent_excon/request_options.rb +4 -2
- data/lib/faraday_persistent_excon/version.rb +1 -1
- metadata +14 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c1f85714b132f22e4e9fe9852dbfc6780b6c00f95f5e36e6b77b7f0a9be7de70
|
4
|
+
data.tar.gz: 8946a5d751ef31a98636799191c0563530be3c4ffeb3347c38adc954d294f7a9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 80df392684a609ed27fc0f4da85f73e1f5675c9a98369ffc0ee34c7310961a6052163af813d94df1986cf65f36288e1d395c2ec5b37399c0a4feaf7359f493eb
|
7
|
+
data.tar.gz: 8e111c4143ae990cf9e5bb201bd8f2360003768ea70a6e4ddaf9704efb7b22a6d609f5d58002b967790e9945162e074d127dbcac02df264d6974ff91b35e4b32
|
data/.dockerignore
ADDED
data/Dockerfile
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
FROM ruby:2.6.5-alpine
|
2
|
+
MAINTAINER Ryan Schlesinger <ryan@outstand.com>
|
3
|
+
|
4
|
+
RUN addgroup -g 1000 -S gem && \
|
5
|
+
adduser -u 1000 -S -s /bin/ash -G gem gem && \
|
6
|
+
apk add --no-cache \
|
7
|
+
ca-certificates \
|
8
|
+
tini \
|
9
|
+
su-exec \
|
10
|
+
build-base \
|
11
|
+
git \
|
12
|
+
openssh
|
13
|
+
|
14
|
+
ENV LANG en_US.UTF-8
|
15
|
+
ENV LANGUAGE en_US:en
|
16
|
+
ENV LC_ALL en_US.UTF-8
|
17
|
+
|
18
|
+
ENV BUNDLER_VERSION 2.1.4
|
19
|
+
RUN gem install bundler -v ${BUNDLER_VERSION} -i /usr/local/lib/ruby/gems/$(ls /usr/local/lib/ruby/gems) --force
|
20
|
+
|
21
|
+
WORKDIR /srv
|
22
|
+
RUN chown -R gem:gem /srv
|
23
|
+
USER gem
|
24
|
+
|
25
|
+
COPY --chown=gem:gem Gemfile faraday_persistent_excon.gemspec /srv/
|
26
|
+
COPY --chown=gem:gem lib/faraday_persistent_excon/version.rb /srv/lib/faraday_persistent_excon/
|
27
|
+
RUN git config --global push.default simple
|
28
|
+
COPY --chown=gem:gem . /srv/
|
29
|
+
|
30
|
+
USER root
|
31
|
+
COPY docker-entrypoint.sh /docker-entrypoint.sh
|
32
|
+
|
33
|
+
ENTRYPOINT ["/sbin/tini", "-g", "--", "/docker-entrypoint.sh"]
|
34
|
+
CMD ["rspec", "spec"]
|
data/README.md
CHANGED
@@ -1,12 +1,14 @@
|
|
1
1
|
# FaradayPersistentExcon
|
2
2
|
|
3
|
+
[![Code Climate](https://codeclimate.com/github/aceofsales/faraday_persistent_excon/badges/gpa.svg)](https://codeclimate.com/github/aceofsales/faraday_persistent_excon)
|
4
|
+
|
3
5
|
Adds configurable connection pools per host for persistent http connections
|
4
6
|
|
5
7
|
## Status
|
6
8
|
|
7
|
-
**
|
9
|
+
**Beta**
|
8
10
|
|
9
|
-
All testing for this gem is currently in the form of bench testing. We're
|
11
|
+
All testing for this gem is currently in the form of bench testing. We're using this gem in our production stack.
|
10
12
|
|
11
13
|
## Installation
|
12
14
|
|
@@ -33,15 +35,19 @@ Excon.defaults[:tcp_nodelay] = true
|
|
33
35
|
Faraday.default_adapter = :persistent_excon
|
34
36
|
FaradayPersistentExcon.connection_pools = {
|
35
37
|
'https://search.example.com' => { size: 5 },
|
36
|
-
'http://localhost:9200' => { size: 10, timeout: 2 }
|
38
|
+
'http://localhost:9200' => { size: 10, timeout: 2, idle_timeout: 300 }
|
37
39
|
}
|
38
40
|
```
|
39
41
|
|
40
42
|
## Development
|
41
43
|
|
42
|
-
|
44
|
+
- `docker-compose build --pull`
|
45
|
+
- `docker-compose run --rm faraday_persistent_excon` to run specs
|
43
46
|
|
44
|
-
To
|
47
|
+
To release a new version:
|
48
|
+
- Update the version number in `version.rb` and commit the result.
|
49
|
+
- `docker-compose build --pull`
|
50
|
+
- `docker-compose run --rm release`
|
45
51
|
|
46
52
|
## Contributing
|
47
53
|
|
data/docker-compose.yml
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
version: '3.6'
|
2
|
+
services:
|
3
|
+
faraday_persistent_excon:
|
4
|
+
build: .
|
5
|
+
image: outstand/faraday_persistent_excon:dev
|
6
|
+
stdin_open: true
|
7
|
+
tty: true
|
8
|
+
volumes:
|
9
|
+
- bundler-data:/usr/local/bundle
|
10
|
+
- .:/srv
|
11
|
+
release:
|
12
|
+
image: outstand/faraday_persistent_excon:dev
|
13
|
+
stdin_open: true
|
14
|
+
tty: true
|
15
|
+
command: rake release
|
16
|
+
volumes:
|
17
|
+
- bundler-data:/usr/local/bundle
|
18
|
+
- ~/.gitconfig:/home/gem/.gitconfig
|
19
|
+
- ~/.gitconfig.user:/home/gem/.gitconfig.user
|
20
|
+
- ~/.ssh/id_rsa:/home/gem/.ssh/id_rsa
|
21
|
+
- ~/.gem:/home/gem/.gem
|
22
|
+
|
23
|
+
volumes:
|
24
|
+
bundler-data:
|
@@ -0,0 +1,20 @@
|
|
1
|
+
#!/bin/sh
|
2
|
+
set -e
|
3
|
+
|
4
|
+
if [ "$(which "$1")" = '' ]; then
|
5
|
+
if [ "$(ls -A /usr/local/bundle/bin)" = '' ]; then
|
6
|
+
echo 'command not in path and bundler not initialized'
|
7
|
+
echo 'running bundle install'
|
8
|
+
su-exec gem bundle install
|
9
|
+
fi
|
10
|
+
fi
|
11
|
+
|
12
|
+
if [ "$1" = 'bundle' ]; then
|
13
|
+
set -- su-exec gem "$@"
|
14
|
+
elif ls /usr/local/bundle/bin | grep -q "\b$1\b"; then
|
15
|
+
set -- su-exec gem bundle exec "$@"
|
16
|
+
|
17
|
+
su-exec gem ash -c 'bundle check || bundle install'
|
18
|
+
fi
|
19
|
+
|
20
|
+
exec "$@"
|
@@ -18,13 +18,13 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
-
spec.add_dependency "faraday"
|
21
|
+
spec.add_dependency "faraday", ">= 0.10.0"
|
22
22
|
spec.add_dependency "excon"
|
23
23
|
spec.add_dependency "connection_pool"
|
24
24
|
spec.add_dependency "activesupport"
|
25
25
|
spec.add_dependency "thread_safe"
|
26
26
|
|
27
|
-
spec.add_development_dependency "bundler", "~> 1
|
28
|
-
spec.add_development_dependency "rake", "~>
|
27
|
+
spec.add_development_dependency "bundler", "~> 2.1"
|
28
|
+
spec.add_development_dependency "rake", "~> 13.0"
|
29
29
|
spec.add_development_dependency "rspec"
|
30
30
|
end
|
@@ -9,6 +9,7 @@ require 'faraday_persistent_excon/perform_request'
|
|
9
9
|
require 'faraday_persistent_excon/adapter'
|
10
10
|
require 'faraday_persistent_excon/request_options'
|
11
11
|
require 'faraday_persistent_excon/connection_pools'
|
12
|
+
require 'faraday_persistent_excon/connection'
|
12
13
|
|
13
14
|
module FaradayPersistentExcon
|
14
15
|
class << self
|
@@ -17,7 +18,7 @@ module FaradayPersistentExcon
|
|
17
18
|
attr_accessor :connection_pools
|
18
19
|
attr_accessor :idempotent_methods
|
19
20
|
attr_accessor :retry_idempotent_methods
|
20
|
-
attr_accessor :
|
21
|
+
attr_accessor :idle_timeout
|
21
22
|
end
|
22
23
|
|
23
24
|
self.excon_options = {}
|
@@ -25,7 +26,7 @@ module FaradayPersistentExcon
|
|
25
26
|
self.connection_pools = {}
|
26
27
|
self.idempotent_methods = %w[GET HEAD PUT DELETE OPTIONS TRACE]
|
27
28
|
self.retry_idempotent_methods = true
|
28
|
-
self.
|
29
|
+
self.idle_timeout = 60
|
29
30
|
end
|
30
31
|
|
31
32
|
Faraday::Adapter.register_middleware persistent_excon: ->{ FaradayPersistentExcon::Adapter }
|
@@ -12,7 +12,13 @@ module FaradayPersistentExcon
|
|
12
12
|
|
13
13
|
def perform_request(env)
|
14
14
|
response = FaradayPersistentExcon.perform_request_class.new.call env
|
15
|
-
save_response(
|
15
|
+
save_response(
|
16
|
+
env,
|
17
|
+
response.status.to_i,
|
18
|
+
response.body,
|
19
|
+
response.headers,
|
20
|
+
response.reason_phrase
|
21
|
+
)
|
16
22
|
end
|
17
23
|
end
|
18
24
|
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module FaradayPersistentExcon
|
2
|
+
class Connection
|
3
|
+
attr_accessor :excon
|
4
|
+
attr_accessor :last_use
|
5
|
+
attr_accessor :idle_timeout
|
6
|
+
|
7
|
+
def initialize(excon:, idle_timeout:)
|
8
|
+
@excon = excon
|
9
|
+
@idle_timeout = idle_timeout
|
10
|
+
end
|
11
|
+
|
12
|
+
def reset
|
13
|
+
excon.reset
|
14
|
+
end
|
15
|
+
|
16
|
+
def expired?
|
17
|
+
return false if last_use.nil? || idle_timeout == 0
|
18
|
+
|
19
|
+
Time.now.utc - last_use > idle_timeout
|
20
|
+
end
|
21
|
+
|
22
|
+
def used!
|
23
|
+
self.last_use = Time.now.utc
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -10,7 +10,14 @@ module FaradayPersistentExcon
|
|
10
10
|
pool = self.connection_pool_for(url)
|
11
11
|
|
12
12
|
if pool
|
13
|
-
pool.with
|
13
|
+
pool.with do |conn|
|
14
|
+
begin
|
15
|
+
conn.reset if conn.expired?
|
16
|
+
block.call(conn.excon)
|
17
|
+
ensure
|
18
|
+
conn.used!
|
19
|
+
end
|
20
|
+
end
|
14
21
|
else
|
15
22
|
# No pool configured. Use normal connection
|
16
23
|
block.call(::Excon.new(url, persistent: false))
|
@@ -23,7 +30,16 @@ module FaradayPersistentExcon
|
|
23
30
|
if config
|
24
31
|
self.__pools.fetch_or_store(url) do
|
25
32
|
::ConnectionPool.new(config) do
|
26
|
-
|
33
|
+
Connection.new(
|
34
|
+
excon: ::Excon.new(
|
35
|
+
url,
|
36
|
+
{
|
37
|
+
persistent: true,
|
38
|
+
thread_safe_sockets: false
|
39
|
+
}.merge(config.fetch(:connection_options, {}))
|
40
|
+
),
|
41
|
+
idle_timeout: config.fetch(:idle_timeout, FaradayPersistentExcon.idle_timeout)
|
42
|
+
)
|
27
43
|
end
|
28
44
|
end
|
29
45
|
end
|
@@ -11,15 +11,16 @@ module FaradayPersistentExcon
|
|
11
11
|
end
|
12
12
|
|
13
13
|
rescue ::Excon::Errors::SocketError => err
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
14
|
+
case err.message
|
15
|
+
when /\btimeout\b/
|
16
|
+
raise Faraday::TimeoutError, err
|
17
|
+
when /\bcertificate\b/
|
18
|
+
raise Faraday::SSLError, err
|
18
19
|
else
|
19
|
-
raise Faraday::
|
20
|
+
raise Faraday::ConnectionFailed, err
|
20
21
|
end
|
21
22
|
rescue ::Excon::Errors::Timeout => err
|
22
|
-
raise Faraday::
|
23
|
+
raise Faraday::TimeoutError, err
|
23
24
|
end
|
24
25
|
|
25
26
|
protected
|
@@ -12,6 +12,9 @@ module FaradayPersistentExcon
|
|
12
12
|
opts[:client_key] = ssl[:client_key] if ssl[:client_key]
|
13
13
|
opts[:certificate] = ssl[:certificate] if ssl[:certificate]
|
14
14
|
opts[:private_key] = ssl[:private_key] if ssl[:private_key]
|
15
|
+
opts[:ssl_version] = ssl[:version] if ssl[:version]
|
16
|
+
opts[:ssl_min_version] = ssl[:min_version] if ssl[:min_version]
|
17
|
+
opts[:ssl_max_version] = ssl[:max_version] if ssl[:max_version]
|
15
18
|
end
|
16
19
|
|
17
20
|
if ( req = env[:request] )
|
@@ -23,12 +26,12 @@ module FaradayPersistentExcon
|
|
23
26
|
|
24
27
|
if req[:open_timeout]
|
25
28
|
opts[:connect_timeout] = req[:open_timeout]
|
26
|
-
opts[:write_timeout] = req[:open_timeout]
|
27
29
|
end
|
28
30
|
|
29
31
|
if req[:proxy]
|
30
32
|
opts[:proxy] = {
|
31
33
|
host: req[:proxy][:uri].host,
|
34
|
+
hostname: req[:proxy][:uri].hostname,
|
32
35
|
port: req[:proxy][:uri].port,
|
33
36
|
scheme: req[:proxy][:uri].scheme,
|
34
37
|
user: req[:proxy][:user],
|
@@ -39,7 +42,6 @@ module FaradayPersistentExcon
|
|
39
42
|
|
40
43
|
if FaradayPersistentExcon.retry_idempotent_methods && FaradayPersistentExcon.idempotent_methods.include?(env[:method].to_s.upcase)
|
41
44
|
opts[:idempotent] = true
|
42
|
-
opts[:retry_limit] = FaradayPersistentExcon.retry_limit
|
43
45
|
end
|
44
46
|
|
45
47
|
opts
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: faraday_persistent_excon
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan Schlesinger
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-06-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 0.10.0
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 0.10.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: excon
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -86,28 +86,28 @@ dependencies:
|
|
86
86
|
requirements:
|
87
87
|
- - "~>"
|
88
88
|
- !ruby/object:Gem::Version
|
89
|
-
version: '1
|
89
|
+
version: '2.1'
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
94
|
- - "~>"
|
95
95
|
- !ruby/object:Gem::Version
|
96
|
-
version: '1
|
96
|
+
version: '2.1'
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
98
|
name: rake
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|
100
100
|
requirements:
|
101
101
|
- - "~>"
|
102
102
|
- !ruby/object:Gem::Version
|
103
|
-
version: '
|
103
|
+
version: '13.0'
|
104
104
|
type: :development
|
105
105
|
prerelease: false
|
106
106
|
version_requirements: !ruby/object:Gem::Requirement
|
107
107
|
requirements:
|
108
108
|
- - "~>"
|
109
109
|
- !ruby/object:Gem::Version
|
110
|
-
version: '
|
110
|
+
version: '13.0'
|
111
111
|
- !ruby/object:Gem::Dependency
|
112
112
|
name: rspec
|
113
113
|
requirement: !ruby/object:Gem::Requirement
|
@@ -129,18 +129,23 @@ executables: []
|
|
129
129
|
extensions: []
|
130
130
|
extra_rdoc_files: []
|
131
131
|
files:
|
132
|
+
- ".dockerignore"
|
132
133
|
- ".gitignore"
|
133
134
|
- ".rspec"
|
134
135
|
- ".travis.yml"
|
136
|
+
- Dockerfile
|
135
137
|
- Gemfile
|
136
138
|
- LICENSE.md
|
137
139
|
- README.md
|
138
140
|
- Rakefile
|
139
141
|
- bin/console
|
140
142
|
- bin/setup
|
143
|
+
- docker-compose.yml
|
144
|
+
- docker-entrypoint.sh
|
141
145
|
- faraday_persistent_excon.gemspec
|
142
146
|
- lib/faraday_persistent_excon.rb
|
143
147
|
- lib/faraday_persistent_excon/adapter.rb
|
148
|
+
- lib/faraday_persistent_excon/connection.rb
|
144
149
|
- lib/faraday_persistent_excon/connection_pools.rb
|
145
150
|
- lib/faraday_persistent_excon/perform_request.rb
|
146
151
|
- lib/faraday_persistent_excon/request_options.rb
|
@@ -163,8 +168,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
163
168
|
- !ruby/object:Gem::Version
|
164
169
|
version: '0'
|
165
170
|
requirements: []
|
166
|
-
|
167
|
-
rubygems_version: 2.4.7
|
171
|
+
rubygems_version: 3.0.3
|
168
172
|
signing_key:
|
169
173
|
specification_version: 4
|
170
174
|
summary: Persistent connections with faraday and excon
|