Skip to content

Commit

Permalink
Implementing connection pooling for HTTP clients for Mediaflux::Http:…
Browse files Browse the repository at this point in the history
…:Request (#517)

* Implementing connection pooling for HTTP clients for Mediaflux::Http::Request

* Ensuring that the HTTP client is shutdown at the level of the Mediaflux::Http::Request Class destructor
  • Loading branch information
jrgriffiniii authored Feb 23, 2024
1 parent 1ff876d commit d0276b2
Showing 1 changed file with 30 additions and 12 deletions.
42 changes: 30 additions & 12 deletions app/models/mediaflux/http/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@
module Mediaflux
module Http
class Request
# This ensures that, once the Class is out of scope, the Net::HTTP::Persistent connection is closed
class << self
def self.create_finalizer
proc {
@http_client.shutdown unless @http_client.nil?
}
end

def initialize(**args)
super

finalizer = self.class.create_finalizer
ObjectSpace.define_finalizer(self, finalizer)
end
end

# As this is an abstract class, this should be overridden to specify the Mediaflux API service
def self.service
raise(NotImplementedError, "#{self} is an abstract class, please override #{self}.service")
Expand Down Expand Up @@ -60,17 +76,25 @@ def self.default_xml_namespace_uri
"http://tigerdata.princeton.edu"
end

# Constructs and memoizes a new instance of the Net::HTTP::Persistent object at the level of the Class
# @returns http_client [Net::HTTP::Persistent] HTTP client for transmitting requests to the Mediaflux server API
def self.find_or_create_http_client
@http_client ||= begin
@http_client = Net::HTTP::Persistent.new
# https is not working correctly on td-meta1 we should not need this, but we do...
@http_client.verify_mode = OpenSSL::SSL::VERIFY_NONE
@http_client
end
end

attr_reader :session_token

# Constructor
# @param file [File] any upload file required for the POST request
# @param session_token [String] the API token for the authenticated session
# @param http_client [Net::HTTP::Persistent] HTTP client for transmitting requests to the Mediaflux server API
def initialize(file: nil, session_token: nil, http_client: nil)
@http_client = http_client || Net::HTTP::Persistent.new
# https is not working correctly on td-meta1 we should not need this, but we do...
@http_client.verify_mode = OpenSSL::SSL::VERIFY_NONE

@http_client = http_client || self.class.find_or_create_http_client
@file = file
@session_token = session_token
end
Expand All @@ -79,8 +103,6 @@ def initialize(file: nil, session_token: nil, http_client: nil)
# @return [Net::HTTP]
def resolve
@http_response = @http_client.request self.class.uri, http_request
@http_client.shutdown
@http_response
end

# Determines whether or not the request has been resolved
Expand Down Expand Up @@ -127,12 +149,8 @@ def response_error
delegate :to_s, to: :response_xml

def xml_payload( name: self.class.service)
@payloads ||= {}
@payloads[name] ||= begin
body = build_http_request_body(name: )
body.to_xml
end
@payloads[name]
body = build_http_request_body(name: )
xml_payload = body.to_xml
end

private
Expand Down

0 comments on commit d0276b2

Please sign in to comment.