mirror of
https://github.com/searxng/searxng.git
synced 2025-12-23 20:20:05 +00:00
fix rate limiting per engine
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
# pyright: strict
|
||||
import hmac
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Optional
|
||||
|
||||
from searx import get_setting
|
||||
|
||||
|
||||
class SharedDict(ABC):
|
||||
@abstractmethod
|
||||
@@ -20,3 +23,23 @@ class SharedDict(ABC):
|
||||
@abstractmethod
|
||||
def set_str(self, key: str, value: str, expire: Optional[int] = None):
|
||||
pass
|
||||
|
||||
def incr_counter(self, name: str, limit: int = 0, expire: int = 0) -> int:
|
||||
# generate dict key from name
|
||||
m = hmac.new(bytes(name, encoding='utf-8'), digestmod='sha256')
|
||||
m.update(bytes(get_setting('server.secret_key'), encoding='utf-8'))
|
||||
key = 'SearXNG_counter_' + m.hexdigest()
|
||||
|
||||
# check requests count
|
||||
count = self.get_int(key)
|
||||
if count is None:
|
||||
# initialize counter with expiration time
|
||||
self.set_int(key, 1, expire)
|
||||
return 1
|
||||
elif limit >= count or not limit:
|
||||
# update counter
|
||||
new_count = count + 1
|
||||
self.set_int(key, new_count, expire)
|
||||
return new_count
|
||||
else:
|
||||
return count
|
||||
|
||||
Reference in New Issue
Block a user