限流器
警告
當前頁面任然沒有此語言的翻譯。 但是你可以幫忙翻譯一下: 貢獻.
限流器主要分为几种:全局限流器、房间限流器、装饰器限流器
优先级是层次递进的:全局限流器<房间限流器<绑定Action的限流器
全局限流和房间限流是整个BaseChannel
类都支持的。加入房间后限流器的优先级是高于权限验证的,这样可以避免多次验证,却没有拿到数据的时间损耗。
from fastapi import FastAPI, WebSocket
from fastapi_channels import add_channel
from fastapi_channels.channels import BaseChannel
from fastapi_limiter.depends import WebSocketRateLimiter
app = FastAPI()
add_channel(app, url="redis://localhost:6379", limiter_url="redis://localhost:6379",
throttle_classes=WebSocketRateLimiter(times=2, seconds=60))
c = BaseChannel()
@app.websocket('/')
async def home_page(websocket: WebSocket):
await c.connect(websocket, 'limiter')
from fastapi import FastAPI, WebSocket
from fastapi_channels import add_channel
from fastapi_channels.channels import BaseChannel
from fastapi_limiter.depends import WebSocketRateLimiter
app = FastAPI()
add_channel(app, url="redis://localhost:6379", limiter_url="redis://localhost:6379")
c = BaseChannel(throttle_classes=WebSocketRateLimiter(times=2, seconds=60))
@app.websocket('/')
async def home_page(websocket: WebSocket):
await c.connect(websocket, 'limiter')
装饰器限流器是在使用Channel
类的基础上进行设置的,它需要识别对应的action
标识,所以你不能在任意的函数上去添加这个装饰器
from fastapi import FastAPI, WebSocket
from fastapi_channels.channels import Channel
from fastapi_channels.throttling import limiter
c = Channel()
@limiter(times=2, seconds=60)
@c.action()
async def message(websocket: WebSocket, channel: str, data: dict, **kwargs):
await c.broadcast_to_channel(channel, 'hello')
app = FastAPI()
@app.websocket('/')
async def home_page(websocket: WebSocket):
await c.connect(websocket, 'limiter')