Skip to content

Commit

Permalink
⚡ improve traffic_state_of function by simplifying the if-else condit…
Browse files Browse the repository at this point in the history
…ion cost (#115)

…
traffic_state_of is a critical function that is called many times. any
improvement there result in noticeable performance bump.
  • Loading branch information
Ousret committed May 16, 2024
1 parent fbe653c commit 4b7c610
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
2.7.908 (2024-05-16)
====================

- Improve ``traffic_state_of`` function to improve the overall performance in a highly concurrent context.

2.7.907 (2024-05-05)
====================

Expand Down
2 changes: 1 addition & 1 deletion src/urllib3/_version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# This file is protected via CODEOWNERS
from __future__ import annotations

__version__ = "2.7.907"
__version__ = "2.7.908"
4 changes: 4 additions & 0 deletions src/urllib3/connectionpool.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ def close(self) -> None:
Close all pooled connections and disable the pool.
"""

@property
def is_idle(self) -> bool:
raise NotImplementedError


# This is taken from http://hg.python.org/cpython/file/7aaba721ebc0/Lib/socket.py#l252
_blocking_errnos = {errno.EAGAIN, errno.EWOULDBLOCK}
Expand Down
4 changes: 2 additions & 2 deletions src/urllib3/util/_async/traffic_police.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ def __init__(self, maxsize: int | None = None, concurrency: bool = False):

self.__ctx_cursor: contextvars.ContextVar[
tuple[int, T] | None
] = contextvars.ContextVar("cursor")
] = contextvars.ContextVar("cursor", default=None)
self.__ctx_wait_clock: contextvars.ContextVar[
float | None
] = contextvars.ContextVar("wait_clock")
] = contextvars.ContextVar("wait_clock", default=None)

@property
def _cursor(self) -> tuple[int, T] | None:
Expand Down
7 changes: 2 additions & 5 deletions src/urllib3/util/traffic_police.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,12 @@ class AtomicTraffic(TrafficPoliceFine, queue.Empty):

def traffic_state_of(manageable_traffic: ManageableTraffic) -> TrafficState:
if (
hasattr(manageable_traffic, "is_saturated")
getattr(manageable_traffic, "is_saturated", None)
and manageable_traffic.sock is not None # type: ignore[union-attr]
and manageable_traffic.is_saturated
):
return TrafficState.SATURATED
elif hasattr(manageable_traffic, "is_idle"):
return TrafficState.IDLE if manageable_traffic.is_idle else TrafficState.USED
else:
return TrafficState.IDLE
return TrafficState.IDLE if manageable_traffic.is_idle else TrafficState.USED


class TrafficPolice(typing.Generic[T]):
Expand Down

0 comments on commit 4b7c610

Please sign in to comment.