fetch-api 0.4.1 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +7 -7
- data/lib/fetch/client.rb +5 -2
- data/lib/fetch/config.rb +1 -1
- data/lib/fetch/connection_pool.rb +7 -5
- data/lib/fetch/version.rb +1 -1
- data/lib/fetch.rb +2 -2
- data/sig/fetch/config.rbs +1 -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: 4217a0ddb4ec43da71a630ba5de24d9a6a02af0f5cf2b39286702c1cc512c699
|
4
|
+
data.tar.gz: 05abc4f266fab3577c2223de54957b8c6a84e7b3ccc24df428f40e771c1e311d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c7caa0c96f93f03ae799a121fc7f25410f401c9c192a7546f97627c760cb638627b79d01b0911ed8a4f2ebaacdaa619e86e84a2b88443a721f066a4a9fd4c1d9
|
7
|
+
data.tar.gz: 826e3c4ff75b7b3c3f0886949a6fe141cdd360fd31da86b42b08aabde98b0a608ac6f51561b65819b65622a1a2f344f5e6784505bafb990d0aad300ac8f378b6
|
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
# Fetch
|
1
|
+
# Fetch::API
|
2
2
|
|
3
3
|
Ruby's Net::HTTP is very powerful, but has a complicated API. OpenURI is easy to use, but has limited functionality. Third-party HTTP clients each have different APIs, and it can sometimes be difficult to learn how to use them.
|
4
4
|
|
5
|
-
There is one HTTP client that we can all use. It is the browser's Fetch API.
|
5
|
+
There is one HTTP client that we can all use. It is the browser's Fetch API. Fetch::API is an HTTP client for Ruby that can be used in a way that is similar to the Fetch API.
|
6
6
|
|
7
7
|
This is not intended to be a complete copy of the Fetch API. For example, responses are returned synchronously rather than asynchronously, as that is more familiar behavior for Ruby programmers. It is only "like" the Fetch API.
|
8
8
|
|
@@ -102,16 +102,16 @@ res = fetch('http://example.com', **{
|
|
102
102
|
})
|
103
103
|
```
|
104
104
|
|
105
|
-
|
105
|
+
### Connection pooling
|
106
106
|
|
107
|
-
Fetch
|
107
|
+
Fetch::API automatically pools and reuses connections to the same origin. Connections are closed after a certain amount of time has passed since the last use, and a new connection is used the next time. Different connections are returned for different threads (in other words, Fetch::API is thread-safe).
|
108
108
|
|
109
|
-
These values
|
109
|
+
These values can be configured as follows (in seconds):
|
110
110
|
|
111
111
|
``` ruby
|
112
112
|
Fetch.configure do |config|
|
113
|
-
config.
|
114
|
-
config.keep_alive_timeout
|
113
|
+
config.max_idle_time = 10 # default
|
114
|
+
config.keep_alive_timeout = 2 # default
|
115
115
|
end
|
116
116
|
```
|
117
117
|
|
data/lib/fetch/client.rb
CHANGED
@@ -48,7 +48,10 @@ module Fetch
|
|
48
48
|
when Net::HTTPRedirection
|
49
49
|
case redirect.to_s
|
50
50
|
when 'follow'
|
51
|
-
|
51
|
+
# @type var location: String
|
52
|
+
location = res['Location']
|
53
|
+
|
54
|
+
fetch(location, method:, headers:, body:, redirect:, _redirected: true) # steep:ignore ArgumentTypeMismatch
|
52
55
|
when 'error'
|
53
56
|
raise RedirectError, "redirected to #{res['Location']}"
|
54
57
|
when 'manual'
|
@@ -67,7 +70,7 @@ module Fetch
|
|
67
70
|
if pool = Thread.current.thread_variable_get(:fetch_connection_pool)
|
68
71
|
pool
|
69
72
|
else
|
70
|
-
Thread.current.thread_variable_set
|
73
|
+
Thread.current.thread_variable_set(:fetch_connection_pool, ConnectionPool.new)
|
71
74
|
end
|
72
75
|
end
|
73
76
|
|
data/lib/fetch/config.rb
CHANGED
@@ -13,7 +13,6 @@ module Fetch
|
|
13
13
|
@sweeper = Thread.new {
|
14
14
|
loop do
|
15
15
|
sleep 1
|
16
|
-
|
17
16
|
sweep
|
18
17
|
|
19
18
|
Thread.stop if @connections.empty?
|
@@ -42,13 +41,16 @@ module Fetch
|
|
42
41
|
|
43
42
|
entry.connection
|
44
43
|
else
|
45
|
-
|
44
|
+
# @type var host: String
|
45
|
+
host = uri.host
|
46
|
+
|
47
|
+
Net::HTTP.new(host, uri.port).tap {|http|
|
46
48
|
http.use_ssl = uri.scheme == 'https'
|
47
49
|
http.keep_alive_timeout = Fetch.config.keep_alive_timeout
|
48
50
|
|
49
|
-
@connections[uri.origin] = Entry.new(connection: http, in_use: true)
|
50
|
-
|
51
51
|
http.start
|
52
|
+
|
53
|
+
@connections[uri.origin] = Entry.new(connection: http, in_use: true)
|
52
54
|
}
|
53
55
|
end
|
54
56
|
}.tap {
|
@@ -70,7 +72,7 @@ module Fetch
|
|
70
72
|
@connections.each do |origin, entry|
|
71
73
|
next if entry.in_use
|
72
74
|
|
73
|
-
if entry.last_used + Fetch.config.
|
75
|
+
if entry.last_used + Fetch.config.max_idle_time < Time.now
|
74
76
|
entry.connection.finish
|
75
77
|
|
76
78
|
@connections.delete origin
|
data/lib/fetch/version.rb
CHANGED
data/lib/fetch.rb
CHANGED
data/sig/fetch/config.rbs
CHANGED