Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ desktop.ini
#логи и временные файлы
*.log
*.tmp
*.temp

*.temp
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ REDIS_PORT=6379

# APP_HOST=0.0.0.0
# APP_PORT=8000
# DEBUG=True

# DEBUG=True
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ __pycache__/
dump.rdb
*.rdb
.env
Thumbs.db

Thumbs.db
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ COPY . .
EXPOSE 8000

#запуск приложения
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Rate Limiter API

REST API сервис для ограничения частоты запросов по алгоритму Fixed Window. Поддерживает глобальные лимиты и лимиты по IP-адресу клиента.
REST API сервис для ограничения частоты запросов по алгоритму Fixed Window. Поддерживает глобальные лимиты и лимиты по IP-адресу клиента.

### Запуск через Docker

Expand Down Expand Up @@ -57,4 +57,5 @@ rate_limiter/
├── .env.example
├── .dockerignore
└── .gitignore
```

```
3 changes: 2 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: '3.8'
version: '3.8'

services:
redis:
Expand All @@ -24,3 +24,4 @@ services:
- .:/app
volumes:
redis-data:

5 changes: 3 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from fastapi import FastAPI, HTTPException, Request
from fastapi import FastAPI, HTTPException, Request
from pydantic import BaseModel
from rate_limit import RateLimiter

Expand Down Expand Up @@ -125,4 +125,5 @@ def reset_counters():
limiter.reset_counters()
return {"status": "успех", "message": "Все счетчики сброшены"}
except Exception as e:
raise HTTPException(status_code=500, detail=f"Ошибка сброса: {str(e)}")

raise HTTPException(status_code=500, detail=f"Ошибка сброса: {str(e)}")
5 changes: 3 additions & 2 deletions rate_limit.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from storage import RedisStorage
from typing import Dict, Tuple, Optional
import time
import time

class RateLimiter:
def __init__(self):
Expand Down Expand Up @@ -92,4 +92,5 @@ def reset_counters(self):
def _get_current_window(self, window_seconds: int) -> str:
current_time = int(time.time())
window_number = current_time // window_seconds
return str(window_number)

return str(window_number)
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ fastapi==0.100.0
uvicorn==0.24.0
redis==5.0.1
pydantic==2.12.5
python-dotenv==1.0.0

python-dotenv==1.0.0
5 changes: 3 additions & 2 deletions storage.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import redis
import json
import json
import os
from dotenv import load_dotenv

Expand Down Expand Up @@ -30,4 +30,5 @@ def load_config(self, config_key: str, default_config: dict) -> dict:
return default_config

def is_connected(self) -> bool:
return self.redis.ping()

return self.redis.ping()