mirror of
https://github.com/open-webui/open-webui.git
synced 2026-09-17 23:52:29 +00:00
refac
This commit is contained in:
parent
daf2295932
commit
6a85abb3f5
1 changed files with 25 additions and 1 deletions
|
|
@ -8,6 +8,8 @@ import logging
|
|||
import os
|
||||
import uuid
|
||||
from datetime import datetime, timedelta
|
||||
from threading import Lock
|
||||
from time import monotonic
|
||||
from typing import Optional, Union
|
||||
|
||||
import bcrypt
|
||||
|
|
@ -249,6 +251,28 @@ def decode_token(token: str) -> dict | None:
|
|||
return None
|
||||
|
||||
|
||||
class RateLimitFilter(logging.Filter):
|
||||
"""Limit a logger to one record per interval per process."""
|
||||
|
||||
def __init__(self, interval=60):
|
||||
super().__init__()
|
||||
self.interval = interval
|
||||
self.next_allowed = float('-inf')
|
||||
self.lock = Lock()
|
||||
|
||||
def filter(self, record):
|
||||
with self.lock:
|
||||
now = monotonic()
|
||||
if now < self.next_allowed:
|
||||
return False
|
||||
self.next_allowed = now + self.interval
|
||||
return True
|
||||
|
||||
|
||||
revocation_log = logging.getLogger(f'{__name__}.revocation')
|
||||
revocation_log.addFilter(RateLimitFilter())
|
||||
|
||||
|
||||
async def is_valid_token(decoded, redis=None) -> bool:
|
||||
"""
|
||||
Check whether a JWT has been revoked. Two mechanisms:
|
||||
|
|
@ -283,7 +307,7 @@ async def is_valid_token(decoded, redis=None) -> bool:
|
|||
except (ValueError, TypeError):
|
||||
pass
|
||||
except RedisError as e:
|
||||
log.warning('Revocation check failed; accepting token: %s', e)
|
||||
revocation_log.warning('Revocation check failed; accepting token: %s', e)
|
||||
|
||||
return True
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue