Skip to content

Commit d8af39d

Browse files
committed
update docs
1 parent a9d970c commit d8af39d

4 files changed

Lines changed: 64 additions & 33 deletions

File tree

docs/.vuepress/data/sponsors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export const homeSponsor: Sponsor = { ...defaultSponsor };
2020
export const goldSponsors: Sponsor[] = [
2121
{
2222
link: 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQQImbvY5S8IbJ1iL6yHfExtHiAhUtLIoi7AQ&s',
23-
href: 'https://user.by.ltd/aff.php?aff=12215',
23+
href: 'https://u.bws.lol/aff.php?aff=12215',
2424
alt: 'Bywave',
2525
expiryTime: '2099-12-31T23:59:59',
2626
},

docs/.vuepress/sidebar.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export const mySidebar: ThemeSidebarMulti = {
3939
{ text: 'RBAC', link: 'RBAC' },
4040
{ text: 'CORS', link: 'CORS' },
4141
{ text: '时区', link: 'timezone' },
42-
{ text: '节流', link: 'limit' },
42+
{ text: '限流', link: 'limit' },
4343
{ text: '事务', link: 'transaction' },
4444
{ text: '缓存', link: 'cache' },
4545
{ text: '国际化', link: 'i18n' },

docs/backend/reference/limit.md

Lines changed: 61 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,76 @@
22
title: 节流
33
---
44

5-
我们有一个关于路由器的历史讨论,如果你感兴趣,可以查看:[#70](https://github.com/fastapi-practices/fastapi_best_architecture/discussions/70)
5+
在现代 Web 开发中,API 限流(Rate
6+
Limiting)是保护后端服务、防止资源滥用、保证服务稳定性的重要机制,我们有一个关于路由器的历史讨论,如果你感兴趣,可以查看:[#70](https://github.com/fastapi-practices/fastapi_best_architecture/discussions/70)
67

7-
[**fastapi-limiter** GitHub 仓库地址](https://github.com/long2ice/fastapi-limiter){.read-more}
8+
## 处理流程
89

9-
## 使用
10+
以下是 RateLimiter 处理一次请求的完整流程:
1011

11-
更多使用方法请查看官方仓库 [README](https://github.com/long2ice/fastapi-limiter/blob/master/README.md#quick-start)
12+
```mermaid
13+
graph TD
14+
A[请求进入路由依赖] --> B[初始化 Bucket 和 Limiter]
15+
B --> C[获取 Identifier]
16+
C --> D[异步尝试获取]
17+
D -->|获取成功| E[放行请求,继续业务处理]
18+
D -->|获取失败| F[计算 Retry-After]
19+
F --> G[执行 Callback<br>(默认抛出 429 异常)]
20+
```
21+
22+
## 使用方法
23+
24+
RateLimiter 设计为 FastAPI 的依赖项,直接在路由中使用 `Depends` 注入
25+
26+
### 单规则限流
27+
28+
```python
29+
# 每分钟最多 60 次
30+
@app.get(
31+
"/api/example",
32+
dependencies=[Depends(RateLimiter(Rate(5, Duration.MINUTE)))]
33+
)
34+
async def example():
35+
return {"message": "success"}
36+
```
37+
38+
### 多规则复合限流
1239

13-
```python{1,6,11-17,25,29}
14-
@app.get("/", dependencies=[Depends(RateLimiter(times=1, seconds=5))])
15-
async def index_get():
16-
return {"msg": "Hello World"}
40+
```python
41+
# 每秒 10 次 + 每分钟 100 次
42+
@app.post(
43+
"/api/heavy",
44+
dependencies=[
45+
Depends(
46+
RateLimiter(
47+
Rate(10, Duration.SECOND),
48+
Rate(100, Duration.MINUTE),
49+
)
50+
)
51+
]
52+
)
53+
async def heavy_endpoint():
54+
return {"status": "ok"}
55+
```
1756

57+
### 自定义 Identifier
1858

19-
@app.post("/", dependencies=[Depends(RateLimiter(times=1, seconds=5))])
20-
async def index_post():
21-
return {"msg": "Hello World"}
59+
```python
60+
async def user_identifier(request: Request) -> str:
61+
return f"user:{request.user.id}"
2262

2363

2464
@app.get(
25-
"/multiple",
65+
"/api/user-data",
2666
dependencies=[
27-
Depends(RateLimiter(times=1, seconds=5)),
28-
Depends(RateLimiter(times=2, seconds=15)),
29-
],
67+
Depends(
68+
RateLimiter(
69+
Rate(50, Duration.MINUTE),
70+
identifier=user_identifier,
71+
)
72+
)
73+
]
3074
)
31-
async def multiple():
32-
return {"msg": "Hello World"}
33-
34-
35-
@app.websocket("/ws")
36-
async def websocket_endpoint(websocket: WebSocket):
37-
await websocket.accept()
38-
ratelimit = WebSocketRateLimiter(times=1, seconds=5)
39-
while True:
40-
try:
41-
data = await websocket.receive_text()
42-
await ratelimit(websocket, context_key=data)
43-
await websocket.send_text("Hello, world")
44-
except HTTPException:
45-
await websocket.send_text("Hello again")
75+
async def user_data():
76+
return {"data": "protected"}
4677
```

docs/frontend/summary/quick-start.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: 快速开始
33
---
44

55
::: caution
6-
前端已默认集成【字典】功能,所以 fba 必须安装字典插件并执行插件中的 SQL 脚本
6+
前端已默认集成【字典】功能,所以 fba 必须安装字典插件(已内置)并执行插件中的 SQL 脚本
77
:::
88

99
::: steps

0 commit comments

Comments
 (0)