DRF-限制不一致

发布于 2025-02-13 16:40:26 字数 1308 浏览 1 评论 0原文

嗨,我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文