Skip to content

Commit

Permalink
Add redis as limiter storage instaed of memcache
Browse files Browse the repository at this point in the history
  • Loading branch information
Aadesh-Baral committed Aug 17, 2022
1 parent 57a9f7a commit c85e1eb
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
10 changes: 5 additions & 5 deletions backend/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def format_url(endpoint):
mail = Mail()
oauth = OAuth()
limiter = Limiter(
storage_uri=EnvironmentConfig.MEMCACHED_URI,
storage_uri=EnvironmentConfig.REDIS_URI,
key_func=get_remote_address,
headers_enabled=True,
)
Expand Down Expand Up @@ -134,11 +134,11 @@ def add_api_endpoints(app):
"message": "You have exceeded the rate limit. Please try again later.",
"status": 429,
},
"MemcacheUnexpectedCloseError": {
"SubCode": "MemcacheUnexpectedCloseError",
"message": "Connection to Memcache server lost.",
"ConnectionError":{
"SubCode": "RedisConnectionError",
"message": "Connection to Redis server refused.",
"status": 500,
},
}
}
api = Api(app, errors=rate_limit_error)

Expand Down
10 changes: 5 additions & 5 deletions backend/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ class EnvironmentConfig:
"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}"
REDIS_PORT = os.getenv("TM_REDIS_PORT", None)
REDIS_HOST = os.getenv("TM_REDIS_HOST", None)
if REDIS_PORT and REDIS_HOST:
REDIS_URI = f"redis://{REDIS_HOST}:{REDIS_PORT}"
else:
MEMCACHED_URI = None
REDIS_URI = None

# Languages offered by the Tasking Manager
# Please note that there must be exactly the same number of Codes as languages.
Expand Down
12 changes: 11 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ services:
<<: *backend
container_name: backend
restart: always
depends_on:
- postgresql
- redis
labels:
- traefik.http.routers.backend.rule=Host(`localhost`) && PathPrefix(`/api/`)
- traefik.http.services.backend.loadbalancer.server.port=5000
Expand All @@ -43,7 +46,14 @@ services:
env_file: ${ENV_FILE:-tasking-manager.env}
networks:
- tm-web

redis:
image: bitnami/redis:7.0.4
container_name: redis
environment:
- ALLOW_EMPTY_PASSWORD=yes
restart: always
networks:
- tm-web
traefik:
image: traefik:v2.3
restart: always
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Mako==1.1.3
markdown==3.3.3
MarkupSafe==1.1.1
mccabe==0.6.1
pymemcache==3.5.2
redis==3.5.0
newrelic==5.22.1.152
nose==1.3.7
oauthlib==2.0.2
Expand Down

0 comments on commit c85e1eb

Please sign in to comment.