diff --git a/backend/__init__.py b/backend/__init__.py index 158e60332c..34878ea6ec 100644 --- a/backend/__init__.py +++ b/backend/__init__.py @@ -2,7 +2,7 @@ import os from logging.handlers import RotatingFileHandler -from flask import Flask, redirect, make_response, jsonify +from flask import Flask, redirect from flask_cors import CORS from flask_migrate import Migrate from flask_oauthlib.client import OAuth @@ -37,8 +37,11 @@ def format_url(endpoint): migrate = Migrate() mail = Mail() oauth = OAuth() -limiter = Limiter(key_func=get_remote_address, headers_enabled=True) - +limiter = Limiter( + storage_uri=EnvironmentConfig.MEMCACHED_URI, + key_func=get_remote_address, + headers_enabled=True, +) osm = oauth.remote_app("osm", app_key="OSM_OAUTH_SETTINGS") # Import all models so that they are registered with SQLAlchemy @@ -130,7 +133,12 @@ def add_api_endpoints(app): "SubCode": "RateLimitExceeded", "message": "You have exceeded the rate limit. Please try again later.", "status": 429, - } + }, + "MemcacheUnexpectedCloseError": { + "SubCode": "MemcacheUnexpectedCloseError", + "message": "Connection to Memcache server lost.", + "status": 500, + }, } api = Api(app, errors=rate_limit_error) diff --git a/backend/config.py b/backend/config.py index 9df90bd8ce..a6aa86c17f 100644 --- a/backend/config.py +++ b/backend/config.py @@ -109,6 +109,13 @@ class EnvironmentConfig: DEFAULT_RATE_LIMIT_THRESHOLD = os.getenv( "TM_API_RATE_LIMIT_THRESHOLD", "100 per hour" ) + # Memcache configuration + MEMCACHED_PORT = os.getenv("TM_MEMCACHE_PORT", None) + MEMCACHED_HOST = os.getenv("TM_MEMCACHE_HOST", None) + if MEMCACHED_PORT and MEMCACHED_HOST: + MEMCACHED_URI = f"memcached://{MEMCACHED_HOST}:{MEMCACHED_PORT}" + else: + MEMCACHED_URI = None # Languages offered by the Tasking Manager # Please note that there must be exactly the same number of Codes as languages. diff --git a/requirements.txt b/requirements.txt index 01c931326d..501ca716d2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -36,6 +36,7 @@ Mako==1.1.3 markdown==3.3.3 MarkupSafe==1.1.1 mccabe==0.6.1 +pymemcache==3.5.2 newrelic==5.22.1.152 nose==1.3.7 oauthlib==2.0.2