Class: Async::HTTP::Internet

Inherits:
Object
  • Object
show all
Defined in:
lib/async/http/internet.rb,
lib/async/http/internet/instance.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Internet

Returns a new instance of Internet.



17
18
19
20
# File 'lib/async/http/internet.rb', line 17

def initialize(**options)
	@clients = Hash.new
	@options = options
end

Instance Attribute Details

#clientsObject

A cache of clients.



24
25
26
# File 'lib/async/http/internet.rb', line 24

def clients
  @clients
end

Class Method Details

.instanceObject

The global instance of the internet.



14
15
16
# File 'lib/async/http/internet/instance.rb', line 14

def self.instance
	::Thread.current.async_http_internet_instance ||= self.new
end

Instance Method Details

#call(verb, url, *arguments, **options, &block) ⇒ Object

Make a request to the internet with the given ‘method` and `url`.

If you provide non-frozen headers, they may be mutated.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/async/http/internet.rb', line 42

def call(verb, url, *arguments, **options, &block)
	endpoint = Endpoint[url]
	client = self.client_for(endpoint)
	
	options[:authority] ||= endpoint.authority
	options[:scheme] ||= endpoint.scheme
	
	request = ::Protocol::HTTP::Request[verb, endpoint.path, *arguments, **options]
	
	response = client.call(request)
	
	return response unless block_given?
	
	begin
		yield response
	ensure
		response.close
	end
end

#client_for(endpoint) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/async/http/internet.rb', line 26

def client_for(endpoint)
	key = host_key(endpoint)
	
	@clients.fetch(key) do
		@clients[key] = self.make_client(endpoint)
	end
end

#closeObject



62
63
64
65
66
67
68
# File 'lib/async/http/internet.rb', line 62

def close
	# The order of operations here is to avoid a race condition between iterating over clients (#close may yield) and creating new clients.
	clients = @clients.values
	@clients.clear
	
	clients.each(&:close)
end