fetch-api 0.4.0 → 0.4.1

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: ff43ec8ec36d621acb463b7327146e89df97446714df7747626276bb51ff4c18
4
+ data.tar.gz: de99e42e871912c41df6a5afabb1200f662d5aeba7b6659a4483434c8ddca3c7
5
5
  SHA512:
6
- metadata.gz: 516ebe663dc6f346a8acc56f7a1c8e1a3bc11f3e133f6ca0c10a8544516a6dc8a8e34efd660585de627c595ba66ed17b6df3f3415ec90a439be84e21e50d7343
7
- data.tar.gz: 3b0630efe83154f9a71df06aec28b551720a6041e24ce1797626a105eedacc54a96495362e061b391018847cbfd2021e4fd750854b75a546841738f2a58d441c
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
- 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
@@ -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[key(uri)]
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[key(uri)] = Entry.new(connection: http, in_use: true)
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[key(uri)]
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 |key, entry|
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 key
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
@@ -1,3 +1,3 @@
1
1
  module Fetch
2
- VERSION = '0.4.0'
2
+ VERSION = '0.4.1'
3
3
  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
@@ -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.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Keita Urashima