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

Adding timeout to socket.py and httplib.py #44685

Closed
facundobatista opened this issue Mar 8, 2007 · 11 comments
Closed

Adding timeout to socket.py and httplib.py #44685

facundobatista opened this issue Mar 8, 2007 · 11 comments
Assignees
Labels
stdlib Python modules in the Lib dir

Comments

@facundobatista
Copy link
Member

BPO 1676823
Nosy @gvanrossum, @birkenfeld, @facundobatista
Files
  • timeout.diff: Patch to code, docs, and test cases
  • timeout.diff: reviewed version
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/birkenfeld'
    closed_at = <Date 2007-03-23.19:33:21.000>
    created_at = <Date 2007-03-08.21:32:11.000>
    labels = ['library']
    title = 'Adding timeout to socket.py and httplib.py'
    updated_at = <Date 2007-03-23.19:33:21.000>
    user = 'https://github.com/facundobatista'

    bugs.python.org fields:

    activity = <Date 2007-03-23.19:33:21.000>
    actor = 'facundobatista'
    assignee = 'georg.brandl'
    closed = True
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2007-03-08.21:32:11.000>
    creator = 'facundobatista'
    dependencies = []
    files = ['7846', '7847']
    hgrepos = []
    issue_num = 1676823
    keywords = ['patch']
    message_count = 11.0
    messages = ['52125', '52126', '52127', '52128', '52129', '52130', '52131', '52132', '52133', '52134', '52135']
    nosy_count = 3.0
    nosy_names = ['gvanrossum', 'georg.brandl', 'facundobatista']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = None
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue1676823'
    versions = ['Python 2.6']

    @facundobatista
    Copy link
    Member Author

    This is a subset of patch bpo-723312. Like it, adds a NetworkConnection object to socket.py, and then use it from other modules.

    This NetworkConnection basically does what all the other modules do once and again, so no mistery about it (basically calls getaddrinfo over the received address, and try to open a socket to that address).

    In this patch I only use this new NetworkConnection from httplib, whose HTTPConnection class now optionally accepts a timeout.

    Beyond the changes in socket.py and httplib.py, this patch also includes changes in the docs and new test cases.

    @facundobatista facundobatista added the stdlib Python modules in the Lib dir label Mar 8, 2007
    @facundobatista facundobatista added the stdlib Python modules in the Lib dir label Mar 8, 2007
    @gvanrossum
    Copy link
    Member

    I like the idea of refactoring refactoring of the connect() loop, however, I wonder if a class isn't overkill. The constructor could just store the timeout parameter instead of the networkconnection object, and the connect() method could just call the refactored connect_with_timeout function passing it the host/port pair and the timeout. But perhaps there's a use case I have missed that appears in one of the classes you haven't patched yet but looked at?

    @facundobatista
    Copy link
    Member Author

    In all the other libraries we can store the timeout, and then make a single call to socket.py.

    There's no "refactored connect_with_timeout" in socket.py. The functionality that will be present is one that is repeated several times in the higher level modules. So, there's no need of a "..._with_timeout" name.

    I'll work in a function, in socket.py, called "connect", with this functionality, and the following signature:

      def connect(address, timeout=None):   # being address == (host, port)
         ...

    @facundobatista
    Copy link
    Member Author

    I refactored the patch, now it uses function connect() in socket.py as specified in my last comment here.

    Of course, tests and docs are also updated in the patch.
    File Added: timeout.diff

    @gvanrossum
    Copy link
    Member

    Georg, can you review this?

    @facundobatista
    Copy link
    Member Author

    I updated the patch, reflecting discussion on python-dev:

    • The function name changed, now it's _create_connection(). Its signature also changed: now, timeout is mandatorily named.

    • HTTPConnection has the posibility to call timeout with a number, and also with None. In both cases, it update sock.timeout (changing effectively the default timeout).

    Docs and tests cases are also updated (note: now there's no doc in libsocket.tex, as this function is now private).

    File Added: timeout.diff

    @gvanrossum
    Copy link
    Member

    I may have missed some of the discussion, but I don't understand why it is so important to differentiate between explicit timeout=None and omitting timeout altogether. I would favor a version where the timeout defaults to None, is a simple positional argument to _create_connection(), and the settimeout() call is skipped when timeout is None.

    I also favor making create_connection() public again since it may be useful for user code or 3rd party libraries that implements some protocol. After all the reality is probably that people have copied the exact loop that's found in httplib and 5 other places into their code anyway, if they care at all about IPv6 support.

    @facundobatista
    Copy link
    Member Author

    The patch is updated, reflecting discussion in python-dev and last comment here.

    • Just put a timeout default to None. If None, skip settimeout() call.

    • Copy the exceptions behaviour that we have actually in the higher
      level libraries, to be sure we aren't breaking anything.

    • The function is now public, named "create_connection".

    Tests and docs are updated too.
    File Added: timeout.diff

    @gvanrossum
    Copy link
    Member

    Can you check this in yourself?

    @birkenfeld
    Copy link
    Member

    I'm attaching a reviewed version. Basically only nits and one added XXX comment about IPv6.
    File Added: timeout.diff

    @facundobatista
    Copy link
    Member Author

    I already commited the previous version, :s.

    I'm closing this patch, feel free to reopen it if you want me to add the XXX comment.

    Regards,

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants