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 a2c06af
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 12 deletions.
8 changes: 4 additions & 4 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,9 +134,9 @@ 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,
},
}
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
9 changes: 8 additions & 1 deletion example.env
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,14 @@ POSTGRES_PASSWORD=tm

# If disabled project update emails will not be sent.
# Set it disabled in case of testing instances
TM_SEND_PROJECT_EMAIL_UPDATES = 1
TM_SEND_PROJECT_EMAIL_UPDATES=1

# Default threshold to rate limit api calls, seperated by comma
TM_API_RATE_LIMIT_THRESHOLD=2/second, 100/hour

# Redis configuration for rate limiter storage(optional, in memory storage is used by default if not provided.).
TM_REDIS_HOST=localhost
TM_REDIS_PORT=6379

# TM_SERVICE_DESK
# If the organisation has a service desk, configures the link
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 a2c06af

Please sign in to comment.