fetch-api 0.4.0 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/fetch/client.rb +10 -5
- data/lib/fetch/connection_pool.rb +5 -9
- data/lib/fetch/version.rb +1 -1
- data/sig/fetch/client.rbs +1 -0
- data/sig/fetch/connection_pool.rbs +0 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ff43ec8ec36d621acb463b7327146e89df97446714df7747626276bb51ff4c18
|
4
|
+
data.tar.gz: de99e42e871912c41df6a5afabb1200f662d5aeba7b6659a4483434c8ddca3c7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7b56ad7b6ac2788343928c4686f48de71e1dc57491e9e13efa5cc479f4f7f57af9d4efad3e7a79cf75fd287d683bb21ec053b69de0c66cdd39a0aef571f86c7b
|
7
|
+
data.tar.gz: 8895b9835e6637a9d4cd845708e2a133b2f6239baab1729478f16ae9f25ea3df927d678c41ebfbcc26cb3482a43c5fb616779df89a21a8a1aa634f0e18ebe392
|
data/lib/fetch/client.rb
CHANGED
@@ -15,10 +15,6 @@ module Fetch
|
|
15
15
|
class Client
|
16
16
|
include Singleton
|
17
17
|
|
18
|
-
def initialize
|
19
|
-
@pool = ConnectionPool.new
|
20
|
-
end
|
21
|
-
|
22
18
|
def fetch(resource, method: :get, headers: [], body: nil, redirect: :follow, _redirected: false)
|
23
19
|
uri = URI.parse(resource)
|
24
20
|
req = Net::HTTP.const_get(method.capitalize).new(uri)
|
@@ -45,7 +41,8 @@ module Fetch
|
|
45
41
|
req.body = body
|
46
42
|
end
|
47
43
|
|
48
|
-
|
44
|
+
# @type var uri: URI::HTTP
|
45
|
+
res = pool.with_connection(uri) { _1.request(req) }
|
49
46
|
|
50
47
|
case res
|
51
48
|
when Net::HTTPRedirection
|
@@ -66,6 +63,14 @@ module Fetch
|
|
66
63
|
|
67
64
|
private
|
68
65
|
|
66
|
+
def pool
|
67
|
+
if pool = Thread.current.thread_variable_get(:fetch_connection_pool)
|
68
|
+
pool
|
69
|
+
else
|
70
|
+
Thread.current.thread_variable_set :fetch_connection_pool, ConnectionPool.new
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
69
74
|
def to_response(url, res, redirected)
|
70
75
|
Response.new(
|
71
76
|
url: url.to_str,
|
@@ -35,7 +35,7 @@ module Fetch
|
|
35
35
|
|
36
36
|
def acquire(uri)
|
37
37
|
@mutex.synchronize {
|
38
|
-
entry = @connections[
|
38
|
+
entry = @connections[uri.origin]
|
39
39
|
|
40
40
|
if entry
|
41
41
|
entry.in_use = true
|
@@ -46,7 +46,7 @@ module Fetch
|
|
46
46
|
http.use_ssl = uri.scheme == 'https'
|
47
47
|
http.keep_alive_timeout = Fetch.config.keep_alive_timeout
|
48
48
|
|
49
|
-
@connections[
|
49
|
+
@connections[uri.origin] = Entry.new(connection: http, in_use: true)
|
50
50
|
|
51
51
|
http.start
|
52
52
|
}
|
@@ -58,7 +58,7 @@ module Fetch
|
|
58
58
|
|
59
59
|
def release(uri)
|
60
60
|
@mutex.synchronize do
|
61
|
-
if entry = @connections[
|
61
|
+
if entry = @connections[uri.origin]
|
62
62
|
entry.in_use = false
|
63
63
|
entry.last_used = Time.now
|
64
64
|
end
|
@@ -67,20 +67,16 @@ module Fetch
|
|
67
67
|
|
68
68
|
def sweep
|
69
69
|
@mutex.synchronize do
|
70
|
-
@connections.each do |
|
70
|
+
@connections.each do |origin, entry|
|
71
71
|
next if entry.in_use
|
72
72
|
|
73
73
|
if entry.last_used + Fetch.config.connection_max_idle_time < Time.now
|
74
74
|
entry.connection.finish
|
75
75
|
|
76
|
-
@connections.delete
|
76
|
+
@connections.delete origin
|
77
77
|
end
|
78
78
|
end
|
79
79
|
end
|
80
80
|
end
|
81
|
-
|
82
|
-
def key(uri)
|
83
|
-
"#{Thread.current.object_id}/#{uri.origin}".freeze
|
84
|
-
end
|
85
81
|
end
|
86
82
|
end
|
data/lib/fetch/version.rb
CHANGED
data/sig/fetch/client.rbs
CHANGED