DRF-限制不一致
嗨,我在Django Rest框架中面临着节流机制的极端不一致。我有节气门10请求/sec
的API端点,但是当我使用Apache Benchmark同时发送20个请求时,API应节气门10请求,而油门4,6
>这是不一致的。有时,它甚至不会油门任何请求。
class StatusApiThrottle(CustomeRateThrottle):
rate = '10/s'
scope = 'status'
def allow_request(self, request, view): # independent throttle for GET request on view
if request.method == "POST":
return True
return super().allow_request(request, view)
缓存配置
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379/1",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient"
},
"KEY_PREFIX": "myapp"
}
}
使用Gunicorn
gunicorn app --workers 2 --threads=2
使用Apache Benchmark进行测试
$ ab -n 20 -c 20 -v 1 http://127.0.0.1:8080/api/v1/access/842b1012-53ef-4e98-9a20-484e681c3789
result #1
Complete requests: 20
Failed requests: 6
result #2
Complete requests: 20
Failed requests: 3
Hi I am facing extreme inconsistency in Throttling mechanism in Django rest framework. I have the api endpoint to throttle 10 requests/sec
but when I send 20 requests at the same time using Apache Benchmark the api should throttle 10 requests whereas it throttles 4, 6
and it is inconsistent. sometimes it does not even throttle any requests.
class StatusApiThrottle(CustomeRateThrottle):
rate = '10/s'
scope = 'status'
def allow_request(self, request, view): # independent throttle for GET request on view
if request.method == "POST":
return True
return super().allow_request(request, view)
cache configuration
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379/1",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient"
},
"KEY_PREFIX": "myapp"
}
}
Run the app using gunicorn
gunicorn app --workers 2 --threads=2
Testing using Apache benchMark
$ ab -n 20 -c 20 -v 1 http://127.0.0.1:8080/api/v1/access/842b1012-53ef-4e98-9a20-484e681c3789
result #1
Complete requests: 20
Failed requests: 6
result #2
Complete requests: 20
Failed requests: 3
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论