Skip to content

Commit

Permalink
style(pre-commit): auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
pre-commit-ci[bot] committed Jul 31, 2023
1 parent 6a1097c commit 32f5cbd
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
5 changes: 2 additions & 3 deletions discord/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@

import aiohttp

from .rate_limiting import BucketStorage

from . import utils
from .activity import ActivityTypes, BaseActivity, create_activity
from .appinfo import AppInfo, PartialAppInfo
Expand All @@ -54,6 +52,7 @@
from .iterators import GuildIterator
from .mentions import AllowedMentions
from .object import Object
from .rate_limiting import BucketStorage
from .stage_instance import StageInstance
from .state import ConnectionState
from .sticker import GuildSticker, StandardSticker, StickerPack, _sticker_factory
Expand Down Expand Up @@ -252,7 +251,7 @@ def __init__(
loop=self.loop,
bucket_storage_cls=options.pop("bucket_storage_cls", BucketStorage),
global_concurrency=options.pop("concurrency", 50),
per_concurrency=options.pop("per", 1)
per_concurrency=options.pop("per", 1),
)

self._handlers: dict[str, Callable] = {"ready": self._handle_ready}
Expand Down
12 changes: 8 additions & 4 deletions discord/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def __init__(
unsync_clock: bool = True,
global_concurrency: int = 10,
per_concurrency: int = 60,
bucket_storage_cls: Type[BucketStorage] = BucketStorage
bucket_storage_cls: type[BucketStorage] = BucketStorage,
) -> None:
self.loop: asyncio.AbstractEventLoop = (
asyncio.get_event_loop() if loop is None else loop
Expand Down Expand Up @@ -312,12 +312,16 @@ async def request(
if is_global:
self.global_dynamo = DynamicBucket()
await self.global_dynamo.executed(
retry_after, remaining or 10, is_global=is_global
retry_after,
remaining or 10,
is_global=is_global,
)
self.global_dynamo = None
else:
tbucket = DynamicBucket()
await self._rate_limit.push_temp_bucket(bucket_id, tbucket)
await self._rate_limit.push_temp_bucket(
bucket_id, tbucket
)
await tbucket.executed(
retry_after, remaining or 10, is_global=True
)
Expand Down Expand Up @@ -347,7 +351,7 @@ async def request(
# This is handling exceptions from the request
except OSError as e:
# Connection reset by peer
if tries < 4 and e.errno in (54, 10054):
if tries < 4 and e.errno in (54, 10054):
await asyncio.sleep(1 + tries * 2)
continue
raise
Expand Down
17 changes: 8 additions & 9 deletions discord/rate_limiting.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
"""

import asyncio
from asyncio.events import AbstractEventLoop
import gc
import time
from asyncio.events import AbstractEventLoop
from contextlib import asynccontextmanager
from typing import AsyncIterator, Literal, cast

Expand Down Expand Up @@ -107,7 +107,9 @@ def reset(self) -> None:
class PriorityFuture(asyncio.Future):
"""A future with priority features added to it."""

def __init__(self, *, priority: int = 0, loop: AbstractEventLoop | None = None) -> None:
def __init__(
self, *, priority: int = 0, loop: AbstractEventLoop | None = None
) -> None:
super().__init__(loop=loop)
self.priority = priority

Expand All @@ -129,7 +131,9 @@ class Bucket:
"""

def __init__(self) -> None:
self._pending: asyncio.PriorityQueue[PriorityFuture[None]] = asyncio.PriorityQueue()
self._pending: asyncio.PriorityQueue[
PriorityFuture[None]
] = asyncio.PriorityQueue()
self._reserved: list[PriorityFuture] = []
self._reset_after_set: bool = False
self.metadata_unknown: bool = False
Expand All @@ -139,7 +143,6 @@ def __init__(self) -> None:
self._fetch_metadata = asyncio.Event()
self._fetch_metadata.set()


@asynccontextmanager
async def reserve(self, priority: int = 0) -> AsyncIterator[None]:
if self.metadata_unknown:
Expand Down Expand Up @@ -205,7 +208,6 @@ async def reserve(self, priority: int = 0) -> AsyncIterator[None]:
async with self.reserve(priority=priority):
yield


def release(self, count: int | None = None) -> None:
"""Release *count* amount of requests.
Expand All @@ -229,7 +231,6 @@ def release(self, count: int | None = None) -> None:

fut.set_result(None)


@property
def garbage(self) -> bool:
"""Whether this bucket should be collected by the garbage collector."""
Expand All @@ -248,14 +249,12 @@ def garbage(self) -> bool:

return False


async def stop(self) -> None:
"""Cancel all reserved futures from use."""

for fut in self._reserved:
fut.set_exception(asyncio.CancelledError)


def set_metadata(
self,
remaining: int | None = None,
Expand Down Expand Up @@ -289,7 +288,6 @@ def set_metadata(
loop = asyncio.get_running_loop()
loop.call_later(cast(float, reset_after), self._reset)


def _reset(self) -> None:
self._reset_after_set = False
self.remaining = None
Expand Down Expand Up @@ -417,6 +415,7 @@ async def pop_temp_bucket(self, id: str) -> None:

self._temp_buckets.pop(id)


class DynamicBucket:
"""A dynamic bucket for on-the-fly rate limits. Should not be used inside a bot directly!"""

Expand Down
2 changes: 0 additions & 2 deletions discord/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
NewType,
Protocol,
Sequence,
Type,
TypeVar,
Union,
overload,
Expand Down Expand Up @@ -1393,4 +1392,3 @@ def filter_params(params, **kwargs):
params[new_param] = params.pop(old_param)

return params

0 comments on commit 32f5cbd

Please sign in to comment.