Skip to content

Fine-tuning timeouts from the default timeout config #435

Closed

Description

@florimondmanca

Currently, if we want to change only one of the three timeouts (connect, read, write) we need to build a full-blown TimeoutConfig. But there is no way to use the default timeout values.

>>> import httpx
>>> httpx.config.DEFAULT_TIMEOUT_CONFIG
TimeoutConfig(timeout=5.0)
>>> httpx.TimeoutConfig(read_timeout=1)
# We'd expect connect_timeout and write_timeout to be 5 (the default)
TimeoutConfig(connect_timeout=None, read_timeout=1, write_timeout=None)

Changing TimeoutConfig to automatically use the default values in case some values aren't given is probably not the good way to go, because TimeoutConfig shouldn't contain any smart behavior.

Instead, we should probably allow explicitly building a TimeoutConfig instance from DEFAULT_TIMEOUT_CONFIG:

I'd see it materialize as factory method on TimeoutConfig:

>>> httpx.TimeoutConfig.from_defaults(read_timeout=1)
TimeoutConfig(connect_timeout=5, read_timeout=1, write_timeout=5)

Does this sound reasonable @tomchristie?

Activity

  1. sethmlarson commented on Oct 3, 2019

    @sethmlarson
    Contributor

    What about making TimeoutConfig(read_timeout=1) result in 5, 1, 5 instead of None, 1, None? Then you have to be explicit about the other two if you truly want "None".

  2. lovelydinosaur commented on Oct 4, 2019

    @lovelydinosaur
    Member

    Simplest question is, what do we want this to return?...

    >>> httpx.TimeoutConfig()

    I think we should probably switch things around so that it'll give you TimeoutConfig(timeout=5) rather than TimeoutConfig(timeout=None), right?

    I guess we want something like this...

    DEFAULT_TIMEOUT = 5
    UNSET = object()
    
    class TimeoutConfig:
        def __init__(
            self,
            timeout: TimeoutTypes = DEFAULT_TIMEOUT,
            *,
            connect_timeout: float = UNSET,
            read_timeout: float = UNSET,
            write_timeout: float = UNSET,
        ):
            self.connect_timeout = timeout if connect_timeout is UNSET else connect_timeout
            self.read_timeout = timeout if read_timeout is UNSET else read_timeout
            self.write_timeout = timeout if connect_timeout is UNSET else write_timeout
            ...
  3. florimondmanca commented on Oct 4, 2019

    @florimondmanca
    ContributorAuthor

    And so we'd have:

    >>> # Override one of the timeouts.
    >>> httpx.TimeoutConfig(read_timeout=1)
    TimeoutConfig(connect_timeout=5, read_timeout=1, write_timeout=5)
    >>> # Disable timeouts.
    >>> httpx.TimeoutConfig(timeout=None)
    TimeoutConfig(connect_timeout=None, read_timeout=None, write_timeout=None)
    >>> # Set all timeouts to 3 seconds but disable read.
    >>> httpx.TimeoutConfig(timeout=3, read_timeout=None)
    TimeoutConfig(connect_timeout=3, read_timeout=None, write_timeout=3)

    This looks like a nice approach to me. I suppose we'd get rid of DEFAULT_TIMEOUT_CONFIG entirely and use TimeoutConfig() instead?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions