Skip to content

Commit 57a9f7a

Browse files
committed
Add limiter storage to memcache
1 parent eb7582d commit 57a9f7a

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

backend/__init__.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import os
33
from logging.handlers import RotatingFileHandler
44

5-
from flask import Flask, redirect, make_response, jsonify
5+
from flask import Flask, redirect
66
from flask_cors import CORS
77
from flask_migrate import Migrate
88
from flask_oauthlib.client import OAuth
@@ -37,8 +37,11 @@ def format_url(endpoint):
3737
migrate = Migrate()
3838
mail = Mail()
3939
oauth = OAuth()
40-
limiter = Limiter(key_func=get_remote_address, headers_enabled=True)
41-
40+
limiter = Limiter(
41+
storage_uri=EnvironmentConfig.MEMCACHED_URI,
42+
key_func=get_remote_address,
43+
headers_enabled=True,
44+
)
4245
osm = oauth.remote_app("osm", app_key="OSM_OAUTH_SETTINGS")
4346

4447
# Import all models so that they are registered with SQLAlchemy
@@ -130,7 +133,12 @@ def add_api_endpoints(app):
130133
"SubCode": "RateLimitExceeded",
131134
"message": "You have exceeded the rate limit. Please try again later.",
132135
"status": 429,
133-
}
136+
},
137+
"MemcacheUnexpectedCloseError": {
138+
"SubCode": "MemcacheUnexpectedCloseError",
139+
"message": "Connection to Memcache server lost.",
140+
"status": 500,
141+
},
134142
}
135143
api = Api(app, errors=rate_limit_error)
136144

backend/config.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@ class EnvironmentConfig:
109109
DEFAULT_RATE_LIMIT_THRESHOLD = os.getenv(
110110
"TM_API_RATE_LIMIT_THRESHOLD", "100 per hour"
111111
)
112+
# Memcache configuration
113+
MEMCACHED_PORT = os.getenv("TM_MEMCACHE_PORT", None)
114+
MEMCACHED_HOST = os.getenv("TM_MEMCACHE_HOST", None)
115+
if MEMCACHED_PORT and MEMCACHED_HOST:
116+
MEMCACHED_URI = f"memcached://{MEMCACHED_HOST}:{MEMCACHED_PORT}"
117+
else:
118+
MEMCACHED_URI = None
112119

113120
# Languages offered by the Tasking Manager
114121
# Please note that there must be exactly the same number of Codes as languages.

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Mako==1.1.3
3636
markdown==3.3.3
3737
MarkupSafe==1.1.1
3838
mccabe==0.6.1
39+
pymemcache==3.5.2
3940
newrelic==5.22.1.152
4041
nose==1.3.7
4142
oauthlib==2.0.2

0 commit comments

Comments
 (0)