Skip to content

Commit

Permalink
Merge pull request #312 from davfsa/task/max-rate-limit
Browse files Browse the repository at this point in the history
Fix max_rate_limit
  • Loading branch information
davfsa authored Oct 13, 2020
2 parents 6c29792 + a9b38f5 commit e77eb0e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
13 changes: 13 additions & 0 deletions hikari/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,16 @@ class RateLimitTooLongError(HTTPError):
period: float = attr.ib()
"""How long the rate limit window lasts for from start to end."""

message: str = attr.ib(init=False)
"""The error message."""

@message.default
def _(self) -> str:
return (
"The request has been rejected, as you would be waiting for more than"
f"the max retry-after ({self.max_retry_after}) on route {self.route}"
)

# This may support other types of limits in the future, this currently
# exists to be self-documenting to the user and for future compatibility
# only.
Expand All @@ -494,6 +504,9 @@ def remaining(self) -> typing.Literal[0]: # noqa: D401 - Imperative mood
"""
return 0

def __str__(self) -> str:
return self.message


@attr.s(auto_exc=True, slots=True, repr=False, weakref_slot=False)
class InternalServerError(HTTPResponseError):
Expand Down
23 changes: 11 additions & 12 deletions hikari/impl/buckets.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,18 +556,17 @@ def acquire(self, compiled_route: routes.CompiledRoute) -> asyncio.Future[None]:
self.real_hashes_to_buckets[real_bucket_hash] = bucket

now = time.monotonic()

if bucket.is_rate_limited(now):
if bucket.reset_at > self.max_rate_limit:
raise errors.RateLimitTooLongError(
route=compiled_route,
retry_after=bucket.reset_at - now,
max_retry_after=self.max_rate_limit,
reset_at=bucket.reset_at,
limit=bucket.limit,
period=bucket.period,
message="The request has been rejected, as you would be waiting for more than the max retry-after",
)
retry_after = bucket.reset_at - now

if bucket.is_rate_limited(now) and retry_after > self.max_rate_limit:
raise errors.RateLimitTooLongError(
route=compiled_route,
retry_after=retry_after,
max_retry_after=self.max_rate_limit,
reset_at=bucket.reset_at,
limit=bucket.limit,
period=bucket.period,
)

return bucket.acquire(self.max_rate_limit)

Expand Down

0 comments on commit e77eb0e

Please sign in to comment.