1

我使用 Django ratelimiting http://django-ratelimit.readthedocs.org/en/v0.3.0/index.html 在开发中一切正常。

然而,在使用虚拟环境时它只是不起作用。速率限制(即视图)在它应该启动的时候没有启动,或者它根本没有启动。这两个环境之间的主要区别是我的设置文件被拆分了。IE

webtools_django15/
|-- __init__.py
|-- myapp
|   |-- __init__.py
|   |-- __init__.pyc
|   |-- myapp.wsgi
|   |-- myapp_settings.py
|   |-- myapp_settings.pyc
|   |-- myapp_urls.py
|   |-- forms.py
|   |-- forms.pyc
|   |-- models.py
|   |-- tests.py
|   |-- views.py
|   |-- views.py-bak
|   `-- views.pyc
|-- manage.py
|-- modules
|   |-- __init__.py
|   `-- dnslookup.py
|-- static
|   ! omitted !
|-- templates
|   ! omitted !
`-- webtools_django15
    |-- __init__.py
    |-- __init__.pyc
    |-- settings.py
    |-- settings.py-bak
    |-- settings.pyc
    `-- urls.py

看法

@ratelimit(rate="5/s", method="POST", block=True)
@ratelimit(ip=True, rate="3/s", method="POST", block=True)
def report_ajax(request):
   ....

MYAPP.SETTINGS.PY

from settings import *

DEBUG = True
TEMPLATE_DEBUG = DEBUG

SITE_ID = 1

ROOT_URLCONF = 'myapp.myapp_urls'

TEMPLATE_DIRS = (
        "/opt/django/webtools_django15/templates"
)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # Uncomment the next line to enable the admin:
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'widget_tweaks',
    'bootstrapform',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
)

设置.py

DEBUG = True
TEMPLATE_DEBUG = DEBUG

ADMINS = (
)

MANAGERS = ADMINS

DATABASES = {
    'default': {
    }
}

LOGIN_URL = '/login/'

TIME_ZONE = 'Europe/London'

LANGUAGE_CODE = 'en-us'

SITE_ID = 1

USE_I18N = True

USE_L10N = True

MEDIA_ROOT = ''

MEDIA_URL = ''

STATIC_ROOT = ''

STATIC_URL = '/static/'

ADMIN_MEDIA_PREFIX = '/static/admin/'

STATICFILES_DIRS = (
    '/opt/django/webtools_django15/static/',
)

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

RATELIMIT_ENABLE = True
RATELIMIT_VIEW = "myapp.views.ratelimited"


TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
)

MIDDLEWARE_CLASSES = (
    'ratelimit.middleware.RatelimitMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
)

有任何想法吗 ??

4

1 回答 1

8

看起来此应用程序使用您的配置cache来保留速率限制用户的计数。我猜您在开发和生产之间的缓存环境中存在差异。

您的设置没有提及任何缓存配置,因此您可能继承了django defaults,这是一个本地内存缓存。

当您在 apache 或 wsgi 容器(如 unicorn 或 uwsgi)中运行时,它们会生成多个进程并行处理服务器请求。I 本地内存缓存不在这些进程之间共享,因此您的计数未正确跟踪。

切换到 memcache 或 redis 之类的缓存,以使这些计数在请求进程之间保持不变。

于 2013-10-12T23:34:40.527 回答