fetch-api 0.4.0 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dbef9662b23db6e0efca2b67264d6450553991b551ec94f1b7099c0b4c2eda55
4
- data.tar.gz: 0456d3ff62bcceeaa780ff9cb42c3ff231d667b977ef968e22bb5b2a5578e358
3
+ metadata.gz: 4217a0ddb4ec43da71a630ba5de24d9a6a02af0f5cf2b39286702c1cc512c699
4
+ data.tar.gz: 05abc4f266fab3577c2223de54957b8c6a84e7b3ccc24df428f40e771c1e311d
5
5
  SHA512:
6
- metadata.gz: 516ebe663dc6f346a8acc56f7a1c8e1a3bc11f3e133f6ca0c10a8544516a6dc8a8e34efd660585de627c595ba66ed17b6df3f3415ec90a439be84e21e50d7343
7
- data.tar.gz: 3b0630efe83154f9a71df06aec28b551720a6041e24ce1797626a105eedacc54a96495362e061b391018847cbfd2021e4fd750854b75a546841738f2a58d441c
6
+ metadata.gz: c7caa0c96f93f03ae799a121fc7f25410f401c9c192a7546f97627c760cb638627b79d01b0911ed8a4f2ebaacdaa619e86e84a2b88443a721f066a4a9fd4c1d9
7
+ data.tar.gz: 826e3c4ff75b7b3c3f0886949a6fe141cdd360fd31da86b42b08aabde98b0a608ac6f51561b65819b65622a1a2f344f5e6784505bafb990d0aad300ac8f378b6
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
- # Fetch API for Ruby
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. This is an HTTP client for Ruby that can be used in a way that is similar to the 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
- ## Connection pooling
105
+ ### Connection pooling
106
106
 
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).
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 (in seconds) can be configured as follows:
109
+ These values can be configured as follows (in seconds):
110
110
 
111
111
  ``` ruby
112
112
  Fetch.configure do |config|
113
- config.connection_max_idle_time = 10 # default
114
- config.keep_alive_timeout = 2 # default
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
@@ -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,13 +41,17 @@ module Fetch
45
41
  req.body = body
46
42
  end
47
43
 
48
- res = @pool.with_connection(uri) { _1.request(req) }
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
52
49
  case redirect.to_s
53
50
  when 'follow'
54
- fetch(res['Location'], method:, headers:, body:, redirect:, _redirected: true) # steep:ignore ArgumentTypeMismatch
51
+ # @type var location: String
52
+ location = res['Location']
53
+
54
+ fetch(location, method:, headers:, body:, redirect:, _redirected: true) # steep:ignore ArgumentTypeMismatch
55
55
  when 'error'
56
56
  raise RedirectError, "redirected to #{res['Location']}"
57
57
  when 'manual'
@@ -66,6 +66,14 @@ module Fetch
66
66
 
67
67
  private
68
68
 
69
+ def pool
70
+ if pool = Thread.current.thread_variable_get(:fetch_connection_pool)
71
+ pool
72
+ else
73
+ Thread.current.thread_variable_set(:fetch_connection_pool, ConnectionPool.new)
74
+ end
75
+ end
76
+
69
77
  def to_response(url, res, redirected)
70
78
  Response.new(
71
79
  url: url.to_str,
data/lib/fetch/config.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Fetch
2
- Config = Struct.new(:connection_max_idle_time, :keep_alive_timeout)
2
+ Config = Struct.new(:max_idle_time, :keep_alive_timeout)
3
3
  end
@@ -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?
@@ -35,20 +34,23 @@ module Fetch
35
34
 
36
35
  def acquire(uri)
37
36
  @mutex.synchronize {
38
- entry = @connections[key(uri)]
37
+ entry = @connections[uri.origin]
39
38
 
40
39
  if entry
41
40
  entry.in_use = true
42
41
 
43
42
  entry.connection
44
43
  else
45
- Net::HTTP.new(uri.host, uri.port).tap {|http| # steep:ignore ArgumentTypeMismatch
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[key(uri)] = 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 {
@@ -58,7 +60,7 @@ module Fetch
58
60
 
59
61
  def release(uri)
60
62
  @mutex.synchronize do
61
- if entry = @connections[key(uri)]
63
+ if entry = @connections[uri.origin]
62
64
  entry.in_use = false
63
65
  entry.last_used = Time.now
64
66
  end
@@ -67,20 +69,16 @@ module Fetch
67
69
 
68
70
  def sweep
69
71
  @mutex.synchronize do
70
- @connections.each do |key, entry|
72
+ @connections.each do |origin, entry|
71
73
  next if entry.in_use
72
74
 
73
- if entry.last_used + Fetch.config.connection_max_idle_time < Time.now
75
+ if entry.last_used + Fetch.config.max_idle_time < Time.now
74
76
  entry.connection.finish
75
77
 
76
- @connections.delete key
78
+ @connections.delete origin
77
79
  end
78
80
  end
79
81
  end
80
82
  end
81
-
82
- def key(uri)
83
- "#{Thread.current.object_id}/#{uri.origin}".freeze
84
- end
85
83
  end
86
84
  end
data/lib/fetch/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Fetch
2
- VERSION = '0.4.0'
2
+ VERSION = '0.4.2'
3
3
  end
data/lib/fetch.rb CHANGED
@@ -10,7 +10,7 @@ module Fetch
10
10
  end
11
11
 
12
12
  configure do |config|
13
- config.connection_max_idle_time = 10
14
- config.keep_alive_timeout = 2
13
+ config.max_idle_time = 10
14
+ config.keep_alive_timeout = 2
15
15
  end
16
16
  end
data/sig/fetch/client.rbs CHANGED
@@ -14,6 +14,7 @@ module Fetch
14
14
  private
15
15
 
16
16
  def initialize: () -> void
17
+ def pool: () -> ConnectionPool
17
18
  def to_response: (string, Net::HTTPResponse, bool) -> Response
18
19
  end
19
20
  end
data/sig/fetch/config.rbs CHANGED
@@ -1,6 +1,6 @@
1
1
  module Fetch
2
2
  class Config
3
- attr_accessor connection_max_idle_time: Integer
3
+ attr_accessor max_idle_time: Integer
4
4
  attr_accessor keep_alive_timeout: Integer
5
5
  end
6
6
  end
@@ -16,6 +16,5 @@ module Fetch
16
16
  def acquire: (URI::HTTP) -> Net::HTTP
17
17
  def release: (URI::HTTP) -> void
18
18
  def sweep: () -> void
19
- def key: (URI::HTTP) -> String
20
19
  end
21
20
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fetch-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Keita Urashima