Skip to content

Commit a2c06af

Browse files
committed
Add redis as limiter storage instaed of memcache
1 parent 57a9f7a commit a2c06af

File tree

5 files changed

+29
-12
lines changed

5 files changed

+29
-12
lines changed

backend/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def format_url(endpoint):
3838
mail = Mail()
3939
oauth = OAuth()
4040
limiter = Limiter(
41-
storage_uri=EnvironmentConfig.MEMCACHED_URI,
41+
storage_uri=EnvironmentConfig.REDIS_URI,
4242
key_func=get_remote_address,
4343
headers_enabled=True,
4444
)
@@ -134,9 +134,9 @@ def add_api_endpoints(app):
134134
"message": "You have exceeded the rate limit. Please try again later.",
135135
"status": 429,
136136
},
137-
"MemcacheUnexpectedCloseError": {
138-
"SubCode": "MemcacheUnexpectedCloseError",
139-
"message": "Connection to Memcache server lost.",
137+
"ConnectionError": {
138+
"SubCode": "RedisConnectionError",
139+
"message": "Connection to Redis server refused.",
140140
"status": 500,
141141
},
142142
}

backend/config.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,12 @@ class EnvironmentConfig:
110110
"TM_API_RATE_LIMIT_THRESHOLD", "100 per hour"
111111
)
112112
# 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}"
113+
REDIS_PORT = os.getenv("TM_REDIS_PORT", None)
114+
REDIS_HOST = os.getenv("TM_REDIS_HOST", None)
115+
if REDIS_PORT and REDIS_HOST:
116+
REDIS_URI = f"redis://{REDIS_HOST}:{REDIS_PORT}"
117117
else:
118-
MEMCACHED_URI = None
118+
REDIS_URI = None
119119

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

docker-compose.yml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ services:
1717
<<: *backend
1818
container_name: backend
1919
restart: always
20+
depends_on:
21+
- postgresql
22+
- redis
2023
labels:
2124
- traefik.http.routers.backend.rule=Host(`localhost`) && PathPrefix(`/api/`)
2225
- traefik.http.services.backend.loadbalancer.server.port=5000
@@ -43,7 +46,14 @@ services:
4346
env_file: ${ENV_FILE:-tasking-manager.env}
4447
networks:
4548
- tm-web
46-
49+
redis:
50+
image: bitnami/redis:7.0.4
51+
container_name: redis
52+
environment:
53+
- ALLOW_EMPTY_PASSWORD=yes
54+
restart: always
55+
networks:
56+
- tm-web
4757
traefik:
4858
image: traefik:v2.3
4959
restart: always

example.env

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,14 @@ POSTGRES_PASSWORD=tm
149149

150150
# If disabled project update emails will not be sent.
151151
# Set it disabled in case of testing instances
152-
TM_SEND_PROJECT_EMAIL_UPDATES = 1
152+
TM_SEND_PROJECT_EMAIL_UPDATES=1
153+
154+
# Default threshold to rate limit api calls, seperated by comma
155+
TM_API_RATE_LIMIT_THRESHOLD=2/second, 100/hour
156+
157+
# Redis configuration for rate limiter storage(optional, in memory storage is used by default if not provided.).
158+
TM_REDIS_HOST=localhost
159+
TM_REDIS_PORT=6379
153160

154161
# TM_SERVICE_DESK
155162
# If the organisation has a service desk, configures the link

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +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
39+
redis==3.5.0
4040
newrelic==5.22.1.152
4141
nose==1.3.7
4242
oauthlib==2.0.2

0 commit comments

Comments
 (0)