hotfix: broken rate-limiter

This commit is contained in:
Alexandre Teles 2023-03-27 15:23:40 -03:00
parent 8381b59dbf
commit 39a64c3ff8
No known key found for this signature in database
GPG Key ID: DB1C7FA46B1007F0
2 changed files with 26 additions and 14 deletions

View File

@ -43,4 +43,4 @@ database = 2
database = 3 database = 3
[auth] [auth]
access_token_expires = 300 access_token_expires = 600

26
run.py
View File

@ -19,23 +19,29 @@ config: dict = toml.load("config.toml")
# Enable sentry logging # Enable sentry logging
sentry_sdk.init(os.environ['SENTRY_DSN'], traces_sample_rate=1.0, integrations=[ sentry_sdk.init(
os.environ["SENTRY_DSN"],
traces_sample_rate=1.0,
integrations=[
RedisIntegration(), RedisIntegration(),
HttpxIntegration(), HttpxIntegration(),
GnuBacktraceIntegration(), GnuBacktraceIntegration(),
],) ],
)
LOG_LEVEL: Any = logging.getLevelName(config['logging']['level']) LOG_LEVEL: Any = logging.getLevelName(config["logging"]["level"])
JSON_LOGS: bool = config['logging']['json_logs'] JSON_LOGS: bool = config["logging"]["json_logs"]
WORKERS: int = int(cpu_count() + 1) WORKERS: int = int(cpu_count() + 1)
BIND: str = f'{os.environ.get("HYPERCORN_HOST")}:{os.environ.get("HYPERCORN_PORT")}' BIND: str = f'{os.environ.get("HYPERCORN_HOST")}:{os.environ.get("HYPERCORN_PORT")}'
class InterceptHandler(logging.Handler): class InterceptHandler(logging.Handler):
"""Intercept logs and forward them to Loguru. """Intercept logs and forward them to Loguru.
Args: Args:
logging.Handler (Filterer): Handler to filter logs logging.Handler (Filterer): Handler to filter logs
""" """
def emit(self, record: logging.LogRecord) -> None: def emit(self, record: logging.LogRecord) -> None:
"""Emit a log record.""" """Emit a log record."""
@ -55,7 +61,9 @@ class InterceptHandler(logging.Handler):
frame = frame.f_back frame = frame.f_back
depth += 1 depth += 1
logger.opt(depth=depth, exception=record.exc_info).log(level, record.getMessage()) logger.opt(depth=depth, exception=record.exc_info).log(
level, record.getMessage()
)
class StubbedGunicornLogger(Logger): class StubbedGunicornLogger(Logger):
@ -64,6 +72,7 @@ class StubbedGunicornLogger(Logger):
Args: Args:
Logger (object): Gunicon logger class Logger (object): Gunicon logger class
""" """
def setup(self, cfg) -> None: def setup(self, cfg) -> None:
"""Setup logger.""" """Setup logger."""
@ -100,7 +109,8 @@ class StandaloneApplication(BaseApplication):
def load_config(self) -> None: def load_config(self) -> None:
"""Load Gunicorn configuration.""" """Load Gunicorn configuration."""
config: dict = { config: dict = {
key: value for key, value in self.options.items() key: value
for key, value in self.options.items()
if key in self.cfg.settings and value is not None if key in self.cfg.settings and value is not None
} }
for key, value in config.items(): for key, value in config.items():
@ -115,7 +125,7 @@ class StandaloneApplication(BaseApplication):
return self.application return self.application
if __name__ == '__main__': if __name__ == "__main__":
intercept_handler = InterceptHandler() intercept_handler = InterceptHandler()
logging.root.setLevel(LOG_LEVEL) logging.root.setLevel(LOG_LEVEL)
@ -143,6 +153,8 @@ if __name__ == '__main__':
"worker_class": "uvicorn.workers.UvicornWorker", "worker_class": "uvicorn.workers.UvicornWorker",
"logger_class": StubbedGunicornLogger, "logger_class": StubbedGunicornLogger,
"preload": True, "preload": True,
"forwarded_allow_ips": "*",
"proxy_allow_ips": "*",
} }
StandaloneApplication(app, options).run() StandaloneApplication(app, options).run()