# Giới hạn tỷ lệ

MoreLogin sử dụng giới hạn tại gateway để bảo vệ API. Cấu hình sản xuất đã xác nhận cho cả API và Open API là 200 yêu cầu mỗi phút với dung lượng burst là 10.

## Hợp đồng triển khai hiện tại

| Mục | Hành vi ở môi trường production |
|  --- | --- |
| Thuật toán | GCRA (Generic Cell Rate Algorithm) |
| Cấu hình API | `rate=200`, `burst=10`, `period=1m` |
| Cấu hình Open API | `rate=200`, `burst=10`, `period=1m` |
| Tốc độ hồi phục liên tục | Một lượt cho phép mỗi 0,3 giây, tức khoảng 3,33 lượt mỗi giây |
| Burst | Tối đa 10 lượt cho phép có thể dùng được ngay |
| Khóa phạm vi của Open API | Đường dẫn request + ID nhóm |
| Khóa phạm vi của API | Đường dẫn request + ID nhóm; nếu không có ID nhóm thì lùi về ID API, rồi IP client |
| Ghi đè lúc chạy | Quy tắc API theo route hoặc theo nhóm có thể ghi đè cấu hình API mặc định |
| Request theo lô | Một request HTTP tiêu thụ một lượt cho phép của gateway; số lượng phần tử không được bộ giới hạn tính đến |


These are the confirmed production gateway values, not the Java fallback (`rate=1`, `burst=1`, `period=1s`). At `rate=200` over 60 seconds, one permit is restored every 0.3 seconds (approximately 3.33 per second), and up to 10 permits can accumulate. The localhost Local API does not traverse this public cloud gateway.

- `rate`: permits restored during `period`
- `burst`: maximum permits immediately available
- `period`: restoration interval


## Phản hồi giới hạn

```json
{
  "code": 35000,
  "msg": "API requests are too frequent. Please try again later.",
  "data": null,
  "requestId": "request-trace-id"
}
```

- The current response writer does not explicitly set HTTP `429`; inspect body `code` even when HTTP status is `200`.
- No `Retry-After`, `RateLimit-*`, or `X-RateLimit-*` headers are currently emitted.
- Handle HTTP `429` defensively for future gateway or edge-proxy changes.
- The message language can vary; branch on `code`, not `msg`.


## Thử lại an toàn

Retry reads with exponential backoff and jitter. For writes, query current state before retrying. A general public idempotency-key header is not currently documented.

```python
import random
import time
import requests

def api_request_with_retry(url, payload, headers=None, max_retries=4):
    for attempt in range(max_retries):
        response = requests.post(url, json=payload, headers=headers, timeout=30)
        try:
            body = response.json()
        except ValueError:
            body = {}

        limited = response.status_code == 429 or body.get("code") == 35000
        if not limited:
            return response

        retry_after = response.headers.get("Retry-After")
        delay = float(retry_after) if retry_after and retry_after.isdigit() else min(2 ** attempt, 30) + random.uniform(0, 0.5)
        time.sleep(delay)

    raise RuntimeError("Rate limit retry budget exhausted")
```

## Giảm số lượng yêu cầu

- Prefer batch endpoints when their semantics match the operation.
- Hãy cache dữ liệu tham chiếu như múi giờ, ngôn ngữ, kernel và mẫu thiết bị.
- Poll asynchronous operations with increasing intervals and stop at a terminal state.
- Do not blindly retry create, purchase, upload-registration, or schedule-creation requests.