Created
November 25, 2020 12:49
-
-
Save TheRusskiy/555173681763f17174248ff2e107290f to your computer and use it in GitHub Desktop.
PersistentHttpClient
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PersistentHttpClient | |
def self.get(url) | |
uri = url.is_a?(URI) ? url : URI(url) | |
connection_manager.get_connection(uri) | |
end | |
def self.connection_manager | |
Thread.current[:persistent_http_connection_manager] ||= new_manager | |
end | |
def self.connection_managers | |
@connection_managers ||= [] | |
end | |
def self.new_manager | |
remove_old_managers | |
mutex.synchronize do | |
manager = ConnectionManager.new | |
connection_managers << manager | |
manager | |
end | |
end | |
def self.remove_old_managers | |
mutex.synchronize do | |
removed = connection_managers.reject!(&:stale?) | |
(removed || []).each(&:close_connections!) | |
end | |
end | |
def self.mutex | |
@mutex ||= Mutex.new | |
end | |
class ConnectionManager | |
attr_accessor :connections, :last_used | |
def initialize | |
self.connections = {} | |
self.last_used = Time.now | |
end | |
def get_connection(uri) | |
mutex.synchronize do | |
self.last_used = Time.now | |
params = [uri.host, uri.port] | |
connection = connections[params] | |
if connection | |
return connection | |
end | |
# https://github.com/stripe/stripe-ruby/blob/f3b83f132ec83646fd1fe74a648954db3f681b12/lib/stripe/connection_manager.rb#L103 | |
connection = Net::HTTP.new(uri.host, uri.port) | |
connection.keep_alive_timeout = 30 | |
connection.use_ssl = uri.scheme == "https" | |
connection.open_timeout = 30 | |
connection.read_timeout = 80 | |
connection.start | |
connections[params] = connection | |
connection | |
end | |
end | |
def close_connections! | |
mutex.synchronize do | |
connections.values.each(&:finish) | |
self.connections = {} | |
end | |
end | |
def stale? | |
Time.now - last_used > 5.minutes | |
end | |
def mutex | |
@mutex ||= Mutex.new | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
uri = URI("https://example.com/api") | |
http = PersistentHttpClient.get(uri) | |
request = Net::HTTP::Post.new(uri.request_uri) | |
response = http.request(request) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment