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

Simplify Net::HTTP connection handing #992

Merged
merged 4 commits into from
Aug 17, 2022
Merged

Conversation

rzane
Copy link
Contributor

@rzane rzane commented Aug 8, 2022

This PR aims to simplify the way that the stub socket is assigned and swapped in Net::HTTP.

@rzane rzane force-pushed the request-connect branch 2 times, most recently from bf8ee64 to 91253df Compare August 8, 2022 02:18
@@ -72,78 +72,57 @@ def constants(inherit=true)
end

def request(request, body = nil, &block)
return super unless started?
Copy link
Contributor Author

@rzane rzane Aug 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This lets us avoid having to deal with the case where the connection is not started yet. It makes the implementation much simpler.

see https://github.com/ruby/net-http/blob/196f3d70f9ce154dc63a86a2cdbf1aec4df4982e/lib/net/http.rb#L1568-L1574

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clever! 👍

request_signature = WebMock::NetHTTPUtility.request_signature_from_request(self, request, body)

WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)

if webmock_response = WebMock::StubRegistry.instance.response_for_request(request_signature)
@socket = Net::HTTP.socket_type.new
Copy link
Contributor Author

@rzane rzane Aug 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no reason to replace the socket here. At the very least, we'd want to close the old socket before initializing a new one. This would have caused a file descriptor leak in the scenario where the first request was allowed, but the second was stubbed.

alias_method :start_with_connect, :start

def start(&block)
def connect
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Net::HTTP#start calls #connect. This is a much simpler place for us to hook in, because unlike #start, #connect doesn't have a block variant.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

At the very least, we'd want to close the current socket before
assigning a new one. But, in this case, I think it's better to preserve
the existing socket
The beginning of `#request` looks like this in net/http:

    def request(req, body = nil, &block)
      unless started?
        start {
          req['connection'] ||= 'close'
          return request(req, body, &block)
        }
      end
@@ -215,14 +215,14 @@ class TestMarshalingInWebMockNetHTTP

if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.7.0')
it "uses the StubSocket to provide IP address" do
Net::HTTP.start("http://example.com") do |http|
Net::HTTP.start("example.com") do |http|
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is me fixing a mistake I made in #988 😄

@bblimke
Copy link
Owner

bblimke commented Aug 17, 2022

Thank you @rzane . Much simpler.

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

Successfully merging this pull request may close these issues.

2 participants