Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

limiter.py 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #
  2. # Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. from fastapi import Request
  17. from fastapi.responses import JSONResponse
  18. from models.enums import ResultStatus
  19. from models.schemas import CodeExecutionResult
  20. from slowapi import Limiter
  21. from slowapi.errors import RateLimitExceeded
  22. from slowapi.util import get_remote_address
  23. limiter = Limiter(key_func=get_remote_address)
  24. async def rate_limit_exceeded_handler(request: Request, exc: Exception) -> JSONResponse:
  25. if isinstance(exc, RateLimitExceeded):
  26. return JSONResponse(
  27. content=CodeExecutionResult(
  28. status=ResultStatus.PROGRAM_RUNNER_ERROR,
  29. stdout="",
  30. stderr="Too many requests, please try again later",
  31. exit_code=-429,
  32. detail="Too many requests, please try again later",
  33. ).model_dump(),
  34. )
  35. raise exc