8000 Tweak implementation of throwaway HTTP sessions by florimondmanca · Pull Request #7579 · DataDog/integrations-core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Tweak implementation of throwaway HTTP sessions #7579

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

Closed
wants to merge 1 commit into from

Conversation

florimondmanca
Copy link
Contributor
@florimondmanca florimondmanca commented Sep 15, 2020

What does this PR do?

Our RequestsWrapper has a persist_connection switch. When it's off (the default), we use a throw-away session for each request, instead of reusing a shared Session.

This PR changes this throw-away mechanism from using the top-level requests namespace to using the same session than we'd use when persisting connections. This is done via a ._create_session() method shared between each code path (persist / don't persist).

Motivation

I'm working on adding Unix Domain Socket support to our RequestsWrapper (marked as TODO in the docs, plus I'm going to need it for IoT Edge #7465).

Using requests-unixsocket this requires mounting an adapter onto a session, eg session.mount("unix://", UnixAdapter()), and so in the "don't persist" code path we'll need to create a dedicated session rather than the top-level requests library.

With this new shared ._create_session() logic, adding UDS support will basically amount to editing _create_session() so that it mounts the adapter. This will apply to both "persist" and "don't persist" use cases.

In any case, using the exact same logic in each code path seems like the best way forward!

Additional Notes

Review checklist (to be filled by reviewers)

  • Feature or bugfix MUST have appropriate tests (unit, integration, e2e)
  • PR title must be written as a CHANGELOG entry (see why)
  • Files changes must correspond to the primary purpose of the PR as described in the title (small unrelated changes should have their own PR)
  • PR must have changelog/ and integration/ labels attached

@@ -124,7 +124,7 @@ def test_extra_headers_on_http_method_call(self):
http = RequestsWrapper(instance, init_config)

extra_headers = {"foo": "bar"}
with mock.patch("requests.get") as get:
with mock.patch("requests.Session.get") as get:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changing all these mock calls took a fair amount of tedious work, but I'm not sure if we should do anything about this.

I thought about parametrizing test cases by the HTTP method, since we do basically the same thing for GET, POST, PUT, PATCH, DELETE. But the devil is in the details and maybe we don't exactly do the same thing in each case, though.

Anyway, we can treat any refactor of these big amounts of mocks as a follow-up…

Copy link
Member

Choose a reason for hiding this comment

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

Agreed with the idea of pushing that change to a separate PR.

Comment on lines +348 to +349
for option, value in iteritems(self.options):
setattr(session, option, value)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note: these two lines were not called in the default "don't persist" case previously, so walking through if there's any behavior change here…

  • Previously, request-level options were built via the call to .populate_options(), which takes the kwargs passed via http.<method>(), updates it with RequestsWrapper options, and returns the result.
  • Now, we do the same thing, except that we initially copy RequestsWrapper options onto the Session instance (via the two lines highlighted here).

The copying onto the Session instance and the behavior of .populate_options() is a bit redundant, since we're basically applying defaults twice (once ourselves via .populate_options(), and once via Session defaults, applied by Requests). But it's not a broken thing to do — and in fact, we were already doing this in the persist_connections: true case.

So there doesn't seem to be any behavioral change. Can anyone confirm I'm getting this right?

Copy link
Contributor

Choose a reason for hiding this comment

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

Confirmed

@florimondmanca
Copy link
Contributor Author

Closing, we'll come back to this later.

@florimondmanca florimondmanca deleted the fm/requests-wrapper-throwaway branch September 15, 2020 16:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0