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

Binding to network interfaces #7132

Open
wants to merge 51 commits into
base: master
Choose a base branch
from
Open

Conversation

Sky-Chrome
Copy link

What do these changes do?

Finally added support to bind directly to network interfaces.

Are there changes in behavior for the user?

No, a optional keyword argument has been added. This will allow end-users to specify the network interface, a feature many related projects lacks without monkey-patching.

Related issue number

#6383

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes
  • If you provide code modification, please add yourself to CONTRIBUTORS.txt
    • The format is <Name> <Surname>.
    • Please keep alphabetical order, the file is sorted by names.
  • Add a new news fragment into the CHANGES folder
    • name it <issue_id>.<type> for example (588.bugfix)
    • if you don't have an issue_id change it to the pr id after creating the pr
    • ensure type is one of the following:
      • .feature: Signifying a new feature.
      • .bugfix: Signifying a bug fix.
      • .doc: Signifying a documentation improvement.
      • .removal: Signifying a deprecation or removal of public API.
      • .misc: A ticket has been closed, but it is not of interest to users.
    • Make sure to use full sentences with correct case and punctuation, for example: "Fix issue with non-ascii contents in doctest text files."

Finally added support to bind directly to network interfaces.
@psf-chronographer psf-chronographer bot added the bot:chronographer:provided There is a change note present in this PR label Dec 17, 2022
@codecov
Copy link

codecov bot commented Dec 17, 2022

Codecov Report

Merging #7132 (2e50c16) into master (d51013d) will decrease coverage by 0.43%.
The diff coverage is 27.77%.

❗ Current head 2e50c16 differs from pull request most recent head 5fb6f4b. Consider uploading reports for the commit 5fb6f4b to get more accurate results

@@            Coverage Diff             @@
##           master    #7132      +/-   ##
==========================================
- Coverage   97.38%   96.95%   -0.44%     
==========================================
  Files         106      106              
  Lines       31093    31110      +17     
  Branches     3875     3878       +3     
==========================================
- Hits        30280    30162     -118     
- Misses        612      735     +123     
- Partials      201      213      +12     
Flag Coverage Δ
CI-GHA 96.87% <27.77%> (-0.41%) ⬇️
OS-Linux 96.84% <27.77%> (-0.10%) ⬇️
OS-Windows ?
OS-macOS 96.46% <27.77%> (-0.05%) ⬇️
Py-3.10.8 ?
Py-3.10.9 96.55% <27.77%> (-0.48%) ⬇️
Py-3.11.0 95.55% <27.77%> (-0.05%) ⬇️
Py-3.7.15 96.71% <27.77%> (-0.05%) ⬇️
Py-3.7.9 ?
Py-3.8.10 ?
Py-3.8.15 96.61% <27.77%> (-0.04%) ⬇️
Py-3.9.13 ?
Py-3.9.14 ?
Py-3.9.15 ?
Py-3.9.16 96.62% <27.77%> (-0.04%) ⬇️
Py-pypy7.3.10 ?
Py-pypy7.3.9 ?
VM-macos 96.46% <27.77%> (-0.05%) ⬇️
VM-ubuntu 96.84% <27.77%> (-0.10%) ⬇️
VM-windows ?

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
aiohttp/connector.py 91.88% <22.22%> (-2.74%) ⬇️
aiohttp/helpers.py 94.37% <33.33%> (-1.02%) ⬇️
tests/autobahn/client/client.py 46.42% <0.00%> (-50.00%) ⬇️
tests/conftest.py 86.84% <0.00%> (-9.65%) ⬇️
aiohttp/__init__.py 85.71% <0.00%> (-7.15%) ⬇️
aiohttp/web_runner.py 92.37% <0.00%> (-5.39%) ⬇️
aiohttp/tcp_helpers.py 94.73% <0.00%> (-5.27%) ⬇️
tests/test_web_runner.py 93.78% <0.00%> (-5.09%) ⬇️
tests/autobahn/test_autobahn.py 95.00% <0.00%> (-5.00%) ⬇️
aiohttp/pytest_plugin.py 94.33% <0.00%> (-3.15%) ⬇️
... and 9 more

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

aiohttp/connector.py Outdated Show resolved Hide resolved
aiohttp/connector.py Outdated Show resolved Hide resolved
@Sky-Chrome Sky-Chrome requested review from Dreamsorcerer and removed request for asvetlov and webknjaz December 18, 2022 12:58
docs/faq.rst Outdated Show resolved Hide resolved
aiohttp/helpers.py Outdated Show resolved Hide resolved
docs/client_advanced.rst Outdated Show resolved Hide resolved
Sky-Chrome and others added 2 commits December 20, 2022 04:21
Co-authored-by: Sviatoslav Sydorenko <wk.cvs.github@sydorenko.org.ua>
fallback by catching the :exc:`RuntimeError` on creation::

try:
conn = aiohttp.TCPConnector(network_interface="eth0")
Copy link
Member

Choose a reason for hiding this comment

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

Should we use the local_addr argument here too so it's closer to the fallback?

docs/faq.rst Outdated Show resolved Hide resolved

If your system has several IP interfaces, you may choose one which will
be used used to bind a socket locally::
Copy link
Member

Choose a reason for hiding this comment

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

Apparently, both the original and the updated text have the double used used bit.

docs/faq.rst Outdated Show resolved Hide resolved
docs/client_advanced.rst Outdated Show resolved Hide resolved
docs/client_advanced.rst Outdated Show resolved Hide resolved
docs/client_advanced.rst Outdated Show resolved Hide resolved
docs/client_advanced.rst Outdated Show resolved Hide resolved
aiohttp/helpers.py Outdated Show resolved Hide resolved
aiohttp/connector.py Outdated Show resolved Hide resolved
Sky-Chrome and others added 2 commits December 21, 2022 00:55
Co-authored-by: Sviatoslav Sydorenko <wk.cvs.github@sydorenko.org.ua>
@webknjaz webknjaz added the backport-3.9 Trigger automatic backporting to the 3.9 release branch by Patchback robot label Dec 24, 2022
@Sky-Chrome
Copy link
Author

Changed it to static

@@ -954,7 +970,9 @@ def _get_fingerprint(self, req: "ClientRequest") -> Optional["Fingerprint"]:

async def _wrap_create_connection(
self,
*args: Any,
factory: functools.partial[ResponseHandler],
Copy link
Member

Choose a reason for hiding this comment

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

The actual requirement is this: https://github.com/python/typeshed/blob/main/stdlib/asyncio/base_events.pyi#L117

So, I'd suggest doing the same thing with Callable[[], _ResponseHandlerT]. Then we can also update the return type so this becomes generic.

@Dreamsorcerer
Copy link
Member

Dreamsorcerer commented Jan 10, 2023

To fix the typing error, I'd suggest renaming the first host variable to raw_host:

        raw_host = req.url.raw_host
        assert raw_host is not None
        port = req.port
        assert port is not None
        host_resolved = asyncio.ensure_future(
            self._resolve_host(raw_host, port, traces=traces), loop=self._loop
        )

aiohttp/aiohttp/connector.py

Lines 1112 to 1118 in f0073af

host = req.url.raw_host
assert host is not None
port = req.port
assert port is not None
host_resolved = asyncio.ensure_future(
self._resolve_host(host, port, traces=traces), loop=self._loop
)

(AF_INET, AF_INET6, AF_NETLINK, AF_TIPC)
or strings (AF_UNIX).
"""
sock = socket.socket(family, socket.SOCK_STREAM)
Copy link
Member

Choose a reason for hiding this comment

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

It probably makes sense to add

Suggested change
sock = socket.socket(family, socket.SOCK_STREAM)
assert SOCKET_SUPPORTS_BINDING_TO_DEVICE
sock = socket.socket(family, socket.SOCK_STREAM)

@bertybuttface
Copy link

Sorry to bump an old PR but is this stale / is there another way to select a particular network interface when using aiohttp.ClientSession?

@Dreamsorcerer
Copy link
Member

Looks like the PR didn't get finished off. Feel free to fork it and create a new PR with the final changes.

@Sky-Chrome
Copy link
Author

Hey @Dreamsorcerer Exactly, what is needed to finish this PR?

@Dreamsorcerer
Copy link
Member

Dreamsorcerer commented Jul 15, 2023

The tests are failing, there are conflicts, and there are unresolved conversations above.

@webknjaz webknjaz added the backport-3.10 Trigger automatic backporting to the 3.10 release branch by Patchback robot label Jan 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-3.9 Trigger automatic backporting to the 3.9 release branch by Patchback robot backport-3.10 Trigger automatic backporting to the 3.10 release branch by Patchback robot bot:chronographer:provided There is a change note present in this PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants