Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persisting the ConnectionPool? #148

Open
AnilRh opened this issue Feb 15, 2024 · 0 comments
Open

Persisting the ConnectionPool? #148

AnilRh opened this issue Feb 15, 2024 · 0 comments

Comments

@AnilRh
Copy link

AnilRh commented Feb 15, 2024

Perhaps I'm missing something obvious, but when I try to use this gem to optimize multiple calls to the same endpoint, but from different code blocks, it doesn't work.

Version 2.9.4 of the gem worked fine. The ConnectionPool in V2 appears to have been stored as class variable. When upgrading to version 3.0.1 I only see optimizations if I keep the instance of Net::HTTP::Persistent.new around and use it to make all of my calls.

Am I doing something stupid? (btw, the reason I can't keep the instance around is that I'm actually making a call through Savon, which uses HTTI, and it creates the the Net::HTTP::Persistent instance.

Example Code:

The setup (not interesting, just gets us ready):

require 'httpi'
require 'benchmark'

def bench(&block)
  time = Benchmark.measure do
    5.times do |i|
      t = Time.now
      yield
      puts "Request took #{Time.now - t} seconds"
    end
  end
  puts "Total time #{time.real}"
end

# here are a couple of sites to try with. They are geographically far from me, so good for testing.
url = "https://eryang.world.tmall.com"
url = "https://kenh14.vn"

Testing with HTTPI - every request is just as slow as the last:

puts "**** Testing with HTTPI\n\n"
HTTPI.adapter = :net_http_persistent
request = HTTPI::Request.new(url)
bench do
  HTTPI.get(request)
end

Testing using the same HTTP:: Persistent instance - we see a speed improvement here:

puts "**** Testing with Net::HTTP::Persistent with existing instance"
http = Net::HTTP::Persistent.new name: 'my_app_name'
bench do
    response = http.request url
end

Testing as if each block of code creates its own HTTP:: Persistent instance. This is slow.

puts "**** Testing with Net::HTTP::Persistent with new instance"
bench do
    http = Net::HTTP::Persistent.new name: 'my_app_name'
    response = http.request url
end

If you try the first example above using version 2.94 of the gem it is fast.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant