Skip to content

Commit

Permalink
Add limiter storage to memcache
Browse files Browse the repository at this point in the history
  • Loading branch information
Aadesh-Baral committed Aug 16, 2022
1 parent eb7582d commit 2d581ab
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
16 changes: 12 additions & 4 deletions backend/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
7 changes: 7 additions & 0 deletions backend/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2d581ab

Please sign in to comment.