DRF 节流行为极其不一致?
DRF Throttling behaving extremely inconsistently?
我一直在我的 DRF API 中使用非常基本的用户节流率,当我尝试 运行 基准测试时,我得到的结果只是……荒谬的结果。
例如,当我将限制设置为每天 15 次时,我的 python 基准测试程序能够在实际被限制之前发出 29 (???) 请求。
这是我的基本设置:
# Django Rest Framework
REST_FRAMEWORK = {
...
'DEFAULT_THROTTLE_CLASSES': [
'rest_framework.throttling.UserRateThrottle',
],
'DEFAULT_THROTTLE_RATES': {
'user': '15/day'
}
}
这是我的基准测试程序:
#!/usr/bin/env python3
# -*- coding: iso-8859-15 -*-
import requests, time, datetime
from pprint import pprint
TOKEN = [redacted]
CSDB_NEXT_URL = "http://localhost:8000/api/siteroles?format=json"
response = requests.get(CSDB_NEXT_URL, headers={'Authorization': 'Token {}'.format(TOKEN)})
startTime = datetime.datetime.now()
for i in range(0, 52):
data = requests.get(CSDB_NEXT_URL, headers={'Authorization': 'Token {}'.format(TOKEN)}).json()
if "Request was throttled" in str(data):
break
endTime = datetime.datetime.now()
print(data, '\n', i, " requests made in ", endTime-startTime, " seconds.").
产生了
[Running] /usr/bin/env python3
"/Users/jgossett/Documents/gateway3/utils/benchmarks/test"
{'detail':
'Request was throttled. Expected available in 86389 seconds.'}
29 requests made in 0:00:12.437853 seconds.
[Done] exited with code=0 in 13.248 seconds
现在这里的数字29特别麻烦,因为它大于15?值得注意的是?有谁知道为什么会这样?
我相信我找到了不一致的原因:我的 Django 服务器正在使用默认的 LocMemCache,其 multi-threaded 缓存系统导致查询记录不一致 as was documented here
我一直在我的 DRF API 中使用非常基本的用户节流率,当我尝试 运行 基准测试时,我得到的结果只是……荒谬的结果。
例如,当我将限制设置为每天 15 次时,我的 python 基准测试程序能够在实际被限制之前发出 29 (???) 请求。
这是我的基本设置:
# Django Rest Framework
REST_FRAMEWORK = {
...
'DEFAULT_THROTTLE_CLASSES': [
'rest_framework.throttling.UserRateThrottle',
],
'DEFAULT_THROTTLE_RATES': {
'user': '15/day'
}
}
这是我的基准测试程序:
#!/usr/bin/env python3
# -*- coding: iso-8859-15 -*-
import requests, time, datetime
from pprint import pprint
TOKEN = [redacted]
CSDB_NEXT_URL = "http://localhost:8000/api/siteroles?format=json"
response = requests.get(CSDB_NEXT_URL, headers={'Authorization': 'Token {}'.format(TOKEN)})
startTime = datetime.datetime.now()
for i in range(0, 52):
data = requests.get(CSDB_NEXT_URL, headers={'Authorization': 'Token {}'.format(TOKEN)}).json()
if "Request was throttled" in str(data):
break
endTime = datetime.datetime.now()
print(data, '\n', i, " requests made in ", endTime-startTime, " seconds.").
产生了
[Running] /usr/bin/env python3 "/Users/jgossett/Documents/gateway3/utils/benchmarks/test"
{'detail': 'Request was throttled. Expected available in 86389 seconds.'}
29 requests made in 0:00:12.437853 seconds.[Done] exited with code=0 in 13.248 seconds
现在这里的数字29特别麻烦,因为它大于15?值得注意的是?有谁知道为什么会这样?
我相信我找到了不一致的原因:我的 Django 服务器正在使用默认的 LocMemCache,其 multi-threaded 缓存系统导致查询记录不一致 as was documented here